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

Bad recovery for <expression> ? <statement> ; #117208

Closed
bend-n opened this issue Oct 26, 2023 · 1 comment · Fixed by #117212
Closed

Bad recovery for <expression> ? <statement> ; #117208

bend-n opened this issue Oct 26, 2023 · 1 comment · Fixed by #117212
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bend-n
Copy link
Contributor

bend-n commented Oct 26, 2023

Code

pub fn foo() {
  let v = v?
  while let Ok(_) = v {};
}

Current output

error: expected one of `.`, `;`, `?`, `else`, or an operator, found `;`
 --> src/main.rs:4:25
  |
4 |   while let Ok(_) = v {};
  |                         ^ expected one of `.`, `;`, `?`, `else`, or an operator

Desired output

error: expected `;`, found `while`
 --> src/main.rs:4:25
  |
3 |  let v = v?
  |            ^ help: add ';' here

Rationale and extra context

Confusing error

Other cases

Removing the semicolon as suggested causes

error: expected `;`, found `}`
 --> src/main.rs:4:25
  |
4 |   while let Ok(_) = v {}
  |                         ^ help: add `;` here
5 | }
  | - unexpected token

error[E0425]: cannot find value `v` in this scope
 --> src/main.rs:3:11
  |
3 |   let v = v?
  |           ^ not found in this scope

For more information about this error, try `rustc --explain E0425`.

Anything else?

Playground

@bend-n bend-n 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 Oct 26, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 26, 2023
@clubby789 clubby789 added the A-parser Area: The parsing of Rust source code to an AST label Oct 26, 2023
@clubby789 clubby789 changed the title Wierd error with ? <no semicolon>. Bad recovery for <expression> ? <statement> ; Oct 26, 2023
@clubby789
Copy link
Contributor

clubby789 commented Oct 26, 2023

Can also be reproduced with

fn main() {
    v ? return;
}

Probably caused by Bisected to #114028

@clubby789 clubby789 removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 26, 2023
@bors bors closed this as completed in b229537 Oct 27, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 27, 2023
Rollup merge of rust-lang#117212 - clubby789:fix-ternary-recover, r=compiler-errors

Properly restore snapshot when failing to recover parsing ternary

If the recovery parsed an expression, then failed to eat a `:`, it would return `false` without restoring the snapshot. Fix this by always restoring the snapshot when returning `false`.

Draft for now because I'd like to try and improve this recovery further.

Fixes rust-lang#117208
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-parser Area: The parsing of Rust source code to an AST 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.

3 participants