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

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

Closed
timClicks opened this issue Jan 9, 2022 · 0 comments · Fixed by #92823
Closed

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

timClicks opened this issue Jan 9, 2022 · 0 comments · Fixed by #92823
Labels
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.

Comments

@timClicks
Copy link
Contributor

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;
    }
}
@timClicks timClicks 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 Jan 9, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 25, 2022
Tweak diagnostics

* Recover from invalid `'label: ` before block.
* Make suggestion to enclose statements in a block multipart.
* Point at `match`, `while`, `loop` and `unsafe` keywords when failing
  to parse their expression. (Fix rust-lang#92705.)
* Do not suggest `{ ; }`.
* Do not suggest `|` when very unlikely to be what was wanted (in `let`
  statements).
@bors bors closed this as completed in a040e2f Feb 28, 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 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.

1 participant