Skip to content

ICE: "can't type-check body of DefId" when using #[type_const] with nested generics. #151477

@trait-Cong

Description

@trait-Cong

I tried this code:

#![feature(min_generic_const_args)]
#![expect(incomplete_features)]

trait Hidden {const VAL: usize;}
trait Mirror {const IMAGE: usize;}

impl<T: Hidden + ?Sized> Mirror for T {
    const IMAGE: usize = T::VAL;
}
struct Wrapper<T: ?Sized>(T);

impl<T: ?Sized> Hidden for Wrapper<T> {
    const VAL: usize = {
        #[type_const]
        const A: usize = <Wrapper<T> as Mirror>::IMAGE;
        A
    };
}
trait Sanity {
    const IS_OK: bool;
}

impl<T: Hidden + ?Sized> Sanity for T {
    const IS_OK: bool = T::VAL == 0;
}

trait Service {
    fn start(&self);
}

impl<T> Service for Wrapper<T> {
    fn start(&self) where Self: Sanity, 
    {}
}
fn main() {
    let service = Wrapper(());
    service.start();
}

I expected to see this happen: The compiler should emit error E0401

Instead, this happened: panics with: error: internal compiler error: .../compiler/rustc_hir_typeck/src/lib.rs:124:9: can't type-check body of DefId(...)

Release channel Result
current Stable error
current Nightly (default solver) ICE
Nightly + -Znext-solver=globally ICE

Meta

rustc --version --verbose:

rustc 1.94.0-nightly (f57eac1bf 2026-01-10)
binary: rustc
commit-hash: f57eac1bf98cb5d578e3364b64365ec398c137df
commit-date: 2026-01-10
host: x86_64-unknown-linux-gnu
release: 1.94.0-nightly
LLVM version: 21.1.8
Backtrace


error: expressions must be enclosed in braces to be used as const generic arguments
  --> Traitor/trait-fuzzer/results/fate/case_iter_10010_var_21/after.rs:15:26
   |
15 |         const A: usize = <Wrapper<T> as Mirror>::IMAGE;
   |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: enclose the `const` expression in braces
   |
15 |         const A: usize = { <Wrapper<T> as Mirror>::IMAGE };
   |                          +                               +

error[E0401]: can't use generic parameters from outer item
  --> Traitor/trait-fuzzer/results/fate/case_iter_10010_var_21/after.rs:15:35
   |
