Skip to content

Commit d382fcd

Browse files
committed
Auto merge of #30314 - fhahn:issue-30299-missing-fields, r=pnkfelix
This PR for #30299 adds the name of the type where the field is missing. The span that's used for the error seems correct. What may be confusing is when the initializer with the missing field contains other intializers. These are then included in the span. For example, consider the following listing. struct A { a1: i32, a2: B, } struct B { b1: i32, b2: i32 } fn main() { let x = A { a2: B { b1: 1, b2: 1 }, }; } It will display the following code snippet along with the message that field `a2` is missing: let x = A { a2: B { b1: 1, b2: 1 }, }; By adding the name of the type it's clearer where the field is missing.
2 parents c4c191a + 1574391 commit d382fcd

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,12 +3156,13 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
31563156
!remaining_fields.is_empty()
31573157
{
31583158
span_err!(tcx.sess, span, E0063,
3159-
"missing field{}: {}",
3159+
"missing field{} {} in initializer of `{}`",
31603160
if remaining_fields.len() == 1 {""} else {"s"},
31613161
remaining_fields.keys()
31623162
.map(|n| format!("`{}`", n))
31633163
.collect::<Vec<_>>()
3164-
.join(", "));
3164+
.join(", "),
3165+
adt_ty);
31653166
}
31663167

31673168
}

src/test/compile-fail/struct-fields-missing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct BuildData {
1515
}
1616

1717
fn main() {
18-
let foo = BuildData { //~ ERROR missing field: `bar`
18+
let foo = BuildData { //~ ERROR missing field `bar` in initializer of `BuildData`
1919
foo: 0
2020
};
2121
}

0 commit comments

Comments
 (0)