Skip to content

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

Open
GokhanKabar wants to merge 6 commits intorust-lang:mainfrom
GokhanKabar:fix-ice-enum-discr-generic-self
Open

Fix ICE when Self is used in enum discriminant of a generic enum#153815
GokhanKabar wants to merge 6 commits 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
@BoxyUwU
Copy link
Copy Markdown
Member

BoxyUwU commented Mar 20, 2026

looks good to me other than the above comment :)

@BoxyUwU
Copy link
Copy Markdown
Member

BoxyUwU commented Mar 25, 2026

I think the logic here is still slightly wrong (see #154281), we should be handling DefKind::InlineConst and DefKind::Closure here, not just DefKind::AnonConst. Would you be interested in looking into that issue too? I expect it shouldn't be too much additional work on top of what this PR already does.

I would be happy to merge this PR as is or for you to bundle a fix for that issue into this PR. Up to you. I assigned the issue to myself just to make sure I don't lose track of it but I would love for someone else to take it on if possible ^^

@theemathas
Copy link
Copy Markdown
Contributor

This code currently compiles in stable rust. If this PR makes this code fail, then a crater run is needed for this PR.

#[repr(usize)]
enum What {
    X = size_of::<*mut Self>(),
}

fn main() {}

@GokhanKabar
Copy link
Copy Markdown
Contributor Author

This code currently compiles in stable rust. If this PR makes this code fail, then a crater run is needed for this PR.

#[repr(usize)]
enum What {
    X = size_of::<*mut Self>(),
}

fn main() {}

Tested this it still compiles fine. In a non-generic enum, Self resolves to a concrete SelfTyAlias which gets lowered to the actual enum type before check_param_uses_if_mcg sees it, so the folder never flags it. No crater run needed for this specific case.

@GokhanKabar GokhanKabar requested a review from BoxyUwU April 1, 2026 22:15
@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 1, 2026
@rust-bors

This comment has been minimized.

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.
- 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
@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
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. 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"

7 participants