close
Skip to content

fix(mgca): Allow specifying generic args (of enum) on enum itself in unit & tuple variant constructions in (direct) const args#155198

Open
JayanAXHF wants to merge 4 commits intorust-lang:mainfrom
JayanAXHF:mgca/const-generic-args
Open

fix(mgca): Allow specifying generic args (of enum) on enum itself in unit & tuple variant constructions in (direct) const args#155198
JayanAXHF wants to merge 4 commits intorust-lang:mainfrom
JayanAXHF:mgca/const-generic-args

Conversation

@JayanAXHF
Copy link
Copy Markdown
Member

@JayanAXHF JayanAXHF commented Apr 12, 2026

View all comments

Closes #154915

Basically, we check if there are three segments and if the first one is a DefKind::Enum, and then use the correct segment for Enum::<...>::Variant syntax for both DefKind::Ctor(_, CtorKind::Const | CtorKind::Fn)

PS: i'm not sure if the common logic from these two branches could be extracted

r? fmease

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 12, 2026

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 Apr 12, 2026
@rust-log-analyzer

This comment has been minimized.

…unit & tuple variant constructions in (direct) const args
@JayanAXHF JayanAXHF force-pushed the mgca/const-generic-args branch from f0b92a1 to 28594d7 Compare April 13, 2026 13:15
@JayanAXHF
Copy link
Copy Markdown
Member Author

Fixed that @fmease

Copy link
Copy Markdown
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! I got some thoughts, apologies for the many comments; most of them are really minor that can be fixed super quickly.

View changes since this review

Comment thread tests/ui/enum/enum-min-const-generic-args.rs Outdated
Comment thread tests/ui/enum/enum-min-const-generic-args.rs Outdated
Comment thread tests/ui/enum/enum-min-const-generic-args.rs Outdated
Comment thread tests/ui/enum/enum-min-const-generic-args.rs Outdated
.prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);

// Check if this is the Enum::<...>::Variant form
let use_enum_segment = if path.segments.len() == 2
Copy link
Copy Markdown
Member

@fmease fmease Apr 13, 2026

Choose a reason for hiding this comment

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

The path should be allowed to have more than two segments. Right now, you're only allowing Enum::<...>::Variant but not e.g., very::long::path::to::Enum::<...>::Variant. So it should be >= 2 rather than == 2.

Once you implement that you also need to make sure to reject arguments on all segments before the enum segment since module::<...>::Enum::<...>::Variant should be forbidden.

Comment on lines +2769 to +2771
let use_enum_segment = if path.segments.len() == 2
&& path.segments[0].args.is_some()
&& matches!(path.segments[0].res, Res::Def(DefKind::Enum, _))
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.

Since we have leading_segments in scope (which would be equal to very::long::path::to::Enum::<...> in the example I gave above), we could use it here plus slice patterns. So something akin to:

Suggested change
let use_enum_segment = if path.segments.len() == 2
&& path.segments[0].args.is_some()
&& matches!(path.segments[0].res, Res::Def(DefKind::Enum, _))
let use_enum_segment = if [.., second_to_last] = leading_segments
&& second_to_last.args.is_some()
&& let Res::Def(DefKind::Enum, _) = second_to_last.res

Comment on lines +2775 to +2782
true
} else {
let _ = self.prohibit_generic_args(
leading_segments.iter(),
GenericsArgsErrExtend::None,
);
false
};
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.

Instead of returning true / false, you could return the argument-bearing segment to be later passed as an argument to lower_generic_args_of_path_segment.

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.

this part of the code is not removed in favour of probe_generic_path_segments

Comment on lines +2773 to +2775
// Enum::<...>::Variant form - allow enum segment to have args, use them
// instead of variant segment's args (which are typically empty)
true
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.

You need to make sure to reject arguments on the variant segment if the enum segment has arguments since Enum::<...>::Variant::<...> should be forbidden.

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.

Is there are way to use probe_generic_path_segments instead? Maybe it just needs to be generalized or split into more reusable parts? Under the current version of this PR, we now have three places that implement the enum variant argument logic.

Of course, if they can't be unified easily we shouldn't force it.

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.

Yeah, it actually fits in pretty well, i changed it to use probe_generic_path_segments in 298d470, lmk if the implementation is good

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.

wasn't sure of how to fit prohibit_generic_args with this tho

@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 13, 2026
@JayanAXHF
Copy link
Copy Markdown
Member Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 14, 2026
…generics/mgca/generic-args-on-enum-variant-segments`
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.

mGCA: Can't specify generic args (of enum) on enum itself in unit & tuple variant constructions in (direct) const args

4 participants