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

Make "missing field" error message more natural #82287

Merged
merged 1 commit into from
Feb 22, 2021
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
31 changes: 17 additions & 14 deletions compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,33 +1348,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span: Span,
remaining_fields: FxHashMap<Ident, (usize, &ty::FieldDef)>,
) {
let tcx = self.tcx;
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
let len = remaining_fields.len();

let mut displayable_field_names =
remaining_fields.keys().map(|ident| ident.as_str()).collect::<Vec<_>>();

displayable_field_names.sort();

let truncated_fields_error = if len <= 3 {
String::new()
} else {
format!(" and {} other field{}", (len - 3), if len - 3 == 1 { "" } else { "s" })
let mut truncated_fields_error = String::new();
let remaining_fields_names = match &displayable_field_names[..] {
[field1] => format!("`{}`", field1),
[field1, field2] => format!("`{}` and `{}`", field1, field2),
[field1, field2, field3] => format!("`{}`, `{}` and `{}`", field1, field2, field3),
_ => {
truncated_fields_error =
format!(" and {} other field{}", len - 3, pluralize!(len - 3));
displayable_field_names
.iter()
.take(3)
.map(|n| format!("`{}`", n))
.collect::<Vec<_>>()
.join(", ")
}
};

let remaining_fields_names = displayable_field_names
.iter()
.take(3)
.map(|n| format!("`{}`", n))
.collect::<Vec<_>>()
.join(", ");

struct_span_err!(
tcx.sess,
self.tcx.sess,
span,
E0063,
"missing field{} {}{} in initializer of `{}`",
pluralize!(remaining_fields.len()),
pluralize!(len),
remaining_fields_names,
truncated_fields_error,
adt_ty
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0063.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() {
let w = SingleFoo { };
//~^ ERROR missing field `x` in initializer of `SingleFoo`
let x = PluralFoo {x: 1};
//~^ ERROR missing fields `y`, `z` in initializer of `PluralFoo`
//~^ ERROR missing fields `y` and `z` in initializer of `PluralFoo`
let y = TruncatedFoo{x:1};
//~^ missing fields `a`, `b`, `y` and 1 other field in initializer of `TruncatedFoo`
let z = TruncatedPluralFoo{x:1};
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/error-codes/E0063.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ error[E0063]: missing field `x` in initializer of `SingleFoo`
LL | let w = SingleFoo { };
| ^^^^^^^^^ missing `x`

error[E0063]: missing fields `y`, `z` in initializer of `PluralFoo`
error[E0063]: missing fields `y` and `z` in initializer of `PluralFoo`
--> $DIR/E0063.rs:34:13
|
LL | let x = PluralFoo {x: 1};
| ^^^^^^^^^ missing `y`, `z`
| ^^^^^^^^^ missing `y` and `z`

error[E0063]: missing fields `a`, `b`, `y` and 1 other field in initializer of `TruncatedFoo`
--> $DIR/E0063.rs:36:13
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-79593.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn wrong() {
foo::Enum::Variant { x: () };
//~^ ERROR missing field `y` in initializer of `Enum`
foo::Enum::Variant { };
//~^ ERROR missing fields `x`, `y` in initializer of `Enum`
//~^ ERROR missing fields `x` and `y` in initializer of `Enum`
}

fn main() {}
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-79593.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ error[E0063]: missing field `y` in initializer of `Enum`
LL | foo::Enum::Variant { x: () };
| ^^^^^^^^^^^^^^^^^^ missing `y`

error[E0063]: missing fields `x`, `y` in initializer of `Enum`
error[E0063]: missing fields `x` and `y` in initializer of `Enum`
--> $DIR/issue-79593.rs:25:5
|
LL | foo::Enum::Variant { };
| ^^^^^^^^^^^^^^^^^^ missing `x`, `y`
| ^^^^^^^^^^^^^^^^^^ missing `x` and `y`

error: aborting due to 5 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/parser/issue-52496.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ fn main() {
let bar = 1.5f32;
let _ = Foo { bar.into(), bat: -1, . };
//~^ ERROR expected one of
//~| ERROR missing fields `bar`, `baz` in initializer of `Foo`
//~| ERROR missing fields `bar` and `baz` in initializer of `Foo`
//~| ERROR expected identifier, found `.`
}
4 changes: 2 additions & 2 deletions src/test/ui/parser/issue-52496.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ error[E0063]: missing field `bat` in initializer of `Foo`
LL | let _ = Foo { bar: .5, baz: 42 };
| ^^^ missing `bat`

error[E0063]: missing fields `bar`, `baz` in initializer of `Foo`
error[E0063]: missing fields `bar` and `baz` in initializer of `Foo`
--> $DIR/issue-52496.rs:8:13
|
LL | let _ = Foo { bar.into(), bat: -1, . };
| ^^^ missing `bar`, `baz`
| ^^^ missing `bar` and `baz`

error: aborting due to 5 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/parser/struct-field-numeric-shorthand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ fn main() {
//~^ ERROR expected identifier, found `0`
//~| ERROR expected identifier, found `1`
//~| ERROR expected identifier, found `2`
//~| ERROR missing fields `0`, `1`, `2` in initializer of `Rgb`
//~| ERROR missing fields `0`, `1` and `2` in initializer of `Rgb`
}
4 changes: 2 additions & 2 deletions src/test/ui/parser/struct-field-numeric-shorthand.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ LL | let _ = Rgb { 0, 1, 2 };
| |
| while parsing this struct

error[E0063]: missing fields `0`, `1`, `2` in initializer of `Rgb`
error[E0063]: missing fields `0`, `1` and `2` in initializer of `Rgb`
--> $DIR/struct-field-numeric-shorthand.rs:4:13
|
LL | let _ = Rgb { 0, 1, 2 };
| ^^^ missing `0`, `1`, `2`
| ^^^ missing `0`, `1` and `2`

error: aborting due to 4 previous errors

Expand Down