Skip to content

Commit 7ebfd38

Browse files
authored
Unrolled build for rust-lang#139024
Rollup merge of rust-lang#139024 - compiler-errors:tweak-default-value-err, r=lcnr Make error message for missing fields with `..` and without `..` more consistent When `..` is not present, we say "missing field `bar` in initializer", but when it is present we say "missing mandatory field `bar`". I don't see why the primary error message should change, b/c the root cause is the same. Let's harmonize these error messages and instead use a label to explain that `..` is required b/c it's not defaulted. r? estebank
2 parents e5fefc3 + 250b848 commit 7ebfd38

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

Diff for: compiler/rustc_hir_typeck/src/expr.rs

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

Diff for: tests/ui/structs/default-field-values/failures.rs

+3-2
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,8 @@ 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
54+
let _ = Bar { baz: 0, .. }; //~ ERROR missing field
5455
let _ = Bar::default(); // silenced
5556
let _ = Bar { bar: S, .. }; // ok
5657
let _ = Qux::<4> { .. };

Diff for: tests/ui/structs/default-field-values/failures.stderr

+16-10
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,20 @@ 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
35+
36+
error: missing field `bar` in initializer
37+
--> $DIR/failures.rs:54:27
38+
|
39+
LL | let _ = Bar { baz: 0, .. };
40+
| ^ fields that do not have a defaulted value must be provided explicitly
3541

3642
error[E0308]: mismatched types
37-
--> $DIR/failures.rs:57:17
43+
--> $DIR/failures.rs:58:17
3844
|
3945
LL | let _ = Rak(..);
4046
| --- ^^ expected `i32`, found `RangeFull`
@@ -47,19 +53,19 @@ note: tuple struct defined here
4753
LL | pub struct Rak(i32 = 42);
4854
| ^^^
4955
help: you might have meant to use `..` to skip providing a value for expected fields, but this is only supported on non-tuple struct literals; it is instead interpreted as a `std::ops::RangeFull` literal
50-
--> $DIR/failures.rs:57:17
56+
--> $DIR/failures.rs:58:17
5157
|
5258
LL | let _ = Rak(..);
5359
| ^^
5460

5561
error[E0061]: this struct takes 1 argument but 2 arguments were supplied
56-
--> $DIR/failures.rs:59:13
62+
--> $DIR/failures.rs:60:13
5763
|
5864
LL | let _ = Rak(0, ..);
5965
| ^^^ -- unexpected argument #2 of type `RangeFull`
6066
|
6167
help: you might have meant to use `..` to skip providing a value for expected fields, but this is only supported on non-tuple struct literals; it is instead interpreted as a `std::ops::RangeFull` literal
62-
--> $DIR/failures.rs:59:20
68+
--> $DIR/failures.rs:60:20
6369
|
6470
LL | let _ = Rak(0, ..);
6571
| ^^
@@ -75,13 +81,13 @@ LL + let _ = Rak(0);
7581
|
7682

7783
error[E0061]: this struct takes 1 argument but 2 arguments were supplied
78-
--> $DIR/failures.rs:61:13
84+
--> $DIR/failures.rs:62:13
7985
|
8086
LL | let _ = Rak(.., 0);
8187
| ^^^ -- unexpected argument #1 of type `RangeFull`
8288
|
8389
help: you might have meant to use `..` to skip providing a value for expected fields, but this is only supported on non-tuple struct literals; it is instead interpreted as a `std::ops::RangeFull` literal
84-
--> $DIR/failures.rs:61:17
90+
--> $DIR/failures.rs:62:17
8591
|
8692
LL | let _ = Rak(.., 0);
8793
| ^^
@@ -96,7 +102,7 @@ LL - let _ = Rak(.., 0);
96102
LL + let _ = Rak(0);
97103
|
98104

99-
error: aborting due to 7 previous errors
105+
error: aborting due to 8 previous errors
100106

101107
Some errors have detailed explanations: E0061, E0277, E0308.
102108
For more information about an error, try `rustc --explain E0061`.

0 commit comments

Comments
 (0)