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

Suggest a missing field when the rest of the field is found #87938

Closed
hkmatsumoto opened this issue Aug 11, 2021 · 0 comments · Fixed by #87960
Closed

Suggest a missing field when the rest of the field is found #87938

hkmatsumoto opened this issue Aug 11, 2021 · 0 comments · Fixed by #87960
Labels
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.

Comments

@hkmatsumoto
Copy link
Member

hkmatsumoto commented Aug 11, 2021

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2c9af7b21295b19d151a109261981cd2

enum Foo {
    Bar { alpha: u8, bravo: u8, charlie: u8 },
}

fn foo(foo: Foo) {
    match foo {
        Foo::Bar {
            alpha,
            beta, // `bravo` miswritten as `beta` here.
            charlie,
        } => todo!(),
    }
}

The current output is:

error[E0026]: variant `Foo::Bar` does not have a field named `beta`
 --> src/lib.rs:9:13
  |
9 |             beta, // `bravo` miswritten as `beta` here.
  |             ^^^^ variant `Foo::Bar` does not have this field

error[E0027]: pattern does not mention field `bravo`
  --> src/lib.rs:7:9
   |
7  | /         Foo::Bar {
8  | |             alpha,
9  | |             beta, // `bravo` miswritten as `beta` here.
10 | |             charlie,
11 | |         } => todo!(),
   | |_________^ missing field `bravo`
   |
help: include the missing field in the pattern
   |
10 |             charlie, bravo } => todo!(),
   |                    ^^^^^^^^^
help: if you don't care about this missing field, you can explicitly ignore it
   |
10 |             charlie, .. } => todo!(),
   |                    ^^^^^^

error: aborting due to 2 previous errors

Ideally the output should look like:

error[E0026]: variant `Foo::Bar` does not have a field named `beta`
 --> src/lib.rs:9:13
  |
9 |             beta, // `bravo` miswritten as `beta` here.
  |             ^^^^ help: maybe replacing this for a missing field: `bravo`

We already provide a suggestion for similar names, for example, brav0 is suggested to replace it for bravo.
Analogous to that, when the rest of the field (alpha & charlie) is mentioned, suggesting to replace the miswritten field (beta) for the missing field (bravo) can be helpful for users.

@hkmatsumoto hkmatsumoto 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 Aug 11, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this issue Sep 19, 2021
…d-for-unmentioned-field, r=estebank

Suggest replacing an inexisting field for an unmentioned field

Fix rust-lang#87938

This PR adds a suggestion to replace an inexisting field for an
unmentioned field. Given the following code:
```rust
enum Foo {
    Bar { alpha: u8, bravo: u8, charlie: u8 },
}

fn foo(foo: Foo) {
    match foo {
        Foo::Bar {
            alpha,
            beta, // `bravo` miswritten as `beta` here.
            charlie,
        } => todo!(),
    }
}
```
the compiler now emits the error messages below.
```text
error[E0026]: variant `Foo::Bar` does not have a field named `beta`
 --> src/lib.rs:9:13
  |
9 |             beta, // `bravo` miswritten as `beta` here.
  |             ^^^^
  |             |
  |             variant `Foo::Bar` does not have this field
  |             help: `Foo::Bar` has a field named `bravo`: `bravo`
```

Note that this suggestion is available iff the number of inexisting
fields and unmentioned fields are both 1.
@bors bors closed this as completed in ebd31f5 Sep 19, 2021
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 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.

1 participant