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

misleading "try adding parentheses" in match with comma #54807

Closed
ChrisJefferson opened this issue Oct 4, 2018 · 6 comments
Closed

misleading "try adding parentheses" in match with comma #54807

ChrisJefferson opened this issue Oct 4, 2018 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@ChrisJefferson
Copy link
Contributor

Consider the following (incorrect) code.

enum E { A,B,C }

fn x(e:E) -> i32 {
    match e {
        A,B => 1,
        C => 2
    }
}

The comma in the match is obviously supposed to be a |. However, rustc recommends I rewrite to (A,B), which makes the situation worse, as the compile error becomes expected enum E, found tuple.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 4, 2018

is obviously supposed

I disagree. It could be either of these. At the point where we're suggesting this we don't know the type of the match or any other details.

We could suggest both (A, B) and A | B to make it clear that the user should put some thought into fixing the error.

@oli-obk oli-obk added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. A-diagnostics Area: Messages for errors, warnings, and lints labels Oct 4, 2018
@11Takanori
Copy link
Contributor

This seems like a nice first issue for a beginner like me to contribute to, so I'd be happy to tackle this one.

@ChrisJefferson
Copy link
Contributor Author

I don't know where the suggestions are mades but in this case (A,B) is obviously wrong, as it doesn't type check, while A|B does :)

@oli-obk
Copy link
Contributor

oli-obk commented Oct 4, 2018

I don't know where the suggestions are mades but in this case (A,B) is obviously wrong, as it doesn't type check, while A|B does :)

We have no way to distinguish between your code sample and the following code sample.

fn x(e: (u8, u8)) -> i32 {
    match e {
        A,B => 1,
        C => 2
    }
}

The error is happening during parsing. This means we have no type information available. All we know is that we're parsing a pattern. Think about it this way, what can you tell about the following code snippet:

    match e {
        A,B => 1,
        C => 2
    }

Just looking at that without looking at the surroundings tells you nothing, except that A, B and C are either constants or enum variants if the programmer didn't ignore a warning about lowercase variable names.

@ZerothLaw
Copy link

@11Takanori Just checking in - have you made progress on this issue? Willing to pitch in on it.

I'd like to slightly modified the initial code with:

enum E {A, B, C}

fn foo(e: E) -> u32 {
    match e {
        E::A,E::B => 0, 
        E::C => 1
    }
}

Error message is the same, but you can see that the compiler has more information, and still giving the wrong message.

@Krout0n
Copy link
Contributor

Krout0n commented Dec 14, 2018

Is anyone working on this issue? I'm a first-timer but want to try.

Centril added a commit to Centril/rust that referenced this issue Jan 30, 2019
…eses-in-match-with-comma, r=oli-obk

suggest `|` when `,` founds in invalid match value

Issue rust-lang#54807
I get stuck on (what | how) I should implement...
Centril added a commit to Centril/rust that referenced this issue Jan 30, 2019
…eses-in-match-with-comma, r=oli-obk

suggest `|` when `,` founds in invalid match value

Issue rust-lang#54807
I get stuck on (what | how) I should implement...
Centril added a commit to Centril/rust that referenced this issue Jan 30, 2019
…eses-in-match-with-comma, r=oli-obk

suggest `|` when `,` founds in invalid match value

Issue rust-lang#54807
I get stuck on (what | how) I should implement...
Centril added a commit to Centril/rust that referenced this issue Jan 31, 2019
…eses-in-match-with-comma, r=oli-obk

suggest `|` when `,` founds in invalid match value

Issue rust-lang#54807
I get stuck on (what | how) I should implement...
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 E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

5 participants