Skip to content

Commit 967ea6a

Browse files
Make error message for missing fields with .. and without .. more consistent
1 parent ecb170a commit 967ea6a

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,8 +2204,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22042204
let fields = listify(&missing_mandatory_fields, |f| format!("`{f}`")).unwrap();
22052205
self.dcx()
22062206
.struct_span_err(
2207-
span.shrink_to_hi(),
2208-
format!("missing mandatory field{s} {fields}"),
2207+
span.shrink_to_lo(),
2208+
format!("missing field{s} {fields} in initializer"),
2209+
)
2210+
.with_span_label(
2211+
span.shrink_to_lo(),
2212+
"fields that do not have a defaulted value must be provided explicitly",
22092213
)
22102214
.emit();
22112215
return;

tests/ui/structs/default-field-values/failures.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(default_field_values)]
1+
#![feature(default_field_values)]
22

33
#[derive(Debug)]
44
pub struct S;
@@ -50,7 +50,7 @@ enum E {
5050
fn main () {
5151
let _ = Foo { .. }; // ok
5252
let _ = Foo::default(); // ok
53-
let _ = Bar { .. }; //~ ERROR mandatory field
53+
let _ = Bar { .. }; //~ ERROR missing field
5454
let _ = Bar::default(); // silenced
5555
let _ = Bar { bar: S, .. }; // ok
5656
let _ = Qux::<4> { .. };

tests/ui/structs/default-field-values/failures.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ LL + #[derive(Default)]
2727
LL | pub struct S;
2828
|
2929

30-
error: missing mandatory field `bar`
31-
--> $DIR/failures.rs:53:21
30+
error: missing field `bar` in initializer
31+
--> $DIR/failures.rs:53:19
3232
|
3333
LL | let _ = Bar { .. };
34-
| ^
34+
| ^ fields that do not have a defaulted value must be provided explicitly
3535

3636
error[E0308]: mismatched types
3737
--> $DIR/failures.rs:57:17

0 commit comments

Comments
 (0)