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 replacing an inexisting field for an unmentioned field #87960

Conversation

hkmatsumoto
Copy link
Member

@hkmatsumoto hkmatsumoto commented Aug 12, 2021

Fix #87938

This PR adds a suggestion to replace an inexisting field for an
unmentioned field. Given the following code:

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.

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.

@rust-highfive
Copy link
Collaborator

r? @LeSeulArtichaut

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 12, 2021
@hkmatsumoto hkmatsumoto force-pushed the suggest-inexisting-field-for-unmentioned-field branch from 7def33c to 9d9f742 Compare August 12, 2021 10:58
Copy link
Contributor

@LeSeulArtichaut LeSeulArtichaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, and sorry for the delay on my part. I have a few minor comments.

compiler/rustc_typeck/src/check/pat.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/pat.rs Outdated Show resolved Hide resolved
src/test/ui/issues/issue-52717.stderr Outdated Show resolved Hide resolved
@LeSeulArtichaut
Copy link
Contributor

r? @estebank who will have a much better idea of what the best diagnostics are

src/test/ui/issues/issue-51102.stderr Outdated Show resolved Hide resolved
src/test/ui/issues/issue-52717.stderr Outdated Show resolved Hide resolved
@inquisitivecrystal inquisitivecrystal added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 24, 2021
@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 13, 2021
@hkmatsumoto hkmatsumoto force-pushed the suggest-inexisting-field-for-unmentioned-field branch from 9d9f742 to 4caab25 Compare September 13, 2021 09:12
@estebank
Copy link
Contributor

Please squash your commits. Then we can merge this :)

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.
@hkmatsumoto hkmatsumoto force-pushed the suggest-inexisting-field-for-unmentioned-field branch from 74277a4 to 37196e3 Compare September 13, 2021 12:16
@estebank
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Sep 18, 2021

📌 Commit 37196e3 has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 18, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this pull request 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 added a commit to rust-lang-ci/rust that referenced this pull request Sep 19, 2021
Rollup of 10 pull requests

Successful merges:

 - rust-lang#87960 (Suggest replacing an inexisting field for an unmentioned field)
 - rust-lang#88855 (Allow simd_shuffle to accept vectors of any length)
 - rust-lang#88966 (Check for shadowing issues involving block labels)
 - rust-lang#88996 (Fix linting when trailing macro expands to a trailing semi)
 - rust-lang#89017 (fix potential race in AtomicU64 time monotonizer)
 - rust-lang#89021 (Add a separate error for `dyn Trait` in `const fn`)
 - rust-lang#89051 (Add intra-doc links and small changes to `std::os` to be more consistent)
 - rust-lang#89053 (refactor: VecDeques IntoIter fields to private)
 - rust-lang#89055 (Suggest better place to add call parentheses for method expressions wrapped in parentheses)
 - rust-lang#89081 (Fix a typo)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ebd31f5 into rust-lang:master Sep 19, 2021
@rustbot rustbot added this to the 1.57.0 milestone Sep 19, 2021
@hkmatsumoto hkmatsumoto deleted the suggest-inexisting-field-for-unmentioned-field branch September 19, 2021 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest a missing field when the rest of the field is found
8 participants