Reject extra fields in MGCA struct const arguments#157886
Conversation
|
HIR ty lowering was modified cc @fmease |
|
r? @davidtwco rustbot has assigned @davidtwco. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| 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()); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
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.
| 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), |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
hmm.. Now I think of it, should have not written expressions. I will update it, Thanks.
| 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), |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Ok, but please still add a test for enum variants so we can at least track the message. After that, r=me
5f70f22 to
84b803e
Compare
This comment has been minimized.
This comment has been minimized.
325f881 to
a55c9f6
Compare
This comment has been minimized.
This comment has been minimized.
a55c9f6 to
89b62af
Compare
|
The job Click to see the possible cause of the failure (guessed by this bot) |
closes: #154538
r? @BoxyUwU