Rollup merge of #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
This commit is contained in:
Stuart Cook 2025-04-08 20:55:01 +10:00 committed by GitHub
commit 6257825c8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 14 deletions

View File

@ -2205,8 +2205,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let fields = listify(&missing_mandatory_fields, |f| format!("`{f}`")).unwrap();
self.dcx()
.struct_span_err(
span.shrink_to_hi(),
format!("missing mandatory field{s} {fields}"),
span.shrink_to_lo(),
format!("missing field{s} {fields} in initializer"),
)
.with_span_label(
span.shrink_to_lo(),
"fields that do not have a defaulted value must be provided explicitly",
)
.emit();
return;

View File

@ -1,4 +1,4 @@
#![feature(default_field_values)]
#![feature(default_field_values)]
#[derive(Debug)]
pub struct S;
@ -50,7 +50,8 @@ enum E {
fn main () {
let _ = Foo { .. }; // ok
let _ = Foo::default(); // ok
let _ = Bar { .. }; //~ ERROR mandatory field
let _ = Bar { .. }; //~ ERROR missing field
let _ = Bar { baz: 0, .. }; //~ ERROR missing field
let _ = Bar::default(); // silenced
let _ = Bar { bar: S, .. }; // ok
let _ = Qux::<4> { .. };

View File

@ -27,14 +27,20 @@ LL + #[derive(Default)]
LL | pub struct S;
|
error: missing mandatory field `bar`
--> $DIR/failures.rs:53:21
error: missing field `bar` in initializer
--> $DIR/failures.rs:53:19
|
LL | let _ = Bar { .. };
| ^
| ^ fields that do not have a defaulted value must be provided explicitly
error: missing field `bar` in initializer
--> $DIR/failures.rs:54:27
|
LL | let _ = Bar { baz: 0, .. };
| ^ fields that do not have a defaulted value must be provided explicitly
error[E0308]: mismatched types
--> $DIR/failures.rs:57:17
--> $DIR/failures.rs:58:17
|
LL | let _ = Rak(..);
| --- ^^ expected `i32`, found `RangeFull`
@ -47,19 +53,19 @@ note: tuple struct defined here
LL | pub struct Rak(i32 = 42);
| ^^^
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
--> $DIR/failures.rs:57:17
--> $DIR/failures.rs:58:17
|
LL | let _ = Rak(..);
| ^^
error[E0061]: this struct takes 1 argument but 2 arguments were supplied
--> $DIR/failures.rs:59:13
--> $DIR/failures.rs:60:13
|
LL | let _ = Rak(0, ..);
| ^^^ -- unexpected argument #2 of type `RangeFull`
|
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
--> $DIR/failures.rs:59:20
--> $DIR/failures.rs:60:20
|
LL | let _ = Rak(0, ..);
| ^^
@ -75,13 +81,13 @@ LL + let _ = Rak(0);
|
error[E0061]: this struct takes 1 argument but 2 arguments were supplied
--> $DIR/failures.rs:61:13
--> $DIR/failures.rs:62:13
|
LL | let _ = Rak(.., 0);
| ^^^ -- unexpected argument #1 of type `RangeFull`
|
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
--> $DIR/failures.rs:61:17
--> $DIR/failures.rs:62:17
|
LL | let _ = Rak(.., 0);
| ^^
@ -96,7 +102,7 @@ LL - let _ = Rak(.., 0);
LL + let _ = Rak(0);
|
error: aborting due to 7 previous errors
error: aborting due to 8 previous errors
Some errors have detailed explanations: E0061, E0277, E0308.
For more information about an error, try `rustc --explain E0061`.