Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
conradludgate opened this issue Jul 6, 2022 · 1 comment · Fixed by #99008
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@conradludgate
Copy link
Contributor

conradludgate commented Jul 6, 2022

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
@conradludgate conradludgate added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 6, 2022
@jyn514 jyn514 added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Jul 6, 2022
@obeis
Copy link
Contributor

obeis commented Jul 6, 2022

@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jul 9, 2022
@bors bors closed this as completed in d75a572 Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants