Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
JayanAXHF:mgca/const-generic-args
Jun 11, 2026
Merged

Conversation

@JayanAXHF

@JayanAXHF JayanAXHF commented Apr 12, 2026

Copy link
Copy Markdown
Member

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

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

This comment has been minimized.

@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

@fmease fmease left a comment

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.

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

@fmease fmease Apr 13, 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.

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 thread compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs Outdated
Comment thread compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs Outdated
Comment thread compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs Outdated
Comment thread compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
@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

@fmease fmease left a comment

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.

I'm so sorry for the delay! Some final comments

View changes since this review

Comment thread compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs Outdated
Comment thread compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs Outdated
let _ = self
.prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);

let generics = self.probe_generic_path_segments(

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.

I'm glad my suggestion to use probe_generic_path_segments is working out.

However, calling that function isn't sufficient, you still need (manually) prohibit generic args on all path segments that aren't generic using prohibit_generic_args, I know this API is a bit awkward. See the preexisting callers of probe_generic_path_segments for how they do it.

(I guess it could be nice if we'd have one function that does the "probing" and the "prohibiting" at once for convenience but I don't think we can do that yet w/o refactoring some things first, so that has to wait for another time)

@JayanAXHF JayanAXHF May 25, 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.

I don't think we can do that yet w/o refactoring some things first

I believe we can. Currently, how this is done is

                let generic_segments =
                    self.probe_generic_path_segments(path.segments, opt_self_ty, kind, did, span);
                let indices: FxHashSet<_> =
                    generic_segments.iter().map(|GenericPathSegment(_, index)| index).collect();
                let _ = self.prohibit_generic_args(
                    path.segments.iter().enumerate().filter_map(|(index, seg)| {
                        if !indices.contains(&index) { Some(seg) } else { None }
                    }),
                    GenericsArgsErrExtend::DefVariant(&path.segments),
                );

I believe this could just be turned into a function

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.

Ah, in that case, go for it! Unless you'd to leave it to a follow-up PR; this way we can merge this PR faster :)

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.

Let's go for a follow-up PR. I'll create an issue so i don't forget if that's okay

Comment thread compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs Outdated
@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 May 8, 2026
@JayanAXHF

Copy link
Copy Markdown
Member Author

apologies for the delay, will look into this on the weekend

@rustbot

This comment has been minimized.

@JayanAXHF JayanAXHF force-pushed the mgca/const-generic-args branch from c9459f9 to 1e6067d Compare May 25, 2026 13:40
@rustbot

rustbot commented May 25, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@JayanAXHF

Copy link
Copy Markdown
Member Author

Made the changes @fmease

@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 May 25, 2026

@fmease fmease left a comment

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.

Apologies for the delay, two final nits and one other note.

View changes since this review

Comment thread tests/ui/const-generics/mgca/generic-args-on-enum-variant-segments-fail.rs Outdated
Comment thread tests/ui/const-generics/mgca/generic-args-on-enum-variant-segments-fail.rs Outdated
let _ = self
.prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);

let generics = self.probe_generic_path_segments(

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.

Ah, in that case, go for it! Unless you'd to leave it to a follow-up PR; this way we can merge this PR faster :)

@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 Jun 8, 2026
@JayanAXHF JayanAXHF 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 Jun 8, 2026
@rust-log-analyzer

This comment has been minimized.

@fmease fmease removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 8, 2026
@fmease fmease added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 8, 2026
…unit & tuple variant constructions in (direct) const args
@fmease fmease force-pushed the mgca/const-generic-args branch from 9d936c2 to 7fe4408 Compare June 10, 2026 21:43
@fmease

fmease commented Jun 10, 2026

Copy link
Copy Markdown
Member

Thanks for your work and your patience! :)

I've --bless'ed the failing test since that was all it needed. and I took the liberty to squash your commits. @bors r+ rollup

@rust-bors

rust-bors Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 7fe4408 has been approved by fmease

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 10, 2026
@rust-bors rust-bors Bot merged commit 644c882 into rust-lang:main Jun 11, 2026
@rustbot rustbot added this to the 1.98.0 milestone Jun 11, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Contributor

@rust-timer build 4858221

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (4858221): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (primary -0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.9% [2.9%, 2.9%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.0% [-3.0%, -3.0%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.0% [-3.0%, 2.9%] 2

Cycles

Results (secondary -2.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.8% [-2.8%, -2.8%] 1
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 517.006s -> 518.123s (0.22%)
Artifact size: 401.43 MiB -> 400.92 MiB (-0.13%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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

6 participants