Skip to content

fix inference variables leaking into HIR const literal lowering logic#153557

Merged
rust-bors[bot] merged 2 commits intorust-lang:mainfrom
Human9000-bit:issue-153525
Mar 19, 2026
Merged

fix inference variables leaking into HIR const literal lowering logic#153557
rust-bors[bot] merged 2 commits intorust-lang:mainfrom
Human9000-bit:issue-153525

Conversation

@Human9000-bit
Copy link
Contributor

@Human9000-bit Human9000-bit commented Mar 8, 2026

Inference variables could leak into further const lowering logic

It ICEs when query system tries to cache lit_to_const() after its execution and panics, because inference variables are not hashable for some reason

Fixes #153524
Fixes #153525

@rustbot
Copy link
Collaborator

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

rustbot commented Mar 8, 2026

r? @dingxiangfei2009

rustbot has assigned @dingxiangfei2009.
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
  • compiler expanded to 69 candidates
  • Random selection from 16 candidates

@Human9000-bit

This comment has been minimized.

@Human9000-bit Human9000-bit marked this pull request as draft March 8, 2026 08:14
@rustbot rustbot 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 8, 2026
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU
Copy link
Member

BoxyUwU commented Mar 8, 2026

r? BoxyUwU

@rustbot rustbot assigned BoxyUwU and unassigned dingxiangfei2009 Mar 8, 2026
@Human9000-bit Human9000-bit marked this pull request as ready for review March 8, 2026 15:00
@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 Mar 8, 2026
@Human9000-bit Human9000-bit changed the title fix inference variables leaking into HIR const lowering logic fix inference variables leaking into HIR const literal lowering logic Mar 8, 2026
@rust-log-analyzer

This comment has been minimized.

let tcx = self.tcx();

if ty.has_infer() {
let e = self.dcx().span_err(span, "inference variables are not supported in constants");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am sure this is pretty suboptimal error message. Could we think about a correct one?

@BoxyUwU
Copy link
Member

BoxyUwU commented Mar 16, 2026

this is interesting, ideally we to support cases where the expected type was unknown if the literal was explicitly typed, e.g. 1_i32 with an unknown expected type should work fine. The lit_to_const logic should already handle producing a lowered literal just fine in the presence of an unknown expected type so I think it's just a matter of actually allowing you to pass in nothing.

I think it would make sense to make the type in LitToConstInput optional and pass None in if there are inference variables in the expected type.

This should allow the following to compile:

#![allow(incomplete_features)]
#![feature(adt_const_params, min_generic_const_args, generic_const_parameter_types)]

fn main() {
    foo::<_, 2_i32>();
}

fn foo<T: ConstParamTy_, const N: T>() {}

@BoxyUwU
Copy link
Member

BoxyUwU commented Mar 16, 2026

because inference variables are not hashable for some reason

the thing here is that we don't want inference variables in query inputs/outputs. it's not that we can't hash them but that at a conceptual level it's not desirable to have them in queries.

an inference variable only makes sense as part of its InferCtxt so you'd have to pass that into the query too. but even if you do this a query with an inferctxt and infer vars is going to have a terrible cache hit rate (it'll basically never get a cache hit) so you really never want to have inference variables values of query arguments.

