Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2204,8 +2204,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;
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/structs/default-field-values/failures.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(default_field_values)]
#![feature(default_field_values)]

#[derive(Debug)]
pub struct S;
Expand Down Expand Up @@ -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> { .. };
Expand Down
26 changes: 16 additions & 10 deletions tests/ui/structs/default-field-values/failures.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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, ..);
| ^^
Expand All @@ -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);
| ^^
Expand All @@ -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`.
Loading