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

Parsing issue in complex nested match statement with &&. #104576

Open
StephanvanSchaik opened this issue Nov 18, 2022 · 3 comments
Open

Parsing issue in complex nested match statement with &&. #104576

StephanvanSchaik opened this issue Nov 18, 2022 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@StephanvanSchaik
Copy link

I think I found a case where I am hitting an issue with Rust's parser which seems to get confused on the following bit of code:

fn main() {
    let x = 2;
    let y = 1;
    
    let test = match x {
        2 => match y {
            1 => true,
            _ => false,
        } && true,
        _ => false,
    };
}

I expected that it would compile similar to the snippet below:

fn main() {
    let x = 2;
    let y = 1;
    
    let test = match x {
        2 => {
            let foo = match y {
                1 => true,
                _ => false,
            };
            foo && true
        }
        _ => false,
    };
}

Instead, it seems to hit a parsing issue following the && after the inner match yielding a bool, where it shows the following error instead:

error: unexpected , in pattern
  --> src/main.rs:11:18
   |
11 |         } && true,
   |                  ^
   |
help: try adding parentheses to match on a tuple...
   |
11 ~         } (&& true,
12 ~         _) => false,
   |
help: ...or a vertical bar to match on multiple alternatives
   |
11 ~         } && true |
12 ~         _ => false,

This happens on:

$ rustc --version
rustc 1.65.0 (897e37553 2022-11-02)

As well as on:

$ rustc +nightly --version
rustc 1.67.0-nightly (73c9eaf21 2022-11-07)
@StephanvanSchaik StephanvanSchaik added the C-bug Category: This is a bug. label Nov 18, 2022
@fmease
Copy link
Member

fmease commented Nov 18, 2022

It's not a parser bug. A match expression MatchExpression is classified as an ExpressionWithBlock (and notably not as an ExpressionWithoutBlock) (via) meaning the closing curly brace } terminates the expression early and the parser immediately looks for an optional comma (which in this case it doesn't find, that's fine) followed by a pattern (via).

&&true is a valid pattern (a reference to a reference to true). And thus, it complains about the , after true. It expected a => after the pattern (not sure why the error message is so vague here).

Still, this is a diagnostic issue. The suggestions are not quite on point and we could potentially guide the user in the right direction with some added notes.

@rustbot label A-diagnostics A-suggestion-diagnostics D-terse

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Nov 18, 2022
@StephanvanSchaik
Copy link
Author

Oh, I see. Thanks for the clarification!

I agree that better diagnostics would have been super helpful here in that case.

@lukas-code
Copy link
Member

lukas-code commented Nov 18, 2022

This is related to #102171 and #67347.

The compiler should suggest adding parentheses around the ExpressionWithBlock like this:

fn main() {
    let x = 2;
    let y = 1;
    
    let test = match x {
        2 => (match y {
            1 => true,
            _ => false,
        }) && true,
        _ => false,
    };
}

@Noratrieb Noratrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2023
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` C-bug Category: This is a bug. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants