Skip to content

Unhelpful message for syntax error in pattern bindings #72298

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

Closed
benschulz opened this issue May 17, 2020 · 1 comment · Fixed by #117289
Closed

Unhelpful message for syntax error in pattern bindings #72298

benschulz opened this issue May 17, 2020 · 1 comment · Fixed by #117289
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@benschulz
Copy link
Contributor

When changing a pattern binding from something like

match some_value {
    SomeEnum::SomeVariant { ref some_field } => { /* do something with some_field */ },
    _ => {}
}

to something like

match some_value {
    SomeEnum::SomeVariant { some_field: ref alias } => { /* do something with alias */ },
    _ => {}
}

it's easy to end up with

match some_value {
    SomeEnum::SomeVariant { ref some_field: alias } => { /* do something with alias */ },
    _ => {}
}

which yields an unhelpful syntax error message (playground). I don't believe it's obvious why ref some_field and some_field: alias are valid, but ref some_field: alias is not. The same holds for ref mut bindings, of course.

It would be nice if rustc's parser accepted the invalid syntax and provided one of its helpful "did you mean to write some_field: ref alias?" error messages.

@benschulz benschulz added the C-bug Category: This is a bug. label May 17, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels May 17, 2020
@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. labels Aug 3, 2023
@estebank
Copy link
Contributor

Current output:

error: expected `,`
  --> src/main.rs:13:47
   |
13 |         SomeEnum::SomeVariant { ref some_field: alias } => { /* do something with alias */ }
   |         ---------------------                 ^
   |         |
   |         while parsing the fields for this pattern

estebank added a commit to estebank/rust that referenced this issue Oct 27, 2023
…ming

If the user writes `S { ref field: name }` instead of
`S { field: ref name }`, we suggest the correct code.

Fix rust-lang#72298.
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 1, 2023
Account for `ref` and `mut` in the wrong place for pattern ident renaming

If the user writes `S { ref field: name }` instead of `S { field: ref name }`, we suggest the correct code.

Fix rust-lang#72298.
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 1, 2023
Account for `ref` and `mut` in the wrong place for pattern ident renaming

If the user writes `S { ref field: name }` instead of `S { field: ref name }`, we suggest the correct code.

Fix rust-lang#72298.
@bors bors closed this as completed in b589f47 Nov 1, 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-parser Area: The lexing & parsing of Rust source code to an AST A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. 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