Skip to content

Reject extra fields in MGCA struct const arguments#157886

Open
Shourya742 wants to merge 2 commits into
rust-lang:mainfrom
Shourya742:2026-06-14-reject-extra-field-mgca-adt-const
Open

Reject extra fields in MGCA struct const arguments#157886
Shourya742 wants to merge 2 commits into
rust-lang:mainfrom
Shourya742:2026-06-14-reject-extra-field-mgca-adt-const

Conversation

@Shourya742

@Shourya742 Shourya742 commented Jun 14, 2026

Copy link
Copy Markdown
Member

closes: #154538

r? @BoxyUwU

@rustbot

rustbot commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator

HIR ty lowering was modified

cc @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 14, 2026
@rustbot

rustbot commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 22 candidates

@rustbot rustbot assigned BoxyUwU and unassigned davidtwco Jun 14, 2026
Comment on lines +2594 to +2603
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());
}
}

@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

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),

@camelid camelid Jun 15, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This message seems a little off... the expression does have the field; it's the struct definition that does not. We should match the wording for the normal (non-MGCA) message: "struct S has no field named x".

View changes since the review

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.

hmm.. Now I think of it, should have not written expressions. I will update it, Thanks.

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) {

@camelid camelid Jun 15, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems not ideal for performance to have an O(n^2) loop here. Probably fine in most cases but would scale badly for really large structs that sometimes appear in generated code. Can you check what the equivalent code for non-MGCA struct exprs does? Not critical to match it here but would be nice (though for all I know, it's also O(n^2)...).

View changes since the review

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.

I will take a look. We also have a similar O(n^2) loop below this for handling missing and duplicate initializers, so I have made a note of it. Thanks for the suggestion. I willl also look into non-MGCA struct expressions handling.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's true, so don't worry too much about it. It'd probably be good for us to fix both occurrences in the future, but you don't have to now.

@camelid camelid assigned camelid and unassigned BoxyUwU Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: mgca: type_of called on const argument's anon const before the const argument was lowered

5 participants