Skip to content

ICE : collection encountered polymorphic constant: Unevaluated #150854

Description

@trait-Cong

This issue was discovered by a fuzzer that generates code by adding trait bounds. Specifically, adding a recursive trait bound (where T: SpecializedTrait) to a specialized implementation seems to confuse the resolution of associated constants.

I tried this code:

#![feature(specialization)]
trait Marker {}
trait SpecializedTrait {
    const CONST_BOOL: bool;
    const CONST_STR: &'static str;
    fn method() -> &'static str;
}
impl<T> SpecializedTrait for T {
    default const CONST_BOOL: bool = false;
    default const CONST_STR: &'static str = "in default impl";
    #[inline(always)]
    default fn method() -> &'static str {
        "in default impl"
    }
}
impl<T: Marker> SpecializedTrait for T
where
    T: SpecializedTrait,
{
    const CONST_BOOL: bool = true;
    const CONST_STR: &'static str = "in specialized impl";
    fn method() -> &'static str {
        "in specialized impl"
    }
}
fn const_bool<T>() -> &'static str {
    if <T as SpecializedTrait>::CONST_BOOL {
        "in specialized impl"
    } else {
        "in default impl"
    }
}
fn const_str<T>() -> &'static str {
    <T as SpecializedTrait>::CONST_STR
}
fn run_method<T>() -> &'static str {
    <T as SpecializedTrait>::method()
}
struct TypeA;
impl Marker for TypeA {}
struct TypeB;
#[inline(never)]
fn exit_if_not_eq(left: &str, right: &str) {
    if left != right {
        std::process::exit(1);
    }
}
pub fn main() {
    exit_if_not_eq("in specialized impl", const_bool::<TypeA>());
}

I expected to see this happen:
The code may need to be correctly rejected.

Instead, this happened:
ICE

Meta

rustc --version --verbose:

rustc 1.94.0-nightly (e29fcf45e 2026-01-04)
binary: rustc
commit-hash: e29fcf45e4ae686d77b490bf07320f0d3a2cf35f
commit-date: 2026-01-04
host: x86_64-unknown-linux-gnu
release: 1.94.0-nightly
LLVM version: 21.1.8
Backtrace

thread 'rustc' panicked at /rustc-dev/e29fcf45e4ae686d77b490bf07320f0d3a2cf35f/compiler/rustc_monomorphize/src/collector.rs:718:50:
Box<dyn Any>
stack backtrace:
   0:     0x7ff8ea419c0b - <std[eaa43e369792efb9]::backtrace::Backtrace>::create
   1:     0x7ff8ea419b55 - <std[eaa43e369792efb9]::backtrace::Backtrace>::force_capture
   2:     0x7ff8e94115b7 - std[eaa43e369792efb9]::panicking::update_hook::<alloc[83e7320d2ee5a6b1]::boxed::Box<rustc_driver_impl[5af842e777d449e6]::install_ice_hook::{closure#1}>>::{closure#0}
   3:     0x7ff8ea42c4b2 - std[eaa43e369792efb9]::panicking::panic_with_hook
   4:     0x7ff8e944de81 - std[eaa43e369792efb9]::panicking::begin_panic::<rustc_errors[3cfa44cd230a06eb]::ExplicitBug>::{closure#0}
   5:     0x7ff8e943e4e6 - std[eaa43e369792efb9]::sys::backtrace::__rust_end_short_backtrace::<std[eaa43e369792efb9]::panicking::begin_panic<rustc_errors[3cfa44cd230a06eb]::ExplicitBug>::{closure#0}, !>
   6:     0x7ff8e943e11c - std[eaa43e369792efb9]::panicking::begin_panic::<rustc_errors[3cfa44cd230a06eb]::ExplicitBug>
   7:     0x7ff8e946bd21 - <rustc_errors[3cfa44cd230a06eb]::diagnostic::BugAbort as rustc_errors[3cfa44cd230a06eb]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   8:     0x7ff8e99f729c - <rustc_errors[3cfa44cd230a06eb]::DiagCtxtHandle>::span_bug::<rustc_span[14443e5baae9ab13]::span_encoding::Span, alloc[83e7320d2ee5a6b1]::string::String>
   9:     0x7ff8e9a19e86 - rustc_middle[829e5443847148d]::util::bug::opt_span_bug_fmt::<rustc_span[14443e5baae9ab13]::span_encoding::Span>::{closure#0}
  10:     0x7ff8e9a1a032 - rustc_middle[829e5443847148d]::ty::context::tls::with_opt::<rustc_middle[829e5443847148d]::util::bug::opt_span_bug_fmt<rustc_span[14443e5baae9ab13]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  11:     0x7ff8e9a0b65b - rustc_middle[829e5443847148d]::ty::context::tls::with_context_opt::<rustc_middle[829e5443847148d]::ty::context::tls::with_opt<rustc_middle[829e5443847148d]::util::bug::opt_span_bug_fmt<rustc_span[14443e5baae9ab13]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  12:     0x7ff8e7d29388 - rustc_middle[829e5443847148d]::util::bug::span_bug_fmt::<rustc_span[14443e5baae9ab13]::span_encoding::Span>
  13:     0x7ff8eb236895 - <rustc_monomorphize[bf80cbd22413be96]::collector::MirUsedCollector as rustc_middle[829e5443847148d]::mir::visit::Visitor>::visit_operand
  14:     0x7ff8eb23ed02 - <rustc_monomorphize[bf80cbd22413be96]::collector::MirUsedCollector as rustc_middle[829e5443847148d]::mir::visit::Visitor>::visit_terminator
  15:     0x7ff8eb237d89 - rustc_monomorphize[bf80cbd22413be96]::collector::items_of_instance
  16:     0x7ff8eb237782 - rustc_query_impl[1b717c0605046cd0]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[1b717c0605046cd0]::query_impl::items_of_instance::dynamic_query::{closure#2}::{closure#0}, rustc_middle[829e5443847148d]::query::erase::Erased<[u8; 32usize]>>
  17:     0x7ff8eb6b7894 - rustc_query_system[5c35911b3dd767e9]::query::plumbing::try_execute_query::<rustc_query_impl[1b717c0605046cd0]::DynamicConfig<rustc_query_system[5c35911b3dd767e9]::query::caches::DefaultCache<(rustc_middle[829e5443847148d]::ty::instance::Instance, rustc_middle[829e5443847148d]::mir::mono::CollectionMode), rustc_middle[829e5443847148d]::query::erase::Erased<[u8; 32usize]>>, false, false, false>, rustc_query_impl[1b717c0605046cd0]::plumbing::QueryCtxt, false>
  18:     0x7ff8eb6b74b3 - rustc_query_impl[1b717c0605046cd0]::query_impl::items_of_instance::get_query_non_incr::__rust_end_short_backtrace
  19:     0x7ff8ebef07ae - rustc_monomorphize[bf80cbd22413be96]::collector::collect_items_rec
  20:     0x7ff8ebef4981 - rustc_monomorphize[bf80cbd22413be96]::collector::collect_items_rec
  21:     0x7ff8ead5d6a1 - rustc_monomorphize[bf80cbd22413be96]::collector::collect_crate_mono_items::{closure#1}::{closure#0}
  22:     0x7ff8eafc83ba - rustc_monomorphize[bf80cbd22413be96]::partitioning::collect_and_partition_mono_items
  23:     0x7ff8ebdedda4 - rustc_query_impl[1b717c0605046cd0]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[1b717c0605046cd0]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle[829e5443847148d]::query::erase::Erased<[u8; 24usize]>>
  24:     0x7ff8ebdedd89 - <rustc_query_impl[1b717c0605046cd0]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2} as core[4141bff0bf9ca9dd]::ops::function::FnOnce<(rustc_middle[829e5443847148d]::ty::context::TyCtxt, ())>>::call_once
  25:     0x7ff8ebded8de - rustc_query_system[5c35911b3dd767e9]::query::plumbing::try_execute_query::<rustc_query_impl[1b717c0605046cd0]::DynamicConfig<rustc_query_system[5c35911b3dd767e9]::query::caches::SingleCache<rustc_middle[829e5443847148d]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[1b717c0605046cd0]::plumbing::QueryCtxt, false>
  26:     0x7ff8ebded683 - rustc_query_impl[1b717c0605046cd0]::query_impl::collect_and_partition_mono_items::get_query_non_incr::__rust_end_short_backtrace
  27:     0x7ff8ebdfa7e5 - rustc_codegen_ssa[99944605b17ba303]::base::codegen_crate::<rustc_codegen_llvm[33f85da736ede4fd]::LlvmCodegenBackend>
  28:     0x7ff8ebdf82bd - <rustc_codegen_llvm[33f85da736ede4fd]::LlvmCodegenBackend as rustc_codegen_ssa[99944605b17ba303]::traits::backend::CodegenBackend>::codegen_crate
  29:     0x7ff8ebd9878f - <rustc_interface[eaf9095e0de5b742]::queries::Linker>::codegen_and_build_linker
  30:     0x7ff8ebd95d99 - <rustc_interface[eaf9095e0de5b742]::passes::create_and_enter_global_ctxt<core[4141bff0bf9ca9dd]::option::Option<rustc_interface[eaf9095e0de5b742]::queries::Linker>, rustc_driver_impl[5af842e777d449e6]::run_compiler::{closure#0}::{closure#2}>::{closure#2} as core[4141bff0bf9ca9dd]::ops::function::FnOnce<(&rustc_session[ea1eec3ed26e42d5]::session::Session, rustc_middle[829e5443847148d]::ty::context::CurrentGcx, alloc[83e7320d2ee5a6b1]::sync::Arc<rustc_data_structures[4e9b1f6e439fd3ef]::jobserver::Proxy>, &std[eaa43e369792efb9]::sync::once_lock::OnceLock<rustc_middle[829e5443847148d]::ty::context::GlobalCtxt>, &rustc_data_structures[4e9b1f6e439fd3ef]::sync::worker_local::WorkerLocal<rustc_middle[829e5443847148d]::arena::Arena>, &rustc_data_structures[4e9b1f6e439fd3ef]::sync::worker_local::WorkerLocal<rustc_hir[67c6535075053fb5]::Arena>, rustc_driver_impl[5af842e777d449e6]::run_compiler::{closure#0}::{closure#2})>>::call_once::{shim:vtable#0}
  31:     0x7ff8ebc6e398 - rustc_interface[eaf9095e0de5b742]::interface::run_compiler::<(), rustc_driver_impl[5af842e777d449e6]::run_compiler::{closure#0}>::{closure#1}
  32:     0x7ff8ebaf7e8e - std[eaa43e369792efb9]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[eaf9095e0de5b742]::util::run_in_thread_with_globals<rustc_interface[eaf9095e0de5b742]::util::run_in_thread_pool_with_globals<rustc_interface[eaf9095e0de5b742]::interface::run_compiler<(), rustc_driver_impl[5af842e777d449e6]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  33:     0x7ff8ebaf7c60 - <std[eaa43e369792efb9]::thread::lifecycle::spawn_unchecked<rustc_interface[eaf9095e0de5b742]::util::run_in_thread_with_globals<rustc_interface[eaf9095e0de5b742]::util::run_in_thread_pool_with_globals<rustc_interface[eaf9095e0de5b742]::interface::run_compiler<(), rustc_driver_impl[5af842e777d449e6]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[4141bff0bf9ca9dd]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  34:     0x7ff8ebaf6038 - <std[eaa43e369792efb9]::sys::thread::unix::Thread>::new::thread_start
  35:     0x7ff8e5494ac3 - start_thread
                               at ./nptl/pthread_create.c:442:8
  36:     0x7ff8e55268c0 - __GI___clone3
                               at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:81:0
  37:                0x0 - <unknown>


rustc version: 1.94.0-nightly (e29fcf45e 2026-01-04)
platform: x86_64-unknown-linux-gnu

query stack during panic:
#0 [items_of_instance] collecting items used by `const_bool::<TypeA>`
#1 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack

No activity

Activity on this issue will appear here.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-specialization`#![feature(specialization)]`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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions