Skip to content

[ICE]: do not use optimized_mir for constants: Const { inline: false } #155803

@matthiaskrgr

Description

@matthiaskrgr

auto-reduced (treereduce-rust):

//@compile-flags: -Clink-dead-code=true
#![feature(const_closures, const_trait_impl)]

const _: () = {
    assert!((const || true)());
};

fn main() {}

original:

//@ check-pass
// 'href="foreigntype.ExternType.html#method.test"'
//@revisions: next old
#![feature(const_closures, const_trait_impl)]

const _: () = {
    assert!((const || true)());
};

fn main() {}

Version information

rustc 1.97.0-nightly (9838411cb 2026-04-25)
binary: rustc
commit-hash: 9838411cb723b60dc62b1625751075c4d933b992
commit-date: 2026-04-25
host: x86_64-unknown-linux-gnu
release: 1.97.0-nightly
LLVM version: 22.1.2

Possibly related line of code:

match tcx.hir_body_const_context(did) {
// Run the `mir_for_ctfe` query, which depends on `mir_drops_elaborated_and_const_checked`
// which we are going to steal below. Thus we need to run `mir_for_ctfe` first, so it
// computes and caches its result.
Some(hir::ConstContext::ConstFn) => tcx.ensure_done().mir_for_ctfe(did),
None => {}
Some(other) => panic!("do not use `optimized_mir` for constants: {other:?}"),
}
debug!("about to call mir_drops_elaborated...");
let body = tcx.mir_drops_elaborated_and_const_checked(did).steal();
let mut body = remap_mir_for_const_eval_select(tcx, body, hir::Constness::NotConst);
if body.tainted_by_errors.is_some() {

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc -Clink-dead-code=true

Program output


thread 'rustc' (327610) panicked at /rustc-dev/9838411cb723b60dc62b1625751075c4d933b992/compiler/rustc_mir_transform/src/lib.rs:806:24:
do not use `optimized_mir` for constants: Const { inline: false }
stack backtrace:
   0:     0x7fa51338bf8b - <<std[1786388830a04a8d]::sys::backtrace::BacktraceLock>::print::DisplayBacktrace as core[e8d809a99398819b]::fmt::Display>::fmt
   1:     0x7fa513a2d988 - core[e8d809a99398819b]::fmt::write
   2:     0x7fa5133a2bc6 - <std[1786388830a04a8d]::sys::stdio::unix::Stderr as std[1786388830a04a8d]::io::Write>::write_fmt
   3:     0x7fa513362208 - std[1786388830a04a8d]::panicking::default_hook::{closure#0}
   4:     0x7fa51337f5f3 - std[1786388830a04a8d]::panicking::default_hook
   5:     0x7fa51233f87c - std[1786388830a04a8d]::panicking::update_hook::<alloc[f5a461388265045a]::boxed::Box<rustc_driver_impl[da6b3b23f136fde3]::install_ice_hook::{closure#1}>>::{closure#0}
   6:     0x7fa51337f8d2 - std[1786388830a04a8d]::panicking::panic_with_hook
   7:     0x7fa5133622c8 - std[1786388830a04a8d]::panicking::panic_handler::{closure#0}
   8:     0x7fa513356829 - std[1786388830a04a8d]::sys::backtrace::__rust_end_short_backtrace::<std[1786388830a04a8d]::panicking::panic_handler::{closure#0}, !>
   9:     0x7fa513363d2d - __rustc[eef9394372a8438c]::rust_begin_unwind
  10:     0x7fa50ff5b11c - core[e8d809a99398819b]::panicking::panic_fmt
  11:     0x7fa513ecbc69 - rustc_mir_transform[3e9e378900fcb99]::optimized_mir
  12:     0x7fa513ecb4f5 - rustc_query_impl[af88a8b7c3088726]::query_impl::optimized_mir::invoke_provider_fn::__rust_begin_short_backtrace
  13:     0x7fa513a1340d - rustc_query_impl[af88a8b7c3088726]::execution::try_execute_query::<rustc_middle[cc385d643382efa7]::query::caches::DefIdCache<rustc_middle[cc385d643382efa7]::query::erase::ErasedData<[u8; 8usize]>>, false>
  14:     0x7fa513a12a4e - rustc_query_impl[af88a8b7c3088726]::query_impl::optimized_mir::execute_query_non_incr::__rust_end_short_backtrace
  15:     0x7fa5145dc88e - <rustc_middle[cc385d643382efa7]::ty::context::TyCtxt>::instance_mir
  16:     0x7fa5145e104b - rustc_monomorphize[c58646706a397996]::collector::items_of_instance
  17:     0x7fa5145e0fd1 - rustc_query_impl[af88a8b7c3088726]::query_impl::items_of_instance::invoke_provider_fn::__rust_begin_short_backtrace
  18:     0x7fa5145ddc5c - rustc_query_impl[af88a8b7c3088726]::execution::try_execute_query::<rustc_middle[cc385d643382efa7]::query::caches::DefaultCache<(rustc_middle[cc385d643382efa7]::ty::instance::Instance, rustc_middle[cc385d643382efa7]::mono::CollectionMode), rustc_middle[cc385d643382efa7]::query::erase::ErasedData<[u8; 32usize]>>, false>
  19:     0x7fa5145dd8c3 - rustc_query_impl[af88a8b7c3088726]::query_impl::items_of_instance::execute_query_non_incr::__rust_end_short_backtrace
  20:     0x7fa514f05ec4 - rustc_monomorphize[c58646706a397996]::collector::collect_items_rec
  21:     0x7fa5141f87b7 - rustc_monomorphize[c58646706a397996]::collector::collect_crate_mono_items::{closure#1}::{closure#0}
  22:     0x7fa514249a7b - rustc_monomorphize[c58646706a397996]::partitioning::collect_and_partition_mono_items
  23:     0x7fa514249716 - rustc_query_impl[af88a8b7c3088726]::query_impl::collect_and_partition_mono_items::invoke_provider_fn::__rust_begin_short_backtrace
  24:     0x7fa514b891f4 - rustc_query_impl[af88a8b7c3088726]::execution::try_execute_query::<rustc_middle[cc385d643382efa7]::query::caches::SingleCache<rustc_middle[cc385d643382efa7]::query::erase::ErasedData<[u8; 24usize]>>, false>
  25:     0x7fa514b88f8a - rustc_query_impl[af88a8b7c3088726]::query_impl::collect_and_partition_mono_items::execute_query_non_incr::__rust_end_short_backtrace
  26:     0x7fa514c73f33 - rustc_codegen_ssa[f943ad127f8832cc]::base::codegen_crate::<rustc_codegen_llvm[5412e4b4c4de4799]::LlvmCodegenBackend>
  27:     0x7fa514c73c4d - <rustc_codegen_llvm[5412e4b4c4de4799]::LlvmCodegenBackend as rustc_codegen_ssa[f943ad127f8832cc]::traits::backend::CodegenBackend>::codegen_crate
  28:     0x7fa514b2aab2 - <rustc_interface[55fbf8829e20395b]::queries::Linker>::codegen_and_build_linker
  29:     0x7fa514b24d65 - rustc_interface[55fbf8829e20395b]::interface::run_compiler::<(), rustc_driver_impl[da6b3b23f136fde3]::run_compiler::{closure#0}>::{closure#1}
  30:     0x7fa514b76484 - std[1786388830a04a8d]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[55fbf8829e20395b]::util::run_in_thread_with_globals<rustc_interface[55fbf8829e20395b]::util::run_in_thread_pool_with_globals<rustc_interface[55fbf8829e20395b]::interface::run_compiler<(), rustc_driver_impl[da6b3b23f136fde3]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  31:     0x7fa514b76b6d - <std[1786388830a04a8d]::thread::lifecycle::spawn_unchecked<rustc_interface[55fbf8829e20395b]::util::run_in_thread_with_globals<rustc_interface[55fbf8829e20395b]::util::run_in_thread_pool_with_globals<rustc_interface[55fbf8829e20395b]::interface::run_compiler<(), rustc_driver_impl[da6b3b23f136fde3]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[e8d809a99398819b]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  32:     0x7fa514b779ec - <std[1786388830a04a8d]::sys::thread::unix::Thread>::new::thread_start
  33:     0x7fa50e6a597a - <unknown>
  34:     0x7fa50e7292bc - <unknown>
  35:                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: rustc 1.97.0-nightly (9838411cb 2026-04-25) running on x86_64-unknown-linux-gnu

note: compiler flags: -C link-dead-code=true -Z dump-mir-dir=dir

query stack during panic:
#0 [optimized_mir] optimizing MIR for `_::{closure#0}`
#1 [items_of_instance] collecting items used by `_::{closure#0}`
#2 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack

@rustbot label +F-const_closures +F-const_trait_impl

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-const_closures`#![feature(const_closures)]`F-const_trait_impl`#![feature(const_trait_impl)]`I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    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