Skip to content

Commit 4c61dd2

Browse files
committed
correct the enum extra field and make sure the diagnostic mimic the hir_typeck
1 parent 5576c5f commit 4c61dd2

5 files changed

Lines changed: 39 additions & 13 deletions

File tree

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,10 +2593,37 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25932593

25942594
for init in inits {
25952595
if !variant_def.fields.iter().any(|field_def| field_def.name == init.field.name) {
2596-
let err = tcx.dcx().struct_span_err(
2597-
init.field.span,
2598-
format!("struct `{}` has no field named `{}`", variant_def.name, init.field),
2599-
);
2596+
let mut err = if adt_def.is_enum() {
2597+
struct_span_code_err!(
2598+
tcx.dcx(),
2599+
init.field.span,
2600+
E0559,
2601+
"variant `{}::{}` has no field named `{}`",
2602+
ty,
2603+
variant_def.name,
2604+
init.field
2605+
)
2606+
} else {
2607+
struct_span_code_err!(
2608+
tcx.dcx(),
2609+
init.field.span,
2610+
E0560,
2611+
"struct `{}` has no field named `{}`",
2612+
variant_def.name,
2613+
init.field
2614+
)
2615+
};
2616+
if adt_def.is_enum() {
2617+
err.span_label(
2618+
init.field.span,
2619+
format!("`{}::{}` does not have this field", ty, variant_def.name),
2620+
);
2621+
} else {
2622+
err.span_label(
2623+
init.field.span,
2624+
format!("`{}` does not have this field", variant_def.name),
2625+
);
2626+
}
26002627
return ty::Const::new_error(tcx, err.emit());
26012628
}
26022629
}

tests/ui/const-generics/mgca/adt_expr_unit_enum_extra_field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ enum E {
88
fn foo<const N: E>() {}
99

1010
fn main() {
11-
E::S { x: const { 1 } };
11+
foo::<{E::S { x: const { 1 } }}>();
1212
//~^ ERROR variant `E::S` has no field named `x` [E0559]
1313
}

tests/ui/const-generics/mgca/adt_expr_unit_enum_extra_field.stderr

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error[E0559]: variant `E::S` has no field named `x`
2-
--> $DIR/adt_expr_unit_enum_extra_field.rs:11:12
2+
--> $DIR/adt_expr_unit_enum_extra_field.rs:11:19
33
|
4-
LL | E::S { x: const { 1 } };
5-
| ^ `E::S` does not have this field
6-
|
7-
= note: all struct fields are already assigned
4+
LL | foo::<{E::S { x: const { 1 } }}>();
5+
| ^ `E::S` does not have this field
86

97
error: aborting due to 1 previous error
108

tests/ui/const-generics/mgca/adt_expr_unit_struct_extra_field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ fn foo<const N: Foo>() {}
77

88
fn main() {
99
foo::<{ Foo { field: const { 1 } } }>();
10-
//~^ ERROR struct `Foo` has no field named `field`
10+
//~^ ERROR struct `Foo` has no field named `field` [E0560]
1111
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
error: struct `Foo` has no field named `field`
1+
error[E0560]: struct `Foo` has no field named `field`
22
--> $DIR/adt_expr_unit_struct_extra_field.rs:9:19
33
|
44
LL | foo::<{ Foo { field: const { 1 } } }>();
5-
| ^^^^^
5+
| ^^^^^ `Foo` does not have this field
66

77
error: aborting due to 1 previous error
88

9+
For more information about this error, try `rustc --explain E0560`.

0 commit comments

Comments
 (0)