Skip to content

Commit 9126b08

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#35691 - jaredwy:update-error-63, r=jonathandturner
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 This is for rust-lang#35218 as part of rust-lang#35233 r? @jonathandturner
2 parents e20915f + 65b8be7 commit 9126b08

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
@@ -3207,14 +3207,30 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
32073207
!error_happened &&
32083208
!remaining_fields.is_empty()
32093209
{
3210-
span_err!(tcx.sess, span, E0063,
3211-
"missing field{} {} in initializer of `{}`",
3212-
if remaining_fields.len() == 1 {""} else {"s"},
3213-
remaining_fields.keys()
3214-
.map(|n| format!("`{}`", n))
3215-
.collect::<Vec<_>>()
3216-
.join(", "),
3217-
adt_ty);
3210+
let len = remaining_fields.len();
3211+
3212+
let truncated_fields = if len <= 3 {
3213+
(remaining_fields.keys().take(len), "".to_string())
3214+
} else {
3215+
(remaining_fields.keys().take(3), format!(", and {} other field{}",
3216+
(len-3), if len-3 == 1 {""} else {"s"}))
3217+
};
3218+
3219+
let remaining_fields_names = truncated_fields.0
3220+
.map(|n| format!("`{}`", n))
3221+
.collect::<Vec<_>>()
3222+
.join(", ");
3223+
3224+
struct_span_err!(tcx.sess, span, E0063,
3225+
"missing field{} {}{} in initializer of `{}`",
3226+
if remaining_fields.len() == 1 {""} else {"s"},
3227+
remaining_fields_names,
3228+
truncated_fields.1,
3229+
adt_ty)
3230+
.span_label(span, &format!("missing {}{}",
3231+
remaining_fields_names,
3232+
truncated_fields.1))
3233+
.emit();
32183234
}
32193235

32203236
}

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

0 commit comments

Comments
 (0)