Skip to content

matching on bitflags leads to unreachable patterns #201

@gz

Description

@gz

I was surprised to find out that matching on bitflags leads to unreachable patterns in the example below (i.e. match_flag always return 3). I'm assuming it conflicts with the | syntax in match arms, is this the intended behavior of bitflags?

use bitflags::bitflags;

bitflags! {
    pub struct Flag: u64 {
        const A      = 0;
        const B      = 1;
        const C      = 2;
    }
}

fn match_flag(f: Flag) -> usize {
    let a = Flag::A;
    let ab = Flag::A | Flag::B;
    let abc = Flag::A | Flag::B | Flag::C;
    match f {
        abc => 3,
        ab => 2,
        a => 1,
    }
}

fn main() {
    let abc = Flag::A | Flag::B | Flag::C;
    let ab = Flag::A | Flag::B;
    let a = Flag::A;
    
    assert_eq!(match_flag(abc), 3);
    assert_eq!(match_flag(ab), 2);
    assert_eq!(match_flag(a), 2);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions