Skip to content

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

Closed

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

Activity

  1. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    A-diagnosticsArea: Messages for errors, warnings, and lints
    P-lowLow priority
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    on Aug 20, 2019
  2. Centril commented on Aug 20, 2019

    @Centril
    Contributor

    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.

  3. added
    S-blockedStatus: Blocked on something else such as an RFC or other implementation work.
    on Aug 20, 2019
  4. self-assigned this
    on Aug 20, 2019
  5. Centril commented on Aug 20, 2019

    @Centril
    Contributor

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

  6. removed
    S-blockedStatus: Blocked on something else such as an RFC or other implementation work.
    on Aug 27, 2019
  7. added a commit that references this issue on Aug 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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