Skip to content
Open
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
10 changes: 10 additions & 0 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2591,6 +2591,16 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let variant_def = adt_def.variant_with_id(variant_did);
let variant_idx = adt_def.variant_index_with_id(variant_did).as_u32();

for init in inits {
if !variant_def.fields.iter().any(|field_def| field_def.name == init.field.name) {
let err = tcx.dcx().struct_span_err(
init.field.span,
format!("struct expression has no field named `{}`", init.field),
);
return ty::Const::new_error(tcx, err.emit());
}
}

Comment on lines +2594 to +2603

@Shourya742 Shourya742 Jun 14, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, lower_const_arg_struct handled missing and duplicate initializers, but it did not check for extra fields. This checkd for fields that do not exist on the target variant.

View changes since the review

let fields = variant_def
.fields
.iter()
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/const-generics/mgca/adt_expr_unit_struct_extra_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(min_generic_const_args, adt_const_params)]

#[derive(Eq, PartialEq, std::marker::ConstParamTy)]
struct Foo;

fn foo<const N: Foo>() {}

fn main() {
foo::<{ Foo { field: const { 1 } } }>();
//~^ ERROR struct expression has no field named `field`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: struct expression has no field named `field`
--> $DIR/adt_expr_unit_struct_extra_field.rs:9:19
|
LL | foo::<{ Foo { field: const { 1 } } }>();
| ^^^^^

error: aborting due to 1 previous error

Loading