1
Fork 0

Make error message for missing fields with .. and without .. more consistent

This commit is contained in:
Michael Goulet 2025-03-27 17:24:06 +00:00
parent ecb170afc8
commit 250b848272
3 changed files with 25 additions and 14 deletions

View file

@ -2204,8 +2204,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let fields = listify(&missing_mandatory_fields, |f| format!("`{f}`")).unwrap(); let fields = listify(&missing_mandatory_fields, |f| format!("`{f}`")).unwrap();
self.dcx() self.dcx()
.struct_span_err( .struct_span_err(
span.shrink_to_hi(), span.shrink_to_lo(),
format!("missing mandatory field{s} {fields}"), 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(); .emit();
return; return;

View file

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

View file

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