Skip to content

Commit 7cb1557

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 f65d96f commit 7cb1557

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

src/librustc_typeck/check/mod.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -3198,14 +3198,30 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
31983198
!error_happened &&
31993199
!remaining_fields.is_empty()
32003200
{
3201-
span_err!(tcx.sess, span, E0063,
3202-
"missing field{} {} in initializer of `{}`",
3203-
if remaining_fields.len() == 1 {""} else {"s"},
3204-
remaining_fields.keys()
3205-
.map(|n| format!("`{}`", n))
3206-
.collect::<Vec<_>>()
3207-
.join(", "),
3208-
adt_ty);
3201+
let len = remaining_fields.len();
3202+
3203+
let truncated_fields = if len <= 3 {
3204+
(remaining_fields.keys().take(len), "".to_string())
3205+
} else {
3206+
(remaining_fields.keys().take(3), format!(", and {} other field{}",
3207+
(len-3), if len-3 == 1 {""} else {"s"}))
3208+
};
3209+
3210+
let remaining_fields_names = truncated_fields.0
3211+
.map(|n| format!("`{}`", n))
3212+
.collect::<Vec<_>>()
3213+
.join(", ");
3214+
3215+
struct_span_err!(tcx.sess, span, E0063,
3216+
"missing field{} {}{} in initializer of `{}`",
3217+
if remaining_fields.len() == 1 {""} else {"s"},
3218+
remaining_fields_names,
3219+
truncated_fields.1,
3220+
adt_ty)
3221+
.span_label(span, &format!("missing {}{}",
3222+
remaining_fields_names,
3223+
truncated_fields.1))
3224+
.emit();
32093225
}
32103226

32113227
}

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 `z`, `y` in initializer of `PluralFoo`
47+
//~| NOTE missing `z`, `y`
48+
let y = TruncatedFoo{x:1};
49+
//~^ ERROR and 1 other field in initializer of `TruncatedFoo`
50+
//~| NOTE and 1 other field
51+
let z = TruncatedPluralFoo{x:1};
52+
//~^ ERROR and 2 other fields in initializer of `TruncatedPluralFoo`
53+
//~| NOTE and 2 other fields
1854
}

0 commit comments

Comments
 (0)