Skip to content

Fix ICE on standard constants in type constraints (min_generic_const_args)#157191

Open
sunny026 wants to merge 1 commit into
rust-lang:mainfrom
sunny026:patch-1
Open

Fix ICE on standard constants in type constraints (min_generic_const_args)#157191
sunny026 wants to merge 1 commit into
rust-lang:mainfrom
sunny026:patch-1

Conversation

@sunny026

Copy link
Copy Markdown

This PR fixes an internal compiler error (ICE) where tcx.def_kind panics when attempting to resolve un-evaluated constant aliases whose parent definition is not a type const.

By adding explicit tcx.is_type_const(uv.def) guards to DefKind::AssocConst and DefKind::Const, and explicitly capturing DefKind::AnonConst | DefKind::Const { .. } | DefKind::AssocConst { .. } in the fallback block, we ensure that constants without the min_generic_const_args attribute are handled gracefully rather than triggering an unreachable panic.

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

rustbot commented May 31, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @tiif (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, types
  • compiler, types expanded to 73 candidates
  • Random selection from 17 candidates

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

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

Hey, thanks for a PR, few things:

  • Is there a filled issue about this ICE? If so, please link this via adding Fixes <issue number> in your description
  • Question about the guard you've added, it's a bit tricky and I'm not really confident in my knowledges about const stuff, but: doesn't if something is AssocConst implies that this is "const"? Because this check reads a little uneasy to me. Same for Const and AnonConst
  • Can you please add a test, likely somewhere in tests/ui/consts maybe

View changes since this review

@rust-log-analyzer

This comment has been minimized.

@sunny026

Copy link
Copy Markdown
Author

Hey @Kivooeo, thanks for the review!

To answer your questions:

Issue number: There is no existing filled issue for this ICE. I ran into it naturally while experimenting with min_generic_const_args in type constraints. I can create an issue for tracking purposes if you'd prefer, but otherwise it's just a direct fix.

The is_type_const guard: You're completely right that AssocConst and Const imply they are constants. However, the tcx.is_type_const(uv.def) method isn't checking if it's a constant in general—it checks the specific is_type_const boolean field that lives inside the DefKind::AssocConst { is_type_const: bool } and DefKind::Const { is_type_const: bool } enum variants. We only want to apply this specific normalization path if the constant is actually being evaluated in a type context (i.e. as a generic const argument). If is_type_const is false, it's just a normal runtime constant, and passing it through the old logic was what caused the tcx.def_kind parent resolution to panic. The guard ensures we only capture actual type-level constants.

Adding a test: My latest commit actually blesses an existing test in tests/ui/const-generics/gca/ambiguous-on-failed-eval-with-vars-fail.rs. Before my fix, that test would ICE internally on the fallback, but now it correctly emits E0308: mismatched types instead of panicking! Do you think that test is sufficient since it exercises this exact code path, or would you still like me to add a dedicated, isolated test in tests/ui/consts?

@sunny026 sunny026 requested a review from Kivooeo May 31, 2026 13:42
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

rustbot commented Jun 2, 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.

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 2, 2026
…args)

Resolves an Internal Compiler Error (ICE) that occurs when standard constants are used where type const is expected under min_generic_const_args.

In the old trait solver's normalize.rs, standard consts were being blindly treated as alias projections. The solver would panic with DefId(...) does not have a const_of_item when attempting to extract metadata for an item that wasn't encoded as a type const.

This PR adds the missing tcx.is_type_const guards. If a constant is not a type const, it falls through to the standard evaluation branch, gracefully emitting an E0308 type mismatch error instead of crashing.
@rustbot rustbot removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. has-merge-commits PR has merge commits, merge with caution. labels Jun 2, 2026
@sunny026

sunny026 commented Jun 2, 2026

Copy link
Copy Markdown
Author

@Kivooeo can u review?

@Kivooeo

Kivooeo commented Jun 2, 2026

Copy link
Copy Markdown
Member

Hey, it's been only two days since you opened PR (review might take up to two weeks, because people are busy with other things than Rust) and I'm not assigned reviewer on that PR. I could assign myself for sure, but I'm very busy this week and therefore I'm trying to resolve work/life things first, thanks for understanding and your patience

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

Hi, thank you for your contribution!

I don't really understand what you mean by Before my fix, that test would ICE internally on the fallback, the test tests/ui/const-generics/gca/ambiguous-on-failed-eval-with-vars-fail.rs is not ICE'ing on either old solver or new solver. So I will prefer adding a new test that demonstrates the ICE.

I am not the right person to judge the correctness of this PR, so I will roll the mgca expert for you.

r? @BoxyUwU

View changes since this review

Comment on lines 47 to 52
fn test_proj() {
let (mut arr, mut arr_with_weird_len) = proj();
//[next]~^ ERROR type annotations needed
arr_with_weird_len = [(); 10];
//[old]~^ ERROR mismatched types
}

@tiif tiif Jun 6, 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.

This is more of a note for @BoxyUwU

I only highlight this specific example, but the same reasoning applies to other tests. Previously in both old solver and new solver, the statement arr_with_weird_len = [(); 10]; did not err in both old solver and new solver, but now it started to error only in old solver, which makes me doubt if this behavior is desirable.

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.

fun, worth noting GCA isn't supported by the old solver so the behaviour here is somewhat 🤷‍♀️

@rustbot rustbot assigned BoxyUwU and unassigned tiif Jun 6, 2026
@rustbot

rustbot commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator

BoxyUwU is currently at their maximum review capacity.
They may take a while to respond.

@BoxyUwU

BoxyUwU commented Jun 7, 2026

Copy link
Copy Markdown
Member

was this ICE only on the old solver and not the new solver? somewhat confused, there definitely needs to be a test which goes from ICE->pass on this PR but that isn't present

@BoxyUwU

BoxyUwU commented Jun 7, 2026

Copy link
Copy Markdown
Member

@rustbot author

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 7, 2026
@rustbot

rustbot commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 7, 2026
@khyperia

khyperia commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

This worries me a bit - AFAIK, UnevaluatedConstKind::Projection/Inherent/Free with non-type-consts should only be reachable under full GCA, not mGCA. I intentionally did not implement them in the old solver, and require -Znext-solver for full GCA. The fact these are reachable under mGCA under the old solver worries we might have accidentally stabilized something we shouldn't have.

Adding a test: My latest commit actually blesses an existing test in tests/ui/const-generics/gca/ambiguous-on-failed-eval-with-vars-fail.rs. Before my fix, that test would ICE internally on the fallback, but now it correctly emits E0308: mismatched types instead of panicking! Do you think that test is sufficient since it exercises this exact code path, or would you still like me to add a dedicated, isolated test in tests/ui/consts?

I'm confused by this comment a bit, sorry - you say that test currently ICEs, but, CI is passing right now, with that test not ICEing... I'm missing something!

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

7 participants