the typical thing you do for that use case is something called canonicalization (which we unfortunately dont really have many docs for other than this https://rustc-dev-guide.rust-lang.org/solve/canonicalization.html). an alternative solution for this ICE would be to make the lit_to_const query a canonical query but that doesn't seem worth it to me.

we dont actually want to handle infer vars inside the query and it would impose a perf overhead

@Human9000-bit
Copy link
Contributor Author

Ah, that makes sense. Thank you for the explanation.

@rustbot
Copy link
Collaborator

rustbot commented Mar 17, 2026

Some changes occurred in match checking

cc @Nadrieril

Some changes occurred in rustc_ty_utils::consts.rs

cc @BoxyUwU

Copy link
Member

@BoxyUwU BoxyUwU left a comment

Choose a reason for hiding this comment

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

thx yeah this looks like the right solution to me now :3

View changes since this review

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Mar 17, 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.

@Human9000-bit
Copy link
Contributor Author

Github is having a moment today

@BoxyUwU
Copy link
Member

BoxyUwU commented Mar 18, 2026

@bors r+ rollup

thx

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 18, 2026

📌 Commit 5113488 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 Mar 18, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 18, 2026
fix inference variables leaking into HIR const literal lowering logic

Inference variables could leak into further const lowering logic

It ICEs when query system tries to cache `lit_to_const()` after its execution and panics, because inference variables are not hashable for some reason

Fixes rust-lang#153524
Fixes rust-lang#153525
@Human9000-bit
Copy link
Contributor Author

thank you for the review

rust-bors bot pushed a commit that referenced this pull request Mar 18, 2026
…uwer

Rollup of 5 pull requests

Successful merges:

 - #153308 (Add hygiene annotations for tokens in `macro_rules!` bodies)
 - #153557 (fix inference variables leaking into HIR const literal lowering logic)
 - #153913 (Fix some suggestions of the `for-loops-over-fallibles` lint)
 - #153987 (mGCA: Lower const generic args to infer when needed)
 - #153992 (bootstrap: Optionally print a backtrace if a command fails)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 18, 2026
fix inference variables leaking into HIR const literal lowering logic

Inference variables could leak into further const lowering logic

It ICEs when query system tries to cache `lit_to_const()` after its execution and panics, because inference variables are not hashable for some reason

Fixes rust-lang#153524
Fixes rust-lang#153525
rust-bors bot pushed a commit that referenced this pull request Mar 19, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - #153957 (Add bootstrap step for stdarch-verify)
 - #153727 (When single impl can satisfy inference error, suggest type)
 - #153308 (Add hygiene annotations for tokens in `macro_rules!` bodies)
 - #153557 (fix inference variables leaking into HIR const literal lowering logic)
 - #153913 (Fix some suggestions of the `for-loops-over-fallibles` lint)
 - #153987 (mGCA: Lower const generic args to infer when needed)
 - #153992 (bootstrap: Optionally print a backtrace if a command fails)
 - #154036 (borrowck/type_check: remove helper left-over from unsized locals)
 - #154038 (merge `regions-outlives-nominal-type-*` tests into one file)
Zalathar added a commit to Zalathar/rust that referenced this pull request Mar 19, 2026
fix inference variables leaking into HIR const literal lowering logic

Inference variables could leak into further const lowering logic

It ICEs when query system tries to cache `lit_to_const()` after its execution and panics, because inference variables are not hashable for some reason

Fixes rust-lang#153524
Fixes rust-lang#153525
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 19, 2026
fix inference variables leaking into HIR const literal lowering logic

Inference variables could leak into further const lowering logic

It ICEs when query system tries to cache `lit_to_const()` after its execution and panics, because inference variables are not hashable for some reason

Fixes rust-lang#153524
Fixes rust-lang#153525
rust-bors bot pushed a commit that referenced this pull request Mar 19, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - #153957 (Add bootstrap step for stdarch-verify)
 - #153727 (When single impl can satisfy inference error, suggest type)
 - #153308 (Add hygiene annotations for tokens in `macro_rules!` bodies)
 - #153557 (fix inference variables leaking into HIR const literal lowering logic)
 - #153913 (Fix some suggestions of the `for-loops-over-fallibles` lint)
 - #153987 (mGCA: Lower const generic args to infer when needed)
 - #153992 (bootstrap: Optionally print a backtrace if a command fails)
 - #154036 (borrowck/type_check: remove helper left-over from unsized locals)
 - #154038 (merge `regions-outlives-nominal-type-*` tests into one file)
rust-bors bot pushed a commit that referenced this pull request Mar 19, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - #153957 (Add bootstrap step for stdarch-verify)
 - #153727 (When single impl can satisfy inference error, suggest type)
 - #153308 (Add hygiene annotations for tokens in `macro_rules!` bodies)
 - #153557 (fix inference variables leaking into HIR const literal lowering logic)
 - #153913 (Fix some suggestions of the `for-loops-over-fallibles` lint)
 - #153987 (mGCA: Lower const generic args to infer when needed)
 - #153992 (bootstrap: Optionally print a backtrace if a command fails)
 - #154036 (borrowck/type_check: remove helper left-over from unsized locals)
 - #154038 (merge `regions-outlives-nominal-type-*` tests into one file)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 19, 2026
fix inference variables leaking into HIR const literal lowering logic

Inference variables could leak into further const lowering logic

It ICEs when query system tries to cache `lit_to_const()` after its execution and panics, because inference variables are not hashable for some reason

Fixes rust-lang#153524
Fixes rust-lang#153525
rust-bors bot pushed a commit that referenced this pull request Mar 19, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - #152870 (Do not enable split debuginfo for windows-gnu)
 - #153333 (Fix bootstrap rust build failure for vxworks)
 - #153681 (ci: add runners for vanilla LLVM 22)
 - #153727 (When single impl can satisfy inference error, suggest type)
 - #153824 (Add `Wake` diagnostic item for `alloc::task::Wake`)
 - #154077 (Optimize 128-bit integer formatting)
 - #154078 (`define_callbacks` tweaks)
 - #151905 (Split the `dec2flt::RawFloat` trait for easier reuse)
 - #153170 (Add is_disconnected functions to mpsc and mpmc channels)
 - #153308 (Add hygiene annotations for tokens in `macro_rules!` bodies)
 - #153557 (fix inference variables leaking into HIR const literal lowering logic)
 - #153804 (Derive Macro Eq: link to more detailed documentation)
 - #153913 (Fix some suggestions of the `for-loops-over-fallibles` lint)
 - #153974 (Point at return type when it is the source of the type expectation)
 - #153987 (mGCA: Lower const generic args to infer when needed)
 - #154018 (simplify and remove `tests/ui/kindck` and related tests)
 - #154036 (borrowck/type_check: remove helper left-over from unsized locals)
 - #154038 (merge `regions-outlives-nominal-type-*` tests into one file)
 - #154041 (bootstrap: Allow `--bless`ing changes to editor settings files)
rust-bors bot pushed a commit that referenced this pull request Mar 19, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - #152870 (Do not enable split debuginfo for windows-gnu)
 - #153333 (Fix bootstrap rust build failure for vxworks)
 - #153681 (ci: add runners for vanilla LLVM 22)
 - #153727 (When single impl can satisfy inference error, suggest type)
 - #153824 (Add `Wake` diagnostic item for `alloc::task::Wake`)
 - #154077 (Optimize 128-bit integer formatting)
 - #154078 (`define_callbacks` tweaks)
 - #151905 (Split the `dec2flt::RawFloat` trait for easier reuse)
 - #153170 (Add is_disconnected functions to mpsc and mpmc channels)
 - #153308 (Add hygiene annotations for tokens in `macro_rules!` bodies)
 - #153557 (fix inference variables leaking into HIR const literal lowering logic)
 - #153804 (Derive Macro Eq: link to more detailed documentation)
 - #153913 (Fix some suggestions of the `for-loops-over-fallibles` lint)
 - #153974 (Point at return type when it is the source of the type expectation)
 - #153987 (mGCA: Lower const generic args to infer when needed)
 - #154018 (simplify and remove `tests/ui/kindck` and related tests)
 - #154036 (borrowck/type_check: remove helper left-over from unsized locals)
 - #154038 (merge `regions-outlives-nominal-type-*` tests into one file)
 - #154041 (bootstrap: Allow `--bless`ing changes to editor settings files)
@rust-bors rust-bors bot merged commit a3898aa into rust-lang:main Mar 19, 2026
11 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Mar 19, 2026
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.

[ICE]: type variables should not be hashed [ICE]: const variables should not be hashed

5 participants