Skip to content

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

Closed
@estebank

Description

@estebank

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 };
  |        --        ^^^

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.P-lowLow priorityT-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