Skip to content

Commit 1ad0549

Browse files
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 1284081 + 0e32d11 commit 1ad0549

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

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)