Skip to content

Skip filled in fields when suggesting field names #111149

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

Closed
gilescope opened this issue May 3, 2023 · 3 comments · Fixed by #112323
Closed

Skip filled in fields when suggesting field names #111149

gilescope opened this issue May 3, 2023 · 3 comments · Fixed by #112323
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

@gilescope
Copy link
Contributor

gilescope commented May 3, 2023

Code

struct S {
    ant: u32,
    bat: u32,
    cat: u32,
    dog: u32,
    elephant: u32,
    fish: u32,
    giraffe: u32
}


fn main() {
    let s = S {
        ant: 1,
        bat: 2,
        cat: 3,
        dog: 4,
        elephant: 5,
        fish: 6,
        zebra: 7
    };
}

Current output

error[E0560]: struct `S` has no field named `zebra`
  --> src/main.rs:20:9
   |
20 |         zebra: 7
   |         ^^^^^ `S` does not have this field
   |
   = note: available fields are: `ant`, `bat`, `cat`, `dog`, `elephant` ... and 2 others

Desired output

error[E0560]: struct `S` has no field named `zebra`
  --> src/main.rs:20:9
   |
20 |         zebra: 7
   |         ^^^^^ `S` does not have this field
   |
   = note: unassigned fields are: `giraffe`

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=b2d383789fe03fc2ca056487e3e7eb10

Rationale and extra context

We can't set a field on a struct more than once, so we can discount those already (likely correctly) set fields from the set of available fields to choose from. This is especially useful if you're only following the compiler error messages as the current error output does not mention giraffe in the error message. If I'm coding blind I get prompted correctly what the first few fields of the struct are named, but once I've filled in the first 5 fields correctly there's no more hints on the other fields. If we exclude the fields we've already filled in then rustc should be able to hint me to get to the end of a struct no matter how many fields it has (after a few compiles as I fill in what I need to).

Other cases

This would be especially useful if I am filling out a struct that has been autogenerated by some macro and I can't easily view the source of it.

Anything else?

This just seems like a cute quality of life improvement in the diagnostics.
(I'm hoping it's not too difficult to implement)

@gilescope gilescope 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 May 3, 2023
@gilescope gilescope changed the title Skip suggesting filled in fields when suggesting field names Skip filled in fields when suggesting field names May 3, 2023
@ilyvion
Copy link

ilyvion commented May 4, 2023

We might also want to consider what it should say when there are no "unassigned fields" and you've just attempted to add an extra one. Should it fall back to listing all of them, or have a custom message for that case?

@gilescope
Copy link
Contributor Author

Yes it would be very helpful to be told that all fields in that struct have been provided and that I've just got extra fields that are unrequited.

@TaKO8Ki TaKO8Ki self-assigned this May 4, 2023
@TaKO8Ki TaKO8Ki removed their assignment Jun 7, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 9, 2023
…ields, r=WaffleLapkin

Don't mention already-set fields in struct constructor missing field error

Fixes rust-lang#111149
@bors bors closed this as completed in 17cc282 Jun 9, 2023
@gilescope
Copy link
Contributor Author

gilescope commented Jun 10, 2023 via email

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.

3 participants