12 | impl<T: ?Sized> Hidden for Wrapper<T> {
   |      - type parameter from outer item
...
15 |         const A: usize = <Wrapper<T> as Mirror>::IMAGE;
   |               -                   ^ use of generic parameter from outer item
   |               |
   |               generic parameter used in this inner constant item
   |
   = note: nested items are independent from their parent item for everything except for privacy and name resolution
   = note: a `const` is a separate item from the item that contains it

error: use of `const` in the type system without `#[type_const]`
  --> Traitor/trait-fuzzer/results/fate/case_iter_10010_var_21/after.rs:15:26
   |
15 |         const A: usize = <Wrapper<T> as Mirror>::IMAGE;
   |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: the declaration must be marked with `#[type_const]`

error: internal compiler error: /rustc-dev/f57eac1bf98cb5d578e3364b64365ec398c137df/compiler/rustc_hir_typeck/src/lib.rs:124:9: can't type-check body of DefId(0:17 ~ after[9715]::{impl#1}::VAL::A)
  --> Traitor/trait-fuzzer/results/fate/case_iter_10010_var_21/after.rs:15:9
   |
15 |         const A: usize = <Wrapper<T> as Mirror>::IMAGE;
   |         ^^^^^^^^^^^^^^


thread 'rustc' (1353527) panicked at /rustc-dev/f57eac1bf98cb5d578e3364b64365ec398c137df/compiler/rustc_hir_typeck/src/lib.rs:124:9:
Box<dyn Any>
stack backtrace:
   0:     0x7fbdee0404f3 - <<std[14974c2a6d31bd09]::sys::backtrace::BacktraceLock>::print::DisplayBacktrace as core[75fca27d79ab979d]::fmt::Display>::fmt
   1:     0x7fbdee60fac8 - core[75fca27d79ab979d]::fmt::write
   2:     0x7fbdee056776 - <std[14974c2a6d31bd09]::sys::stdio::unix::Stderr as std[14974c2a6d31bd09]::io::Write>::write_fmt
   3:     0x7fbdee0160e8 - std[14974c2a6d31bd09]::panicking::default_hook::{closure#0}
   4:     0x7fbdee033d83 - std[14974c2a6d31bd09]::panicking::default_hook
   5:     0x7fbded015eaa - std[14974c2a6d31bd09]::panicking::update_hook::<alloc[97bcaa3c9f42b50c]::boxed::Box<rustc_driver_impl[8626402415690959]::install_ice_hook::{closure#1}>>::{closure#0}
   6:     0x7fbdee034062 - std[14974c2a6d31bd09]::panicking::panic_with_hook
   7:     0x7fbded053251 - std[14974c2a6d31bd09]::panicking::begin_panic::<rustc_errors[cacf81fbc0306139]::ExplicitBug>::{closure#0}
   8:     0x7fbded043506 - std[14974c2a6d31bd09]::sys::backtrace::__rust_end_short_backtrace::<std[14974c2a6d31bd09]::panicking::begin_panic<rustc_errors[cacf81fbc0306139]::ExplicitBug>::{closure#0}, !>
   9:     0x7fbded040b49 - std[14974c2a6d31bd09]::panicking::begin_panic::<rustc_errors[cacf81fbc0306139]::ExplicitBug>
  10:     0x7fbded070f91 - <rustc_errors[cacf81fbc0306139]::diagnostic::BugAbort as rustc_errors[cacf81fbc0306139]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  11:     0x7fbded5ff73c - <rustc_errors[cacf81fbc0306139]::DiagCtxtHandle>::span_bug::<rustc_span[ea6610cf11a1023c]::span_encoding::Span, alloc[97bcaa3c9f42b50c]::string::String>
  12:     0x7fbded622266 - rustc_middle[42069f076602ef26]::util::bug::opt_span_bug_fmt::<rustc_span[ea6610cf11a1023c]::span_encoding::Span>::{closure#0}
  13:     0x7fbded622412 - rustc_middle[42069f076602ef26]::ty::context::tls::with_opt::<rustc_middle[42069f076602ef26]::util::bug::opt_span_bug_fmt<rustc_span[ea6610cf11a1023c]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  14:     0x7fbded61352b - rustc_middle[42069f076602ef26]::ty::context::tls::with_context_opt::<rustc_middle[42069f076602ef26]::ty::context::tls::with_opt<rustc_middle[42069f076602ef26]::util::bug::opt_span_bug_fmt<rustc_span[ea6610cf11a1023c]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  15:     0x7fbdeb6917d8 - rustc_middle[42069f076602ef26]::util::bug::span_bug_fmt::<rustc_span[ea6610cf11a1023c]::span_encoding::Span>
  16:     0x7fbdebb6942b - rustc_hir_typeck[f46337d5327ab94d]::typeck_with_inspect::{closure#0}::{closure#0}
  17:     0x7fbdef369738 - rustc_hir_typeck[f46337d5327ab94d]::typeck_with_inspect::{closure#0}
  18:     0x7fbdef363ef4 - rustc_query_impl[7762d22649c04fd8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[7762d22649c04fd8]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 8usize]>>
  19:     0x7fbdee83ddf7 - rustc_query_system[4067c90f9d31d9ad]::query::plumbing::try_execute_query::<rustc_query_impl[7762d22649c04fd8]::DynamicConfig<rustc_data_structures[5b2e5bcce01f3ea3]::vec_cache::VecCache<rustc_span[ea6610cf11a1023c]::def_id::LocalDefId, rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[4067c90f9d31d9ad]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[7762d22649c04fd8]::plumbing::QueryCtxt, false>
  20:     0x7fbdee83d6c9 - rustc_query_impl[7762d22649c04fd8]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  21:     0x7fbdeea81e19 - rustc_mir_build[c2fac1714b0df487]::thir::pattern::check_match::check_match
  22:     0x7fbdeea811db - rustc_query_impl[7762d22649c04fd8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[7762d22649c04fd8]::query_impl::check_match::dynamic_query::{closure#2}::{closure#0}, rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 1usize]>>
  23:     0x7fbdeebe40ed - rustc_query_system[4067c90f9d31d9ad]::query::plumbing::try_execute_query::<rustc_query_impl[7762d22649c04fd8]::DynamicConfig<rustc_data_structures[5b2e5bcce01f3ea3]::vec_cache::VecCache<rustc_span[ea6610cf11a1023c]::def_id::LocalDefId, rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 1usize]>, rustc_query_system[4067c90f9d31d9ad]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[7762d22649c04fd8]::plumbing::QueryCtxt, false>
  24:     0x7fbdeebe3dbd - rustc_query_impl[7762d22649c04fd8]::query_impl::check_match::get_query_non_incr::__rust_end_short_backtrace
  25:     0x7fbdef107ee5 - rustc_mir_build[c2fac1714b0df487]::builder::build_mir
  26:     0x7fbdee60a5e9 - rustc_mir_transform[959ce5525355527d]::mir_built
  27:     0x7fbdee60a5b3 - rustc_query_impl[7762d22649c04fd8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[7762d22649c04fd8]::query_impl::mir_built::dynamic_query::{closure#2}::{closure#0}, rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 8usize]>>
  28:     0x7fbdee83ddf7 - rustc_query_system[4067c90f9d31d9ad]::query::plumbing::try_execute_query::<rustc_query_impl[7762d22649c04fd8]::DynamicConfig<rustc_data_structures[5b2e5bcce01f3ea3]::vec_cache::VecCache<rustc_span[ea6610cf11a1023c]::def_id::LocalDefId, rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[4067c90f9d31d9ad]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[7762d22649c04fd8]::plumbing::QueryCtxt, false>
  29:     0x7fbdee83d849 - rustc_query_impl[7762d22649c04fd8]::query_impl::mir_built::get_query_non_incr::__rust_end_short_backtrace
  30:     0x7fbdee79d481 - rustc_query_impl[7762d22649c04fd8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[7762d22649c04fd8]::query_impl::trivial_const::dynamic_query::{closure#2}::{closure#0}, rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 32usize]>>
  31:     0x7fbdee79ab2c - rustc_query_system[4067c90f9d31d9ad]::query::plumbing::try_execute_query::<rustc_query_impl[7762d22649c04fd8]::DynamicConfig<rustc_query_system[4067c90f9d31d9ad]::query::caches::DefIdCache<rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 32usize]>>, false, false, false>, rustc_query_impl[7762d22649c04fd8]::plumbing::QueryCtxt, false>
  32:     0x7fbdee79a3a0 - rustc_query_impl[7762d22649c04fd8]::query_impl::trivial_const::get_query_non_incr::__rust_end_short_backtrace
  33:     0x7fbdee60acc3 - rustc_mir_transform[959ce5525355527d]::mir_built
  34:     0x7fbdee60a5b3 - rustc_query_impl[7762d22649c04fd8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[7762d22649c04fd8]::query_impl::mir_built::dynamic_query::{closure#2}::{closure#0}, rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 8usize]>>
  35:     0x7fbdee83ddf7 - rustc_query_system[4067c90f9d31d9ad]::query::plumbing::try_execute_query::<rustc_query_impl[7762d22649c04fd8]::DynamicConfig<rustc_data_structures[5b2e5bcce01f3ea3]::vec_cache::VecCache<rustc_span[ea6610cf11a1023c]::def_id::LocalDefId, rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[4067c90f9d31d9ad]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[7762d22649c04fd8]::plumbing::QueryCtxt, false>
  36:     0x7fbdee83d849 - rustc_query_impl[7762d22649c04fd8]::query_impl::mir_built::get_query_non_incr::__rust_end_short_backtrace
  37:     0x7fbdef46392d - rustc_mir_build[c2fac1714b0df487]::check_unsafety::check_unsafety
  38:     0x7fbdef4636ab - rustc_query_impl[7762d22649c04fd8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[7762d22649c04fd8]::query_impl::check_unsafety::dynamic_query::{closure#2}::{closure#0}, rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 0usize]>>
  39:     0x7fbdee83bdbf - rustc_query_system[4067c90f9d31d9ad]::query::plumbing::try_execute_query::<rustc_query_impl[7762d22649c04fd8]::DynamicConfig<rustc_data_structures[5b2e5bcce01f3ea3]::vec_cache::VecCache<rustc_span[ea6610cf11a1023c]::def_id::LocalDefId, rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 0usize]>, rustc_query_system[4067c90f9d31d9ad]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[7762d22649c04fd8]::plumbing::QueryCtxt, false>
  40:     0x7fbdee83bb3d - rustc_query_impl[7762d22649c04fd8]::query_impl::check_unsafety::get_query_non_incr::__rust_end_short_backtrace
  41:     0x7fbdee83ac66 - <rustc_middle[42069f076602ef26]::ty::context::TyCtxt>::par_hir_body_owners::<rustc_interface[90afb413dadbe4f1]::passes::run_required_analyses::{closure#1}::{closure#0}>::{closure#0}
  42:     0x7fbdee83878a - rustc_interface[90afb413dadbe4f1]::passes::analysis
  43:     0x7fbdee837ba7 - rustc_query_impl[7762d22649c04fd8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[7762d22649c04fd8]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 0usize]>>
  44:     0x7fbdef75cbe6 - rustc_query_system[4067c90f9d31d9ad]::query::plumbing::try_execute_query::<rustc_query_impl[7762d22649c04fd8]::DynamicConfig<rustc_query_system[4067c90f9d31d9ad]::query::caches::SingleCache<rustc_middle[42069f076602ef26]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[7762d22649c04fd8]::plumbing::QueryCtxt, false>
  45:     0x7fbdef75c948 - rustc_query_impl[7762d22649c04fd8]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  46:     0x7fbdef924754 - <rustc_interface[90afb413dadbe4f1]::passes::create_and_enter_global_ctxt<core[75fca27d79ab979d]::option::Option<rustc_interface[90afb413dadbe4f1]::queries::Linker>, rustc_driver_impl[8626402415690959]::run_compiler::{closure#0}::{closure#2}>::{closure#2} as core[75fca27d79ab979d]::ops::function::FnOnce<(&rustc_session[7c34b462142e1273]::session::Session, rustc_middle[42069f076602ef26]::ty::context::CurrentGcx, alloc[97bcaa3c9f42b50c]::sync::Arc<rustc_data_structures[5b2e5bcce01f3ea3]::jobserver::Proxy>, &std[14974c2a6d31bd09]::sync::once_lock::OnceLock<rustc_middle[42069f076602ef26]::ty::context::GlobalCtxt>, &rustc_data_structures[5b2e5bcce01f3ea3]::sync::worker_local::WorkerLocal<rustc_middle[42069f076602ef26]::arena::Arena>, &rustc_data_structures[5b2e5bcce01f3ea3]::sync::worker_local::WorkerLocal<rustc_hir[a6c28dd0effa383c]::Arena>, rustc_driver_impl[8626402415690959]::run_compiler::{closure#0}::{closure#2})>>::call_once::{shim:vtable#0}
  47:     0x7fbdef81cf98 - rustc_interface[90afb413dadbe4f1]::interface::run_compiler::<(), rustc_driver_impl[8626402415690959]::run_compiler::{closure#0}>::{closure#1}
  48:     0x7fbdef665f4e - std[14974c2a6d31bd09]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[90afb413dadbe4f1]::util::run_in_thread_with_globals<rustc_interface[90afb413dadbe4f1]::util::run_in_thread_pool_with_globals<rustc_interface[90afb413dadbe4f1]::interface::run_compiler<(), rustc_driver_impl[8626402415690959]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  49:     0x7fbdef665d20 - <std[14974c2a6d31bd09]::thread::lifecycle::spawn_unchecked<rustc_interface[90afb413dadbe4f1]::util::run_in_thread_with_globals<rustc_interface[90afb413dadbe4f1]::util::run_in_thread_pool_with_globals<rustc_interface[90afb413dadbe4f1]::interface::run_compiler<(), rustc_driver_impl[8626402415690959]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[75fca27d79ab979d]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  50:     0x7fbdef668438 - <std[14974c2a6d31bd09]::sys::thread::unix::Thread>::new::thread_start
  51:     0x7fbde9094ac3 - start_thread
                               at ./nptl/pthread_create.c:442:8
  52:     0x7fbde91268c0 - __GI___clone3
                               at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:81:0
  53:                0x0 - <unknown>

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please make sure that you have updated to the latest nightly

note: please attach the file at `/home/laix/Study/rustc-ice-2026-01-22T03_17_07-1353406.txt` to your bug report

note: rustc 1.94.0-nightly (f57eac1bf 2026-01-10) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z next-solver=globally

query stack during panic:
#0 [typeck] type-checking `<impl at Traitor/trait-fuzzer/results/fate/case_iter_10010_var_21/after.rs:12:1: 12:38>::VAL::A`
#1 [check_match] match-checking `<impl at Traitor/trait-fuzzer/results/fate/case_iter_10010_var_21/after.rs:12:1: 12:38>::VAL::A`
... and 5 other queries... use `env RUST_BACKTRACE=1` to see the full query stack
error: aborting due to 4 previous errors

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-min_generic_const_args`#![feature(min_generic_const_args)]`I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions