Skip to content

Fix ICE when Self is used in enum discriminant of a generic enum#153815

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
GokhanKabar:fix-ice-enum-discr-generic-self
Apr 15, 2026
Merged

Fix ICE when Self is used in enum discriminant of a generic enum#153815
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
GokhanKabar:fix-ice-enum-discr-generic-self

Conversation

@GokhanKabar
Copy link
Copy Markdown
Contributor

@GokhanKabar GokhanKabar commented Mar 13, 2026

View all comments

Fixes #153756
Let discriminant AnonConst inherit parent generics via Node::Variant in generics_of, and emit a proper error instead of span_bug! for the TooGeneric case in wfcheck.

@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 Mar 13, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 13, 2026

r? @petrochenkov

rustbot has assigned @petrochenkov.
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, types
  • compiler, types expanded to 69 candidates
  • Random selection from 15 candidates

@petrochenkov petrochenkov 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 Mar 13, 2026
@GokhanKabar GokhanKabar force-pushed the fix-ice-enum-discr-generic-self branch from 7cbee2e to c6e30c7 Compare March 13, 2026 12:49
@petrochenkov
Copy link
Copy Markdown
Contributor

There's probably a way to implement this better.
r? types

@rustbot rustbot added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Mar 13, 2026
@rustbot rustbot assigned jackh726 and unassigned petrochenkov Mar 13, 2026
@BoxyUwU
Copy link
Copy Markdown
Member

BoxyUwU commented Mar 13, 2026

r? BoxyUwU

@rustbot rustbot assigned BoxyUwU and unassigned jackh726 Mar 13, 2026
@BoxyUwU
Copy link
Copy Markdown
Member

BoxyUwU commented Mar 17, 2026

Yeah this isn't quite what we want 🤔 The general structure here is supposed to be that name resolution rejects uses of generic parameters, and the things it can't do we reject during HIR ty lowering.

Name resolution can't/doesn't reject uses of Self which alias a generic type, nor can it reject generic parameters introduce by type dependent name resolution (e.g. <Self>::Assoc turning into <u8 as Trait<T>>::Assoc). So we have special cases for those in HIR ty lowering.

Instead of adding a new place where we check for illegal generic parameters can you look into why the existing checks in HIR ty lowering aren't working correctly for discriminant exprs. I expect that what happened is that the refactoring in #150519 was overly fit to const generics rather than all anon consts and so stopped validating stuff that it should.

https://github.com/rust-lang/rust/pull/150519/changes#diff-ce4273e1e949bf3052de7a08466c4dad785890b24091122954541b615b0ba199R2161

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 17, 2026

HIR ty lowering was modified

cc @fmease

@rustbot

This comment has been minimized.

@GokhanKabar GokhanKabar force-pushed the fix-ice-enum-discr-generic-self branch from eca8904 to 94a0b1d Compare March 17, 2026 14:35
@rust-log-analyzer

This comment has been minimized.

@GokhanKabar GokhanKabar force-pushed the fix-ice-enum-discr-generic-self branch from 94a0b1d to b6afb25 Compare March 17, 2026 14:43
@rust-log-analyzer

This comment has been minimized.

@GokhanKabar GokhanKabar force-pushed the fix-ice-enum-discr-generic-self branch from 93afad5 to d09f2e8 Compare March 18, 2026 22:12
@GokhanKabar GokhanKabar force-pushed the fix-ice-enum-discr-generic-self branch from 2a868a9 to 30ddd00 Compare April 3, 2026 15:24
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 3, 2026

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.

@rust-log-analyzer

This comment has been minimized.

@GokhanKabar GokhanKabar force-pushed the fix-ice-enum-discr-generic-self branch from 30ddd00 to 87f3952 Compare April 3, 2026 15:30
@GokhanKabar GokhanKabar requested a review from BoxyUwU April 4, 2026 18:04
@@ -0,0 +1,15 @@
//@ check-fail
// Test that `Self` is rejected even when nested inside an inline const
// or closure within an enum discriminant. Regression test for issue #154281.
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.

can you add the test for the closure case ✨

@GokhanKabar GokhanKabar force-pushed the fix-ice-enum-discr-generic-self branch from 87f3952 to 16d1dcb Compare April 7, 2026 12:27
@GokhanKabar GokhanKabar requested a review from BoxyUwU April 7, 2026 14:05
@BoxyUwU
Copy link
Copy Markdown
Member

BoxyUwU commented Apr 14, 2026

@bors squash

@rust-bors

This comment has been minimized.

* Fix ICE when Self is used in enum discriminant of a generic enum

Move the validation into the existing `check_param_uses_if_mcg` machinery
in HIR ty lowering instead of adding a new check in wfcheck. After the
`AnonConstKind` refactoring, `ForbidMCGParamUsesFolder` was only gated on
`AnonConstKind::MCG`, causing discriminant anon consts (`NonTypeSystem`) to
bypass it entirely.

Add `anon_const_forbids_generic_params()` which returns the appropriate
`ForbidParamContext` for both MCG and enum discriminant contexts. Wire it
into `check_param_uses_if_mcg` so that `Self` aliasing a generic type is
caught before reaching `const_eval_poly`. Convert the `TooGeneric` span_bug
into a proper diagnostic as a fallback for anything slipping through
type-dependent path resolution.
* Address review comments

- Rename `ForbidMCGParamUsesFolder` to `ForbidParamUsesFolder`
- Rename `MinConstGenerics` variant to `ConstArgument` with updated doc
- Simplify doc comment on `anon_const_forbids_generic_params`
- Make match on `AnonConstKind` exhaustive
- Move `anon_const_def_id` inside the `if let` in `check_param_uses_if_mcg`
- Remove now-unreachable `TooGeneric` span_err in wfcheck
* Revert TooGeneric arm back to span_bug! as requested by reviewer
* Use generics_of to determine if NonTypeSystem anon consts allow generic params
* Also check InlineConst and Closure defs nested in enum discriminants
* Simplify logic for determining anonymous constant parent in generic contexts
* add test
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 14, 2026

🔨 7 commits were squashed into aacac7e.

@rust-bors rust-bors bot force-pushed the fix-ice-enum-discr-generic-self branch from b2a6842 to aacac7e Compare April 14, 2026 14:32
@BoxyUwU
Copy link
Copy Markdown
Member

BoxyUwU commented Apr 14, 2026

@bors r+

sorry for the long turnaround on reviews :) thank you for working on this

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 14, 2026

📌 Commit aacac7e has been approved by BoxyUwU

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-review Status: Awaiting review from the assignee but also interested parties. labels Apr 14, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 14, 2026
…eric-self, r=BoxyUwU

Fix ICE when Self is used in enum discriminant of a generic enum

Fixes rust-lang#153756
Let discriminant AnonConst inherit parent generics via Node::Variant in generics_of, and emit a proper error instead of span_bug! for the TooGeneric case in wfcheck.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 14, 2026
…eric-self, r=BoxyUwU

Fix ICE when Self is used in enum discriminant of a generic enum

Fixes rust-lang#153756
Let discriminant AnonConst inherit parent generics via Node::Variant in generics_of, and emit a proper error instead of span_bug! for the TooGeneric case in wfcheck.
rust-bors bot pushed a commit that referenced this pull request Apr 14, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - #153536 (Add `const_param_ty_unchecked` gate)
 - #153815 (Fix ICE when Self is used in enum discriminant of a generic enum)
 - #154882 (Gate tuple const params behind `min_adt_const_params` feature)
 - #155293 (fix arch names in cfg pretty printer)
 - #154765 (Clarify ascii whitespace exclusion of vertical tab in the doc)
 - #155172 (Some small nits for supertrait_item_shadowing, and additional testing)
 - #155279 (Test/lexer unicode pattern white space)
 - #155280 (Tests for precise-capture through RPIT and TAIT)
 - #155304 (remove PointeeParser)
@rust-bors

This comment has been minimized.

@rust-bors rust-bors bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 15, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 15, 2026

☀️ Test successful - CI
Approved by: BoxyUwU
Duration: 3h 16m 22s
Pushing bd1e7c7 to main...

@rust-bors rust-bors bot merged commit bd1e7c7 into rust-lang:main Apr 15, 2026
12 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 15, 2026
@github-actions
Copy link
Copy Markdown
Contributor

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing a5c825c (parent) -> bd1e7c7 (this PR)

Test differences

Show 8 test diffs

Stage 1

  • [ui] tests/ui/enum-discriminant/generic-self-in-discr-ice.rs: [missing] -> pass (J1)
  • [ui] tests/ui/enum-discriminant/generic-self-in-discr-inline-const.rs: [missing] -> pass (J1)

Stage 2

  • [ui] tests/ui/enum-discriminant/generic-self-in-discr-ice.rs: [missing] -> pass (J0)
  • [ui] tests/ui/enum-discriminant/generic-self-in-discr-inline-const.rs: [missing] -> pass (J0)

Additionally, 4 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard bd1e7c79485d6a4113bb11dd98cb8b415cd4a55e --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-aarch64-linux: 1h 45m -> 2h 31m (+44.5%)
  2. dist-s390x-linux: 1h 11m -> 1h 30m (+26.2%)
  3. dist-various-1: 1h 12m -> 54m 31s (-25.3%)
  4. dist-ohos-x86_64: 1h 1m -> 1h 16m (+25.1%)
  5. x86_64-msvc-ext2: 1h 53m -> 1h 24m (-25.0%)
  6. x86_64-gnu-tools: 1h 5m -> 49m 19s (-24.6%)
  7. dist-powerpc64le-linux-musl: 1h 17m -> 1h 36m (+24.2%)
  8. x86_64-gnu-gcc: 1h 8m -> 52m 19s (-24.1%)
  9. x86_64-mingw-2: 2h 45m -> 2h 5m (-24.0%)
  10. aarch64-apple: 4h 7m -> 3h 10m (-23.2%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (bd1e7c7): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.2%] 3
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (secondary 2.0%)

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)
4.9% [2.9%, 6.8%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.8% [-3.8%, -3.8%] 1
All ❌✅ (primary) - - 0

Cycles

Results (primary -0.7%, secondary 3.6%)

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

mean range count
Regressions ❌
(primary)
2.5% [2.5%, 2.5%] 1
Regressions ❌
(secondary)
3.6% [3.6%, 3.6%] 1
Improvements ✅
(primary)
-3.8% [-3.8%, -3.8%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.7% [-3.8%, 2.5%] 2

Binary size

Results (primary 0.1%, secondary 0.1%)

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

mean range count
Regressions ❌
(primary)
0.1% [0.0%, 0.1%] 15
Regressions ❌
(secondary)
0.1% [0.0%, 0.1%] 20
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [0.0%, 0.1%] 15

Bootstrap: 489.296s -> 495.503s (1.27%)
Artifact size: 394.04 MiB -> 394.22 MiB (0.05%)

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

Labels

merged-by-bors This PR was explicitly merged by bors. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: Enum discriminant referring to Self causes "... has parameters, but no args were provided in instantiate"

8 participants