Skip to content

Commit 0e32d11

Browse files
committed
Update the wording for E0063. This will truncate the fields to 3.
Instead of listing every field it will now show missing `a`, `z`, `b`, and 1 other field
1 parent 1df6445 commit 0e32d11

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed

src/librustc_typeck/check/mod.rs

+30-8
Original file line numberDiff line numberDiff line change
@@ -3158,14 +3158,36 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
31583158
tcx.sess.span_err(span, "union expressions should have exactly one field");
31593159
}
31603160
} else if check_completeness && !error_happened && !remaining_fields.is_empty() {
3161-
span_err!(tcx.sess, span, E0063,
3162-
"missing field{} {} in initializer of `{}`",
3163-
if remaining_fields.len() == 1 {""} else {"s"},
3164-
remaining_fields.keys()
3165-
.map(|n| format!("`{}`", n))
3166-
.collect::<Vec<_>>()
3167-
.join(", "),
3168-
adt_ty);
3161+
let len = remaining_fields.len();
3162+
3163+
let mut displayable_field_names = remaining_fields
3164+
.keys()
3165+
.map(|x| x.as_str())
3166+
.collect::<Vec<_>>();
3167+
3168+
displayable_field_names.sort();
3169+
3170+
let truncated_fields_error = if len <= 3 {
3171+
"".to_string()
3172+
} else {
3173+
format!(" and {} other field{}", (len - 3), if len - 3 == 1 {""} else {"s"})
3174+
};
3175+
3176+
let remaining_fields_names = displayable_field_names.iter().take(3)
3177+
.map(|n| format!("`{}`", n))
3178+
.collect::<Vec<_>>()
3179+
.join(", ");
3180+
3181+
struct_span_err!(tcx.sess, span, E0063,
3182+
"missing field{} {}{} in initializer of `{}`",
3183+
if remaining_fields.len() == 1 {""} else {"s"},
3184+
remaining_fields_names,
3185+
truncated_fields_error,
3186+
adt_ty)
3187+
.span_label(span, &format!("missing {}{}",
3188+
remaining_fields_names,
3189+
truncated_fields_error))
3190+
.emit();
31693191
}
31703192
}
31713193

src/test/compile-fail/E0063.rs

+39-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,47 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
struct Foo {
11+
// ignore-tidy-linelength
12+
13+
struct SingleFoo {
14+
x: i32
15+
}
16+
17+
struct PluralFoo {
18+
x: i32,
19+
y: i32,
20+
z: i32
21+
}
22+
23+
struct TruncatedFoo {
24+
a: i32,
25+
b: i32,
1226
x: i32,
13-
y: i32
27+
y: i32,
28+
z: i32
1429
}
1530

31+
struct TruncatedPluralFoo {
32+
a: i32,
33+
b: i32,
34+
c: i32,
35+
x: i32,
36+
y: i32,
37+
z: i32
38+
}
39+
40+
1641
fn main() {
17-
let x = Foo { x: 0 }; //~ ERROR E0063
42+
let w = SingleFoo { };
43+
//~^ ERROR missing field `x` in initializer of `SingleFoo`
44+
//~| NOTE missing `x`
45+
let x = PluralFoo {x: 1};
46+
//~^ ERROR missing fields `y`, `z` in initializer of `PluralFoo`
47+
//~| NOTE missing `y`, `z`
48+
let y = TruncatedFoo{x:1};
49+
//~^ missing fields `a`, `b`, `y` and 1 other field in initializer of `TruncatedFoo`
50+
//~| NOTE `a`, `b`, `y` and 1 other field
51+
let z = TruncatedPluralFoo{x:1};
52+
//~^ ERROR missing fields `a`, `b`, `c` and 2 other fields in initializer of `TruncatedPluralFoo`
53+
//~| NOTE missing `a`, `b`, `c` and 2 other fields
1854
}

0 commit comments

Comments
 (0)