Skip to content

Custom diagnostic for bad mut in pattern with sub-bindings #63764

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
estebank opened this issue Aug 20, 2019 · 2 comments · Fixed by #63945
Closed

Custom diagnostic for bad mut in pattern with sub-bindings #63764

estebank opened this issue Aug 20, 2019 · 2 comments · Fixed by #63945
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

Given (as brought up in #63693 (comment) CC @Centril):

pub fn main() {
    struct Foo { x: isize }
    let mut Foo { x: x } = Foo { x: 3 };
}

We currently emit a parse error:

error: expected one of `:`, `;`, `=`, or `@`, found `{`
 --> src/main.rs:3:17
  |
3 |     let mut Foo { x: x } = Foo { x: 3 };
  |                 ^ expected one of `:`, `;`, `=`, or `@` here

It should instead parse as if it were let Foo { x: mut x } = Foo { x: 3 }; (medium priority), silence any complaints that mut is unnecessary (lower priority) and emit a custom error explaining that mut cannot lead a pattern with subpatterns (must have), and suggest the appropriate mut placement in the sub-bindings (nice to have):

error: incorrect `mut` pattern
 --> src/main.rs:3:17
  |
3 |     let mut Foo { x: x } = Foo { x: 3 };
  |         ^^^ patterns with sub-bindings can't be marked as `mut`
help: mark the individual sub-bindings as `mut` instead
3 |     let Foo { x: mut x } = Foo { x: 3 };
  |        --        ^^^
@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Aug 20, 2019
@Centril
Copy link
Contributor

Centril commented Aug 20, 2019

This should be relatively easy to do by hooking into

fn recover_pat_ident_mut_first(&mut self) -> PResult<'a, PatKind> {
or where it is called. If a ref token does not follow you simply call the recursive pattern parser and then once you have parsed a PatKind you walk (with a mutating visitor) to all the binding sites, insert a mut there, pretty print it and suggest the output.

@Centril Centril added the S-blocked Status: Blocked on something else such as an RFC or other implementation work. label Aug 20, 2019
@Centril Centril self-assigned this Aug 20, 2019
@Centril
Copy link
Contributor

Centril commented Aug 20, 2019

Marking as blocked for now on #63693 so we don't get a lot of conflicts.

@Centril Centril removed the S-blocked Status: Blocked on something else such as an RFC or other implementation work. label Aug 27, 2019
@bors bors closed this as completed in 52c3846 Aug 29, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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. P-low Low priority 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.

2 participants