Skip to content

Commit a42e95d

Browse files
Rollup merge of #157886 - Shourya742:2026-06-14-reject-extra-field-mgca-adt-const, r=camelid
Reject extra fields in MGCA struct const arguments closes: #154538 r? @BoxyUwU
2 parents 18d7e5f + 4c61dd2 commit a42e95d

5 files changed

Lines changed: 79 additions & 0 deletions

File tree

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,6 +2594,43 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25942594
let variant_def = adt_def.variant_with_id(variant_did);
25952595
let variant_idx = adt_def.variant_index_with_id(variant_did).as_u32();
25962596

2597+
for init in inits {
2598+
if !variant_def.fields.iter().any(|field_def| field_def.name == init.field.name) {
2599+
let mut err = if adt_def.is_enum() {
2600+
struct_span_code_err!(
2601+
tcx.dcx(),
2602+
init.field.span,
2603+
E0559,
2604+
"variant `{}::{}` has no field named `{}`",
2605+
ty,
2606+
variant_def.name,
2607+
init.field
2608+
)
2609+
} else {
2610+
struct_span_code_err!(
2611+
tcx.dcx(),
2612+
init.field.span,
2613+
E0560,
2614+
"struct `{}` has no field named `{}`",
2615+
variant_def.name,
2616+
init.field
2617+
)
2618+
};
2619+
if adt_def.is_enum() {
2620+
err.span_label(
2621+
init.field.span,
2622+
format!("`{}::{}` does not have this field", ty, variant_def.name),
2623+
);
2624+
} else {
2625+
err.span_label(
2626+
init.field.span,
2627+
format!("`{}` does not have this field", variant_def.name),
2628+
);
2629+
}
2630+
return ty::Const::new_error(tcx, err.emit());
2631+
}
2632+
}
2633+
25972634
let fields = variant_def
25982635
.fields
25992636
.iter()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(min_generic_const_args, adt_const_params)]
2+
3+
#[derive(Eq, PartialEq, std::marker::ConstParamTy)]
4+
enum E {
5+
S {}
6+
}
7+
8+
fn foo<const N: E>() {}
9+
10+
fn main() {
11+
foo::<{E::S { x: const { 1 } }}>();
12+
//~^ ERROR variant `E::S` has no field named `x` [E0559]
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0559]: variant `E::S` has no field named `x`
2+
--> $DIR/adt_expr_unit_enum_extra_field.rs:11:19
3+
|
4+
LL | foo::<{E::S { x: const { 1 } }}>();
5+
| ^ `E::S` does not have this field
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0559`.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(min_generic_const_args, adt_const_params)]
2+
3+
#[derive(Eq, PartialEq, std::marker::ConstParamTy)]
4+
struct Foo;
5+
6+
fn foo<const N: Foo>() {}
7+
8+
fn main() {
9+
foo::<{ Foo { field: const { 1 } } }>();
10+
//~^ ERROR struct `Foo` has no field named `field` [E0560]
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0560]: struct `Foo` has no field named `field`
2+
--> $DIR/adt_expr_unit_struct_extra_field.rs:9:19
3+
|
4+
LL | foo::<{ Foo { field: const { 1 } } }>();
5+
| ^^^^^ `Foo` does not have this field
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0560`.

0 commit comments

Comments
 (0)