Skip to content

Reject extra fields in MGCA struct const arguments#157886

Open
Shourya742 wants to merge 3 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 3 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.

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.

Added here: 5f70f22

Comment thread compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
@camelid camelid assigned camelid and unassigned BoxyUwU Jun 15, 2026
@Shourya742 Shourya742 requested a review from camelid June 16, 2026 13:29
let err = tcx.dcx().struct_span_err(
init.field.span,
format!("struct expression has no field named `{}`", init.field),
format!("struct {} has no field named `{}`", variant_def.name, init.field),

@camelid camelid Jun 16, 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 should use backticks around the struct name, like the non-MGCA error message I showed before.

Also, please add a test with an enum struct variant (like enum E { S {} }) so we can make sure that message looks good too. You will probably have to change the message for enums, so we that can match the non-MGCA error message for that too: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=ce981a7c73a8b0d5322b9bd9f9bd628d

Otherwise, this looks good!

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.

Ah, my bad, I didn't see the backticks.

I don't think we need the enum case, since it doesn't ICE and already provides a clear error message for enum. Apparently, this ICE's only for struct variants.

@rust-cloud-vms rust-cloud-vms Bot force-pushed the 2026-06-14-reject-extra-field-mgca-adt-const branch from 5f70f22 to 84b803e Compare June 16, 2026 17:07
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