Skip to content

hir_ty_lowering: fix anon const type recovery#158874

Open
Dnreikronos wants to merge 2 commits into
rust-lang:mainfrom
Dnreikronos:hir_ty_lowering/anon_const_type_of_race
Open

hir_ty_lowering: fix anon const type recovery#158874
Dnreikronos wants to merge 2 commits into
rust-lang:mainfrom
Dnreikronos:hir_ty_lowering/anon_const_type_of_race

Conversation

@Dnreikronos

@Dnreikronos Dnreikronos commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

fixes #158818

with -zthreads=0, an anon const in a const generic arg could get its type_of queried before generic arg lowering had fed the expected type. that left an error type in the query cache, then the later feed tried to write u32 over it.

imo the safest fix is to recover the expected type only for the shapes we can prove here: normal resolved path args and inherent type-relative fn paths like s::f::<...>. malformed generic arg lists still bail out to the old path, which matters because idk that we want type_of doing diagnostic work here.

added ui coverage for the original repro and the adjacent inherent path case. ltm if you want this broader, but i kept it tight on purpose since this area is easy to overreach in.

@rustbot

rustbot commented Jul 6, 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 Jul 6, 2026
@rustbot

rustbot commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

r? @davidtwco

rustbot has assigned @davidtwco.
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 75 candidates
  • Random selection from 20 candidates

@rust-log-analyzer

This comment has been minimized.

@Dnreikronos Dnreikronos force-pushed the hir_ty_lowering/anon_const_type_of_race branch from 0ce9ac1 to 2b086ad Compare July 6, 2026 18:18
Comment thread compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs Outdated
Comment thread compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@Dnreikronos Dnreikronos force-pushed the hir_ty_lowering/anon_const_type_of_race branch from 2b086ad to 7eb6962 Compare July 6, 2026 23:02
@Dnreikronos

Copy link
Copy Markdown
Contributor Author

@fmease i pushed a much narrower version.

i dropped the type-relative recovery path completely, so there is no separate iat/inherent assoc selection anymore. i also removed the generic-arg lowering helper from generics.rs. the remaining type_of code only accepts the exact simple shape from the repro: one resolved segment, one explicit const arg, one own const param, no parent generics, no self, no inferred args, no constraints, no late-bound regions.

imo this is aligned with the review feedback while still fixing the bug. there is still a tiny guard in type_of to make sure the anon const maps to that one const param, but idk how to remove even that without either reusing an existing lowering hook or changing the shape of the fix more deeply. ltm if you want zero matching in type_of. in that case i think the next step is finding a proper existing hook, or maybe backing out of this query-side recovery approach.

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
---- [ui] tests/ui/typeck/issue-84831.rs stdout ----

error: Error: expected failure status (Some(1)) but received status Some(101).
status: exit status: 101
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/typeck/issue-84831.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/typeck/issue-84831" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error: expected one of `,` or `>`, found keyword `as`
##[error]  --> /checkout/tests/ui/typeck/issue-84831.rs:5:13
   |
LL |     std::<_ as _>; //~ ERROR expected value
   |             ^^ expected one of `,` or `>`
   |
help: expressions must be enclosed in braces to be used as const generic arguments
   |
LL |     std::<{ _ as _ }>; //~ ERROR expected value
   |           +        +

error[E0423]: expected value, found crate `std`
##[error]  --> /checkout/tests/ui/typeck/issue-84831.rs:2:5
   |
LL |     std::<0>; //~ ERROR expected value
   |     ^^^^^^^^ not a value

error[E0423]: expected value, found crate `std`
##[error]  --> /checkout/tests/ui/typeck/issue-84831.rs:5:5
   |
LL |     std::<_ as _>; //~ ERROR expected value
   |     ^^^^^^^^^^^^^ not a value


thread 'rustc' (197845) panicked at compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs:222:1:
DefId(1:0 ~ std[83d7]) does not have a "generics_of"
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: rustc_metadata::rmeta::decoder::cstore_impl::provide_extern::generics_of::{closure#1}
   3: rustc_metadata::rmeta::decoder::cstore_impl::provide_extern::generics_of
      [... omitted 2 frames ...]
   4: rustc_hir_analysis::collect::type_of::type_of
      [... omitted 2 frames ...]
   5: rustc_hir_typeck::typeck_with_inspect
      [... omitted 2 frames ...]
   6: <rustc_middle::ty::context::TyCtxt>::typeck::<rustc_span::def_id::LocalDefId>
      [... omitted 2 frames ...]
   7: rustc_hir_analysis::check_unused::check_unused_traits
      [... omitted 2 frames ...]
   8: rustc_hir_analysis::check_crate
   9: rustc_interface::passes::analysis
      [... omitted 2 frames ...]
  10: std::panicking::catch_unwind::<core::option::Option<rustc_interface::queries::Linker>, core::panic::unwind_safe::AssertUnwindSafe<rustc_interface::passes::create_and_enter_global_ctxt<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}>>
  11: <std::thread::local::LocalKey<core::cell::Cell<*const ()>>>::with::<rustc_middle::ty::context::tls::enter_context<<rustc_middle::ty::context::GlobalCtxt>::enter<rustc_interface::passes::create_and_enter_global_ctxt<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}, core::option::Option<rustc_interface::queries::Linker>>::{closure#1}, core::option::Option<rustc_interface::queries::Linker>>::{closure#0}, core::option::Option<rustc_interface::queries::Linker>>
  12: <rustc_middle::ty::context::TyCtxt>::create_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_interface::passes::create_and_enter_global_ctxt<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}>
  13: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>
  14: std::panicking::catch_unwind::<(), core::panic::unwind_safe::AssertUnwindSafe<rustc_interface::interface::run_compiler<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}::{closure#0}>>
  15: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
  16: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}::{closure#0}, ()>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
---
note: please make sure that you have updated to the latest nightly

note: rustc 1.99.0-nightly (8bcbf758c 2026-07-06) running on aarch64-unknown-linux-gnu

note: compiler flags: -Z threads=1 -Z simulate-remapped-rust-src-base=/rustc/FAKE_PREFIX -Z translate-remapped-path-to-local-path=no -Z ignore-directory-in-diagnostics-source-blocks=/cargo -Z ignore-directory-in-diagnostics-source-blocks=/checkout/vendor -C codegen-units=1 -Z ui-testing -Z deduplicate-diagnostics=no -Z write-long-types-to-disk=no -C strip=debuginfo -C prefer-dynamic -C rpath -C debuginfo=0

query stack during panic:
#0 [generics_of] computing generics of `std`
#1 [type_of] computing type of `f::{constant#0}`
#2 [typeck_root] type-checking `f::{constant#0}`
#3 [used_trait_imports] finding used_trait_imports `f::{constant#0}`
#4 [check_unused_traits] checking unused trait imports in crate
#5 [analysis] running analysis passes on crate `issue_84831`
end of query stack
error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0423`.

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: Computed query value for type_of is inconsistent with fed value / Trying to feed an already recorded value for query type_of

5 participants