Skip to content

Reject extra fields in MGCA struct const arguments#157886

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

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.

Ok, but please still add a test for enum variants so we can at least track the message. After that, r=me

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: 325f881

@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
@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms Bot force-pushed the 2026-06-14-reject-extra-field-mgca-adt-const branch from 325f881 to a55c9f6 Compare June 17, 2026 00:52
@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms Bot force-pushed the 2026-06-14-reject-extra-field-mgca-adt-const branch from a55c9f6 to 89b62af Compare June 17, 2026 01:35
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
fmt: checked 6923 files
tidy check
tidy [rustdoc_json (src)]: `rustdoc-json-types` modified, checking format version
tidy: Skipping binary file check, read-only filesystem
tidy [style (tests)]: /checkout/tests/ui/const-generics/mgca/adt_expr_unit_enum_extra_field.rs: missing trailing newline
tidy [style (tests)]: FAIL
removing old virtual environment
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'venv'
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'virtualenv'
Requirement already satisfied: pip in ./build/venv/lib/python3.10/site-packages (26.1.1)
Collecting pip
---
info: ✓ ES-Check passed! All files are ES10 compatible.
typechecking javascript files
tidy: The following check failed: style (tests)
Bootstrap failed while executing `test src/tools/tidy tidyselftest --extra-checks=py,cpp,js,spellcheck`
Command `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools-bin/rust-tidy --root-path=/checkout --cargo-path=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo --output-dir=/checkout/obj/build --concurrency=4 --npm-path=/node/bin/yarn --ci=true --extra-checks=py,cpp,js,spellcheck` failed with exit code 1
Created at: src/bootstrap/src/core/build_steps/tool.rs:1624:23
Executed at: src/bootstrap/src/core/build_steps/test.rs:1511:29

--- BACKTRACE vvv
   0: <bootstrap::utils::exec::DeferredCommand>::finish_process
             at /checkout/src/bootstrap/src/utils/exec.rs:939:17
   1: <bootstrap::utils::exec::DeferredCommand>::wait_for_output::<&bootstrap::utils::exec::ExecutionContext>
             at /checkout/src/bootstrap/src/utils/exec.rs:831:21
   2: <bootstrap::utils::exec::ExecutionContext>::run
             at /checkout/src/bootstrap/src/utils/exec.rs:741:45
   3: <bootstrap::utils::exec::BootstrapCommand>::run::<&bootstrap::core::builder::Builder>
             at /checkout/src/bootstrap/src/utils/exec.rs:339:27
   4: <bootstrap::core::build_steps::test::Tidy as bootstrap::core::builder::Step>::run
             at /checkout/src/bootstrap/src/core/build_steps/test.rs:1511:29
   5: <bootstrap::core::builder::Builder>::ensure::<bootstrap::core::build_steps::test::Tidy>
             at /checkout/src/bootstrap/src/core/builder/mod.rs:1596:36
   6: <bootstrap::core::build_steps::test::Tidy as bootstrap::core::builder::Step>::make_run
             at /checkout/src/bootstrap/src/core/build_steps/test.rs:1433:21
   7: <bootstrap::core::builder::StepDescription>::maybe_run
             at /checkout/src/bootstrap/src/core/builder/mod.rs:476:13
   8: bootstrap::core::builder::cli_paths::match_paths_to_steps_and_run
             at /checkout/src/bootstrap/src/core/builder/cli_paths.rs:232:18
   9: <bootstrap::core::builder::Builder>::run_step_descriptions
             at /checkout/src/bootstrap/src/core/builder/mod.rs:1139:9
  10: <bootstrap::core::builder::Builder>::execute_cli
             at /checkout/src/bootstrap/src/core/builder/mod.rs:1118:14
  11: <bootstrap::Build>::build
             at /checkout/src/bootstrap/src/lib.rs:803:25
  12: bootstrap::main
             at /checkout/src/bootstrap/src/bin/main.rs:130:11
  13: <fn() as core::ops::function::FnOnce<()>>::call_once
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/core/src/ops/function.rs:250:5
  14: std::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()>
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/sys/backtrace.rs:166:18
  15: std::rt::lang_start::<()>::{closure#0}
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/rt.rs:206:18
  16: <&dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe as core::ops::function::FnOnce<()>>::call_once
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/core/src/ops/function.rs:287:21
  17: std::panicking::catch_unwind::do_call::<&dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe, i32>
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/panicking.rs:581:40
  18: std::panicking::catch_unwind::<i32, &dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe>
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/panicking.rs:544:19
  19: std::panic::catch_unwind::<&dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe, i32>
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/panic.rs:359:14
  20: std::rt::lang_start_internal::{closure#0}
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/rt.rs:175:24
  21: std::panicking::catch_unwind::do_call::<std::rt::lang_start_internal::{closure#0}, isize>
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/panicking.rs:581:40
---
  28: __libc_start_main
  29: _start


Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:02:46
  local time: Wed Jun 17 01:49:48 UTC 2026
  network time: Wed, 17 Jun 2026 01:49:49 GMT
##[error]Process completed with exit code 1.
##[group]Run echo "disk usage:"

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

6 participants