Skip to content

Awkward pattern matching error "cannot be named the same as a tuple variant" #98974

Closed
@conradludgate

Description

@conradludgate

Given the following code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4afb15166fa46eb6f2eaf2ba053d9501

fn foo(x: Option<()>) {
    match x {
        Some => {},
        None => {},
    }
}

The current output is:

error[[E0530]](https://doc.rust-lang.org/stable/error-index.html#E0530): match bindings cannot shadow tuple variants
   --> src/lib.rs:3:9
    |
3   |         Some => {},
    |         ^^^^ cannot be named the same as a tuple variant

For more information about this error, try `rustc --explain E0530`.
error: could not compile `playground` due to previous error

Ideally the output should look like:

error[[E0530]](https://doc.rust-lang.org/stable/error-index.html#E0530): match bindings cannot shadow tuple variants
   --> src/lib.rs:3:9
    |
3   |         Some => {},
    |         ^^^^ cannot be named the same as a tuple variant
help: try specify the pattern arguments
    |
3   |         Some(_) => {},
    |             +++

For more information about this error, try `rustc --explain E0530`.
error: could not compile `playground` due to previous error

One of my colleagues who is new to Rust is familiar with the idea behind pattern matching, but was not aware of all the syntax required to make it work. They made a best guess using the demo above and was puzzled by the error message

In a similar block of code

fn foo(x: Option<()>) {
    match x {
        Some() => {},
        //  ^^
        None => {},
    }
}

the current error looks like this:

error[[E0023]](https://doc.rust-lang.org/stable/error-index.html#E0023): this pattern has 0 fields, but the corresponding tuple variant has 1 field
   --> src/lib.rs:3:9
    |
3   |         Some() => {},
    |         ^^^^^^ expected 1 field, found 0
    |
help: missing parentheses
    |
3   |         Some(()) => {},
    |             +  +

For more information about this error, try `rustc --explain E0023`.
error: could not compile `playground` due to previous error

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions