Skip to content

ICE: expected const for .. but found Type(usize) when instantiating args #142209

Open
@matthiaskrgr

Description

@matthiaskrgr

Code

slight modification of #121429 (comment)

#![feature(generic_const_exprs)]

struct Bar<const X: usize>;
const FRAC_LHS: usize = 0;

trait Foo<const N: usize> {}

impl<const N: usize = { const { 3 } }> PartialEq<dyn Foo<FRAC_LHS>> for Bar<KABOOM> {}

pub fn main() {}

Meta

rustc --version --verbose:

rustc 1.89.0-nightly (244bbfc60 2025-06-08)
binary: rustc
commit-hash: 244bbfc60ee8593db96892468eee876240cb7ba1
commit-date: 2025-06-08
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Error output

error[E0412]: cannot find type `KABOOM` in this scope
 --> code.rs:8:77
  |
8 | ..._LHS>> for Bar<KABOOM> {}
  |                   ^^^^^^ not found in this scope
  |
help: you might be missing a type parameter
  |
8 | impl<const N: usize = { const { 3 } }, KABOOM> PartialEq<dyn Foo<FRAC_LHS>> for Bar<KABOOM> {}
  |                                      ++++++++

warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes
 --> code.rs:1:12
  |
1 | #![feature(generic_const_exprs)]
  |            ^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #76560 <https://github.com/rust-lang/rust/issues/76560> for more information
  = note: `#[warn(incomplete_features)]` on by default

error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
 --> code.rs:8:6
  |
8 | impl<const N: usize = { const { 3 } }> PartialEq<dyn Foo<FRAC_LH...
  |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0747]: unresolved item provided when a constant was expected
 --> code.rs:8:77
  |
8 | ...dyn Foo<FRAC_LHS>> for Bar<KABOOM> {}
  |                               ^^^^^^
  |
help: if this generic argument was intended as a const parameter, surround it with braces
  |
8 | impl<const N: usize = { const { 3 } }> PartialEq<dyn Foo<FRAC_LHS>> for Bar<{ KABOOM }> {}
  |                                                                             +        +
Backtrace

thread 'rustc' panicked at /rustc/244bbfc60ee8593db96892468eee876240cb7ba1/compiler/rustc_type_ir/src/binder.rs:782:9:
expected const for `N/#0` (N/#0/0) but found Type(usize) when instantiating args=[usize]
stack backtrace:
   0:     0x74da8fd115f3 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h500dbd0ac4ffd93c
   1:     0x74da90402c37 - core::fmt::write::hd10d454a472f5f50
   2:     0x74da8fd07413 - std::io::Write::write_fmt::hdd04ab57a090e22a
   3:     0x74da8fd11452 - std::sys::backtrace::BacktraceLock::print::hff44058169b053c2
   4:     0x74da8fd1503a - std::panicking::default_hook::{{closure}}::h8c268cf0d300423a
   5:     0x74da8fd14bbf - std::panicking::default_hook::hb3346906ddedadb7
   6:     0x74da8ee1adc3 - std[b5410eca7fa28284]::panicking::update_hook::<alloc[50e434591bf825ff]::boxed::Box<rustc_driver_impl[ebb49b94bdb15e90]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x74da8fd158a3 - std::panicking::rust_panic_with_hook::h1ed19da442b72600
   8:     0x74da8fd1559a - std::panicking::begin_panic_handler::{{closure}}::hfe0e28da70f97dee
   9:     0x74da8fd11ac9 - std::sys::backtrace::__rust_end_short_backtrace::h7d8451a2d642517b
  10:     0x74da8fd1526d - __rustc[12e6413ce9cb3a65]::rust_begin_unwind
  11:     0x74da8c474e00 - core::panicking::panic_fmt::h3926485ae7cc80ae
  12:     0x74da8c919ed5 - <rustc_type_ir[ab37147dfd4b7475]::binder::ArgFolder<rustc_middle[a1aef02fddbd4fbe]::ty::context::TyCtxt>>::const_param_expected
  13:     0x74da9235db6d - <rustc_type_ir[ab37147dfd4b7475]::binder::ArgFolder<rustc_middle[a1aef02fddbd4fbe]::ty::context::TyCtxt> as rustc_type_ir[ab37147dfd4b7475]::fold::TypeFolder<rustc_middle[a1aef02fddbd4fbe]::ty::context::TyCtxt>>::fold_const.llvm.2542103270621495274.cold
  14:     0x74da90434d13 - <&rustc_middle[a1aef02fddbd4fbe]::ty::list::RawList<(), rustc_middle[a1aef02fddbd4fbe]::ty::generic_args::GenericArg> as rustc_type_ir[ab37147dfd4b7475]::fold::TypeFoldable<rustc_middle[a1aef02fddbd4fbe]::ty::context::TyCtxt>>::fold_with::<rustc_type_ir[ab37147dfd4b7475]::binder::ArgFolder<rustc_middle[a1aef02fddbd4fbe]::ty::context::TyCtxt>>
  15:     0x74da9235daf6 - <rustc_type_ir[ab37147dfd4b7475]::binder::ArgFolder<rustc_middle[a1aef02fddbd4fbe]::ty::context::TyCtxt> as rustc_type_ir[ab37147dfd4b7475]::fold::TypeFolder<rustc_middle[a1aef02fddbd4fbe]::ty::context::TyCtxt>>::fold_const.llvm.2542103270621495274.cold
  16:     0x74da90439678 - <rustc_middle[a1aef02fddbd4fbe]::ty::predicate::Clause as rustc_type_ir[ab37147dfd4b7475]::fold::TypeFoldable<rustc_middle[a1aef02fddbd4fbe]::ty::context::TyCtxt>>::fold_with::<rustc_type_ir[ab37147dfd4b7475]::binder::ArgFolder<rustc_middle[a1aef02fddbd4fbe]::ty::context::TyCtxt>>
  17:     0x74da90437c9f - <rustc_middle[a1aef02fddbd4fbe]::ty::generics::GenericPredicates>::instantiate_into
  18:     0x74da90437e53 - <rustc_middle[a1aef02fddbd4fbe]::ty::generics::GenericPredicates>::instantiate_into
  19:     0x74da91120d33 - rustc_trait_selection[ddc2528950311cf6]::traits::query::type_op::ascribe_user_type::type_op_ascribe_user_type_with_span
  20:     0x74da90a1e905 - rustc_traits[ac762ab5a7074669]::type_op::type_op_ascribe_user_type
  21:     0x74da90a1ddf7 - rustc_query_impl[47d0a639797bd9bb]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[47d0a639797bd9bb]::query_impl::type_op_ascribe_user_type::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a1aef02fddbd4fbe]::query::erase::Erased<[u8; 8usize]>>
  22:     0x74da90a1c8a1 - rustc_query_system[15f975c2b7c31892]::query::plumbing::try_execute_query::<rustc_query_impl[47d0a639797bd9bb]::DynamicConfig<rustc_query_system[15f975c2b7c31892]::query::caches::DefaultCache<rustc_type_ir[ab37147dfd4b7475]::canonical::CanonicalQueryInput<rustc_middle[a1aef02fddbd4fbe]::ty::context::TyCtxt, rustc_middle[a1aef02fddbd4fbe]::ty::ParamEnvAnd<rustc_middle[a1aef02fddbd4fbe]::traits::query::type_op::AscribeUserType>>, rustc_middle[a1aef02fddbd4fbe]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[47d0a639797bd9bb]::plumbing::QueryCtxt, false>
  23:     0x74da90a1c51b - rustc_query_impl[47d0a639797bd9bb]::query_impl::type_op_ascribe_user_type::get_query_non_incr::__rust_end_short_backtrace
  24:     0x74da90d8d7d9 - <rustc_borrowck[a0f5614314d664e3]::type_check::TypeChecker>::ascribe_user_type
  25:     0x74da90d323c0 - <rustc_borrowck[a0f5614314d664e3]::type_check::TypeChecker as rustc_middle[a1aef02fddbd4fbe]::mir::visit::Visitor>::visit_const_operand
  26:     0x74da919fd4da - <rustc_borrowck[a0f5614314d664e3]::type_check::TypeChecker as rustc_middle[a1aef02fddbd4fbe]::mir::visit::Visitor>::visit_body
  27:     0x74da8d8a8db2 - rustc_borrowck[a0f5614314d664e3]::type_check::type_check
  28:     0x74da9170d83b - rustc_borrowck[a0f5614314d664e3]::do_mir_borrowck
  29:     0x74da9062944a - rustc_borrowck[a0f5614314d664e3]::mir_borrowck
  30:     0x74da90629159 - rustc_query_impl[47d0a639797bd9bb]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[47d0a639797bd9bb]::query_impl::mir_borrowck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a1aef02fddbd4fbe]::query::erase::Erased<[u8; 8usize]>>
  31:     0x74da90627f26 - rustc_query_system[15f975c2b7c31892]::query::plumbing::try_execute_query::<rustc_query_impl[47d0a639797bd9bb]::DynamicConfig<rustc_data_structures[327f15f6a9f3270f]::vec_cache::VecCache<rustc_span[3d073c07287ff8ce]::def_id::LocalDefId, rustc_middle[a1aef02fddbd4fbe]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[15f975c2b7c31892]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[47d0a639797bd9bb]::plumbing::QueryCtxt, false>
  32:     0x74da90627809 - rustc_query_impl[47d0a639797bd9bb]::query_impl::mir_borrowck::get_query_non_incr::__rust_end_short_backtrace
  33:     0x74da9062698e - <rustc_middle[a1aef02fddbd4fbe]::ty::context::TyCtxt>::par_hir_body_owners::<rustc_interface[6a7d28815c163ddb]::passes::run_required_analyses::{closure#1}::{closure#0}>::{closure#0}
  34:     0x74da9062436a - rustc_interface[6a7d28815c163ddb]::passes::analysis
  35:     0x74da90623833 - rustc_query_impl[47d0a639797bd9bb]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[47d0a639797bd9bb]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a1aef02fddbd4fbe]::query::erase::Erased<[u8; 0usize]>>
  36:     0x74da9151db4e - rustc_query_system[15f975c2b7c31892]::query::plumbing::try_execute_query::<rustc_query_impl[47d0a639797bd9bb]::DynamicConfig<rustc_query_system[15f975c2b7c31892]::query::caches::SingleCache<rustc_middle[a1aef02fddbd4fbe]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[47d0a639797bd9bb]::plumbing::QueryCtxt, false>
  37:     0x74da9151d736 - rustc_query_impl[47d0a639797bd9bb]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  38:     0x74da9178967f - rustc_interface[6a7d28815c163ddb]::passes::create_and_enter_global_ctxt::<core[74dc601333a2912]::option::Option<rustc_interface[6a7d28815c163ddb]::queries::Linker>, rustc_driver_impl[ebb49b94bdb15e90]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  39:     0x74da91779470 - rustc_interface[6a7d28815c163ddb]::interface::run_compiler::<(), rustc_driver_impl[ebb49b94bdb15e90]::run_compiler::{closure#0}>::{closure#1}
  40:     0x74da916c88be - std[b5410eca7fa28284]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[6a7d28815c163ddb]::util::run_in_thread_with_globals<rustc_interface[6a7d28815c163ddb]::util::run_in_thread_pool_with_globals<rustc_interface[6a7d28815c163ddb]::interface::run_compiler<(), rustc_driver_impl[ebb49b94bdb15e90]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  41:     0x74da916c85a4 - <<std[b5410eca7fa28284]::thread::Builder>::spawn_unchecked_<rustc_interface[6a7d28815c163ddb]::util::run_in_thread_with_globals<rustc_interface[6a7d28815c163ddb]::util::run_in_thread_pool_with_globals<rustc_interface[6a7d28815c163ddb]::interface::run_compiler<(), rustc_driver_impl[ebb49b94bdb15e90]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[74dc601333a2912]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  42:     0x74da916c6bbd - std::sys::pal::unix::thread::Thread::new::thread_start::he6125d035aa27702
  43:     0x74da8b0a57eb - <unknown>
  44:     0x74da8b12918c - <unknown>
  45:                0x0 - <unknown>

error: the compiler unexpectedly panicked. this is a bug.

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/matthias/vcs/github/CRED/rustc-ice-2025-06-08T18_44_46-1757273.txt` to your bug report

query stack during panic:
#0 [type_op_ascribe_user_type] evaluating `type_op_ascribe_user_type` `AscribeUserType { mir_ty: usize, user_ty: UserType { kind: TypeOf(DefId(0:12 ~ code[809a]::{impl#0}::{constant#0}::{constant#0}), UserArgs { args: [usize], user_self_ty: None }), bounds: [] } }`
#1 [mir_borrowck] borrow-checking `<impl at code.rs:8:1: 8:84>::{constant#0}`
#2 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 3 previous errors; 1 warning emitted

Some errors have detailed explanations: E0412, E0747.
For more information about an error, try `rustc --explain E0412`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-generic_const_exprs`#![feature(generic_const_exprs)]`I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions