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

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

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2081,13 +2081,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
},
_ => {
// prevent all specified fields from being suggested
let skip_fields = skip_fields.iter().map(|x| x.ident.name);
if let Some(field_name) = self.suggest_field_name(
variant,
field.ident.name,
skip_fields.collect(),
expr_span,
) {
let skip_fields: Vec<_> = skip_fields.iter().map(|x| x.ident.name).collect();
if let Some(field_name) =
self.suggest_field_name(variant, field.ident.name, &skip_fields, expr_span)
{
err.span_suggestion(
field.ident.span,
"a field with a similar name exists",
Expand All @@ -2108,9 +2105,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
format!("`{ty}` does not have this field"),
);
}
let available_field_names =
let mut available_field_names =
self.available_field_names(variant, expr_span);
if !available_field_names.is_empty() {
available_field_names
.retain(|name| skip_fields.iter().all(|skip| name != skip));
if available_field_names.is_empty() {
err.note("all struct fields are already assigned");
} else {
err.note(format!(
"available fields are: {}",
self.name_series_display(available_field_names)
Expand All @@ -2130,7 +2131,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self,
variant: &'tcx ty::VariantDef,
field: Symbol,
skip: Vec<Symbol>,
skip: &[Symbol],
// The span where stability will be checked
span: Span,
) -> Option<Symbol> {
Expand Down Expand Up @@ -2582,7 +2583,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
access_span: Span,
) {
if let Some(suggested_field_name) =
self.suggest_field_name(def.non_enum_variant(), field.name, vec![], access_span)
self.suggest_field_name(def.non_enum_variant(), field.name, &[], access_span)
{
err.span_suggestion(
field.span,
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/async-await/drop-track-bad-field-in-fru.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0559]: variant `Option<_>::None` has no field named `value`
|
LL | None { value: (), ..Default::default() }.await;
| ^^^^^ `Option<_>::None` does not have this field
|
= note: all struct fields are already assigned
Copy link
Member Author

Choose a reason for hiding this comment

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

perhaps this could be a span-suggestion to remove the field altogether, but honestly I'm fine with just letting the programmer decide what to do with this

Copy link
Member

Choose a reason for hiding this comment

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

I think making a suggestion would be better, but it's fine as-is


error[E0277]: `Option<_>` is not a future
--> $DIR/drop-track-bad-field-in-fru.rs:7:46
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ error[E0560]: struct `Demo` has no field named `egregiously_nonexistent_field`
LL | Self { secret_integer: 3, egregiously_nonexistent_field: () }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Demo` does not have this field
|
= note: available fields are: `favorite_integer`, `secret_integer`, `innocently_misspellable`, `another_field`, `yet_another_field` ... and 2 others
= note: available fields are: `favorite_integer`, `innocently_misspellable`, `another_field`, `yet_another_field`, `always_more_fields`, `and_ever`

error[E0609]: no field `inocently_mispellable` on type `Demo`
--> $DIR/issue-42599_available_fields_note.rs:32:41
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0560.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0560]: struct `Simba` has no field named `father`
LL | let s = Simba { mother: 1, father: 0 };
| ^^^^^^ `Simba` does not have this field
|
= note: available fields are: `mother`
= note: all struct fields are already assigned

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-5439.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0560]: struct `Foo` has no field named `nonexistent`
LL | return Box::new(Foo { nonexistent: self, foo: i });
| ^^^^^^^^^^^ `Foo` does not have this field
|
= note: available fields are: `foo`
= note: all struct fields are already assigned

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/proc-macro/span-preservation.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ error[E0560]: struct `Foo` has no field named `b`
LL | let y = Foo { a: 10, b: 10isize };
| ^ `Foo` does not have this field
|
= note: available fields are: `a`
= note: all struct fields are already assigned

error[E0308]: mismatched types
--> $DIR/span-preservation.rs:39:5
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/structs/struct-field-cfg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ error[E0560]: struct `Foo` has no field named `absent`
LL | let _ = Foo { present: (), #[cfg(all())] absent: () };
| ^^^^^^ `Foo` does not have this field
|
= note: available fields are: `present`
= note: all struct fields are already assigned

error[E0027]: pattern does not mention field `present`
--> $DIR/struct-field-cfg.rs:13:9
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/structs/struct-fields-shorthand.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0560]: struct `Foo` has no field named `z`
LL | x, y, z
| ^ `Foo` does not have this field
|
= note: available fields are: `x`, `y`
= note: all struct fields are already assigned

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/structs/struct-fields-too-many.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0560]: struct `BuildData` has no field named `bar`
LL | bar: 0
| ^^^ `BuildData` does not have this field
|
= note: available fields are: `foo`
= note: all struct fields are already assigned

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/union/union-fields-2.mirunsafeck.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ error[E0560]: union `U` has no field named `c`
LL | let u = U { a: 0, b: 1, c: 2 };
| ^ `U` does not have this field
|
= note: available fields are: `a`, `b`
= note: all struct fields are already assigned

error[E0784]: union expressions should have exactly one field
--> $DIR/union-fields-2.rs:13:13
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/union/union-fields-2.thirunsafeck.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ error[E0560]: union `U` has no field named `c`
LL | let u = U { a: 0, b: 1, c: 2 };
| ^ `U` does not have this field
|
= note: available fields are: `a`, `b`
= note: all struct fields are already assigned

error[E0784]: union expressions should have exactly one field
--> $DIR/union-fields-2.rs:13:13
Expand Down