Skip to content

Commit 17cc282

Browse files
authored
Rollup merge of #112323 - compiler-errors:dont-mention-set-fields, r=WaffleLapkin
Don't mention already-set fields in struct constructor missing field error Fixes #111149
2 parents 68c8fda + 140c011 commit 17cc282

11 files changed

+23
-20
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -2081,13 +2081,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20812081
},
20822082
_ => {
20832083
// prevent all specified fields from being suggested
2084-
let skip_fields = skip_fields.iter().map(|x| x.ident.name);
2085-
if let Some(field_name) = self.suggest_field_name(
2086-
variant,
2087-
field.ident.name,
2088-
skip_fields.collect(),
2089-
expr_span,
2090-
) {
2084+
let skip_fields: Vec<_> = skip_fields.iter().map(|x| x.ident.name).collect();
2085+
if let Some(field_name) =
2086+
self.suggest_field_name(variant, field.ident.name, &skip_fields, expr_span)
2087+
{
20912088
err.span_suggestion(
20922089
field.ident.span,
20932090
"a field with a similar name exists",
@@ -2108,9 +2105,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21082105
format!("`{ty}` does not have this field"),
21092106
);
21102107
}
2111-
let available_field_names =
2108+
let mut available_field_names =
21122109
self.available_field_names(variant, expr_span);
2113-
if !available_field_names.is_empty() {
2110+
available_field_names
2111+
.retain(|name| skip_fields.iter().all(|skip| name != skip));
2112+
if available_field_names.is_empty() {
2113+
err.note("all struct fields are already assigned");
2114+
} else {
21142115
err.note(format!(
21152116
"available fields are: {}",
21162117
self.name_series_display(available_field_names)
@@ -2130,7 +2131,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21302131
&self,
21312132
variant: &'tcx ty::VariantDef,
21322133
field: Symbol,
2133-
skip: Vec<Symbol>,
2134+
skip: &[Symbol],
21342135
// The span where stability will be checked
21352136
span: Span,
21362137
) -> Option<Symbol> {
@@ -2582,7 +2583,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25822583
access_span: Span,
25832584
) {
25842585
if let Some(suggested_field_name) =
2585-
self.suggest_field_name(def.non_enum_variant(), field.name, vec![], access_span)
2586+
self.suggest_field_name(def.non_enum_variant(), field.name, &[], access_span)
25862587
{
25872588
err.span_suggestion(
25882589
field.span,

tests/ui/async-await/drop-track-bad-field-in-fru.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0559]: variant `Option<_>::None` has no field named `value`
33
|
44
LL | None { value: (), ..Default::default() }.await;
55
| ^^^^^ `Option<_>::None` does not have this field
6+
|
7+
= note: all struct fields are already assigned
68

79
error[E0277]: `Option<_>` is not a future
810
--> $DIR/drop-track-bad-field-in-fru.rs:7:46

tests/ui/did_you_mean/issue-42599_available_fields_note.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0560]: struct `Demo` has no field named `egregiously_nonexistent_field`
1010
LL | Self { secret_integer: 3, egregiously_nonexistent_field: () }
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Demo` does not have this field
1212
|
13-
= note: available fields are: `favorite_integer`, `secret_integer`, `innocently_misspellable`, `another_field`, `yet_another_field` ... and 2 others
13+
= note: available fields are: `favorite_integer`, `innocently_misspellable`, `another_field`, `yet_another_field`, `always_more_fields`, `and_ever`
1414

1515
error[E0609]: no field `inocently_mispellable` on type `Demo`
1616
--> $DIR/issue-42599_available_fields_note.rs:32:41

tests/ui/error-codes/E0560.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0560]: struct `Simba` has no field named `father`
44
LL | let s = Simba { mother: 1, father: 0 };
55
| ^^^^^^ `Simba` does not have this field
66
|
7-
= note: available fields are: `mother`
7+
= note: all struct fields are already assigned
88

99
error: aborting due to previous error
1010

tests/ui/issues/issue-5439.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0560]: struct `Foo` has no field named `nonexistent`
44
LL | return Box::new(Foo { nonexistent: self, foo: i });
55
| ^^^^^^^^^^^ `Foo` does not have this field
66
|
7-
= note: available fields are: `foo`
7+
= note: all struct fields are already assigned
88

99
error: aborting due to previous error
1010

tests/ui/proc-macro/span-preservation.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ error[E0560]: struct `Foo` has no field named `b`
3232
LL | let y = Foo { a: 10, b: 10isize };
3333
| ^ `Foo` does not have this field
3434
|
35-
= note: available fields are: `a`
35+
= note: all struct fields are already assigned
3636

3737
error[E0308]: mismatched types
3838
--> $DIR/span-preservation.rs:39:5

tests/ui/structs/struct-field-cfg.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0560]: struct `Foo` has no field named `absent`
1010
LL | let _ = Foo { present: (), #[cfg(all())] absent: () };
1111
| ^^^^^^ `Foo` does not have this field
1212
|
13-
= note: available fields are: `present`
13+
= note: all struct fields are already assigned
1414

1515
error[E0027]: pattern does not mention field `present`
1616
--> $DIR/struct-field-cfg.rs:13:9

tests/ui/structs/struct-fields-shorthand.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0560]: struct `Foo` has no field named `z`
44
LL | x, y, z
55
| ^ `Foo` does not have this field
66
|
7-
= note: available fields are: `x`, `y`
7+
= note: all struct fields are already assigned
88

99
error: aborting due to previous error
1010

tests/ui/structs/struct-fields-too-many.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0560]: struct `BuildData` has no field named `bar`
44
LL | bar: 0
55
| ^^^ `BuildData` does not have this field
66
|
7-
= note: available fields are: `foo`
7+
= note: all struct fields are already assigned
88

99
error: aborting due to previous error
1010

tests/ui/union/union-fields-2.mirunsafeck.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error[E0560]: union `U` has no field named `c`
1616
LL | let u = U { a: 0, b: 1, c: 2 };
1717
| ^ `U` does not have this field
1818
|
19-
= note: available fields are: `a`, `b`
19+
= note: all struct fields are already assigned
2020

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

tests/ui/union/union-fields-2.thirunsafeck.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error[E0560]: union `U` has no field named `c`
1616
LL | let u = U { a: 0, b: 1, c: 2 };
1717
| ^ `U` does not have this field
1818
|
19-
= note: available fields are: `a`, `b`
19+
= note: all struct fields are already assigned
2020

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

0 commit comments

Comments
 (0)