Skip to content

Using while if let triggers an error about mismatched curly brackets #92705

Closed
@timClicks

Description

@timClicks

When refactoring, I accidentally tried to create an while if let expression. This caused the parser to give me an error about mismatched brackets. This made it difficult to fix the underlying bug.

Given the following code:

fn main() {
    let container = vec![Some(1), Some(2), None];
    
    let mut i = 0;
    while if let Some(thing) = container.get(i) {
        println!("{:?}", thing);
        i += 1;
    }
}

The current output is:

error: expected `{`, found `}`
 --> src/main.rs:9:1
  |
9 | }
  | ^ expected `{`

Ideally, the output should indicate that either the if or while should be deleted.

p.s. it turns out that the let is not actually necessary to trigger the same error message:

fn main() {
    let container = vec![Some(1), Some(2), None];
    
    let mut i = 0;
    while if let Some(thing) = container.get(i) {
        println!("{:?}", thing);
        i += 1;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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