Skip to content

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

Open
@StephanvanSchaik

Description

@StephanvanSchaik

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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