Skip to content

Pattern matching on C-like enums when values are not imported cause unintuitive behaviour #10402

Closed
@LeoTestard

Description

@LeoTestard

When doing pattern matching on a C-like enum whose values haven't be imported in the current namespace, like :

mod foo {
    enum Bar {
        Baz,
        Qux
    }
}

// ...

match someBar {
    Baz => // ...
    Qux => // ...
}

Rustc doesn't know Baz and Qux are values, since they are not imported.
The first Baz pattern is thus considered as a binding, and all following patterns are seen as "unreachable".

Maybe we should put the enum value names inside the namespace of the enum type (like C++11 enum classes), and, in any case, implement a better error or warning message. Many people apparently ran into this issue, and found it very weird and hard to debug. (cc @cmr )

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