Skip to content

[ICE]: do not use optimized_mir for constants #153860

@matthiaskrgr

Description

@matthiaskrgr

auto-reduced (treereduce-rust):

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

const fn create_array<const N: usize>(mut f: impl FnMut(usize) -> u32 + Copy) -> [u32; N] {
    let mut array = [8; N];

    array
}

fn main() {
    let x = const { create_array(const |i| 2 * i as u32) };
    assert_eq!(x, [0, 2, 4, 6, 8]);

    let y = const { create_array(const |i| 2 * i as u32 + 1) };
    assert_eq!(y, [1, 3, 5, 7, 9]);
}

original:

#![feature(const_closures, const_trait_impl)]

const fn create_array<const N: usize>(mut f: impl FnMut(usize) -> u32 + Copy) -> [u32; N] {
    let mut array = [8; N];
    let mut i = 0;
    loop {
            break;
        }
    array
}

fn main() {
    let x = const { create_array(const |i| 2 * i as u32) };
    assert_eq!(x, [0, 2, 4, 6, 8]);

    let y = const { create_array(const |i| 2 * i as u32 + 1) };
    assert_eq!(y, [1, 3, 5, 7, 9]);
}

Version information

rustc 1.96.0-nightly (620e36a8d 2026-03-14)
binary: rustc
commit-hash: 620e36a8d1fc2f9e5694ce83c2631877651a962c
commit-date: 2026-03-14
host: x86_64-unknown-linux-gnu
release: 1.96.0-nightly
LLVM version: 22.1.0

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

warning: variable does not need to be mutable
 --> /tmp/icemaker_global_tempdir.SfRO4ViS2tri/rustc_testrunner_tmpdir_reporting.IcmoXMtfbRQI/mvce.rs:3:39
  |
3 | const fn create_array<const N: usize>(mut f: impl FnMut(usize) -> u32 + Copy) -> [u32; N] {
  |                                       ----^
  |                                       |
  |                                       help: remove this `mut`
  |
  = note: `#[warn(unused_mut)]` (part of `#[warn(unused)]`) on by default

warning: variable does not need to be mutable
 --> /tmp/icemaker_global_tempdir.SfRO4ViS2tri/rustc_testrunner_tmpdir_reporting.IcmoXMtfbRQI/mvce.rs:4:9
  |
4 |     let mut array = [8; N];
  |         ----^^^^^
  |         |
  |         help: remove this `mut`

warning: unused variable: `f`
 --> /tmp/icemaker_global_tempdir.SfRO4ViS2tri/rustc_testrunner_tmpdir_reporting.IcmoXMtfbRQI/mvce.rs:3:39
  |
3 | const fn create_array<const N: usize>(mut f: impl FnMut(usize) -> u32 + Copy) -> [u32; N] {
  |                                       ^^^^^ help: if this is intentional, prefix it with an underscore: `_f`
  |
  = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default


thread 'rustc' (115826) panicked at /rustc-dev/620e36a8d1fc2f9e5694ce83c2631877651a962c/compiler/rustc_mir_transform/src/lib.rs:806:24:
do not use `optimized_mir` for constants: Const { inline: true }
stack backtrace:
   0:     0x7fbfb4333edb - <<std[19a6abdc00a85c0a]::sys::backtrace::BacktraceLock>::print::DisplayBacktrace as core[a01f64186d0a4116]::fmt::Display>::fmt
   1:     0x7fbfb4a1d608 - core[a01f64186d0a4116]::fmt::write
   2:     0x7fbfb434aee6 - <std[19a6abdc00a85c0a]::sys::stdio::unix::Stderr as std[19a6abdc00a85c0a]::io::Write>::write_fmt
   3:     0x7fbfb4309fc8 - std[19a6abdc00a85c0a]::panicking::default_hook::{closure#0}
   4:     0x7fbfb4327353 - std[19a6abdc00a85c0a]::panicking::default_hook
   5:     0x7fbfb332304c - std[19a6abdc00a85c0a]::panicking::update_hook::<alloc[2a768ccbf5681b63]::boxed::Box<rustc_driver_impl[66fe8e6405ecffa8]::install_ice_hook::{closure#1}>>::{closure#0}
   6:     0x7fbfb4327632 - std[19a6abdc00a85c0a]::panicking::panic_with_hook
   7:     0x7fbfb430a088 - std[19a6abdc00a85c0a]::panicking::panic_handler::{closure#0}
   8:     0x7fbfb42fe5d9 - std[19a6abdc00a85c0a]::sys::backtrace::__rust_end_short_backtrace::<std[19a6abdc00a85c0a]::panicking::panic_handler::{closure#0}, !>
   9:     0x7fbfb430baed - __rustc[8095b879eff51ae9]::rust_begin_unwind
  10:     0x7fbfb0eeef3c - core[a01f64186d0a4116]::panicking::panic_fmt
  11:     0x7fbfb4f6d655 - rustc_mir_transform[6f2825949973c21d]::optimized_mir
  12:     0x7fbfb4f6cee3 - rustc_query_impl[2a0fe4806a816c14]::query_impl::optimized_mir::invoke_provider_fn::__rust_begin_short_backtrace
  13:     0x7fbfb4a5d68a - rustc_query_impl[2a0fe4806a816c14]::execution::try_execute_query::<rustc_middle[fd09a3981aae0ca9]::query::caches::DefIdCache<rustc_middle[fd09a3981aae0ca9]::query::erase::ErasedData<[u8; 8usize]>>, false>
  14:     0x7fbfb4a5cc0e - rustc_query_impl[2a0fe4806a816c14]::query_impl::optimized_mir::execute_query_non_incr::__rust_end_short_backtrace
  15:     0x7fbfb563f7ce - <rustc_middle[fd09a3981aae0ca9]::ty::context::TyCtxt>::instance_mir
  16:     0x7fbfb564408b - rustc_monomorphize[8c46c46852f8fc83]::collector::items_of_instance
  17:     0x7fbfb5644027 - rustc_query_impl[2a0fe4806a816c14]::query_impl::items_of_instance::invoke_provider_fn::__rust_begin_short_backtrace
  18:     0x7fbfb5640b1a - rustc_query_impl[2a0fe4806a816c14]::execution::try_execute_query::<rustc_middle[fd09a3981aae0ca9]::query::caches::DefaultCache<(rustc_middle[fd09a3981aae0ca9]::ty::instance::Instance, rustc_middle[fd09a3981aae0ca9]::mir::mono::CollectionMode), rustc_middle[fd09a3981aae0ca9]::query::erase::ErasedData<[u8; 32usize]>>, false>
  19:     0x7fbfb5640761 - rustc_query_impl[2a0fe4806a816c14]::query_impl::items_of_instance::execute_query_non_incr::__rust_end_short_backtrace
  20:     0x7fbfb5f13dce - rustc_monomorphize[8c46c46852f8fc83]::collector::collect_items_rec
  21:     0x7fbfb4eeee25 - rustc_monomorphize[8c46c46852f8fc83]::collector::collect_crate_mono_items::{closure#1}::{closure#0}
  22:     0x7fbfb5189879 - rustc_monomorphize[8c46c46852f8fc83]::partitioning::collect_and_partition_mono_items
  23:     0x7fbfb5189516 - rustc_query_impl[2a0fe4806a816c14]::query_impl::collect_and_partition_mono_items::invoke_provider_fn::__rust_begin_short_backtrace
  24:     0x7fbfb5c6952c - rustc_query_impl[2a0fe4806a816c14]::execution::try_execute_query::<rustc_middle[fd09a3981aae0ca9]::query::caches::SingleCache<rustc_middle[fd09a3981aae0ca9]::query::erase::ErasedData<[u8; 24usize]>>, false>
  25:     0x7fbfb5c692bc - rustc_query_impl[2a0fe4806a816c14]::query_impl::collect_and_partition_mono_items::execute_query_non_incr::__rust_end_short_backtrace
  26:     0x7fbfb5d47f79 - rustc_codegen_ssa[b421984d3b93326d]::base::codegen_crate::<rustc_codegen_llvm[35ad57f6c7ed31a0]::LlvmCodegenBackend>
  27:     0x7fbfb5d47c8d - <rustc_codegen_llvm[35ad57f6c7ed31a0]::LlvmCodegenBackend as rustc_codegen_ssa[b421984d3b93326d]::traits::backend::CodegenBackend>::codegen_crate
  28:     0x7fbfb5c1d63a - <rustc_interface[bf7b071ad3326fb4]::queries::Linker>::codegen_and_build_linker
  29:     0x7fbfb5c18101 - rustc_interface[bf7b071ad3326fb4]::interface::run_compiler::<(), rustc_driver_impl[66fe8e6405ecffa8]::run_compiler::{closure#0}>::{closure#1}
  30:     0x7fbfb5c4cb7e - std[19a6abdc00a85c0a]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[bf7b071ad3326fb4]::util::run_in_thread_with_globals<rustc_interface[bf7b071ad3326fb4]::util::run_in_thread_pool_with_globals<rustc_interface[bf7b071ad3326fb4]::interface::run_compiler<(), rustc_driver_impl[66fe8e6405ecffa8]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  31:     0x7fbfb5c4d420 - <std[19a6abdc00a85c0a]::thread::lifecycle::spawn_unchecked<rustc_interface[bf7b071ad3326fb4]::util::run_in_thread_with_globals<rustc_interface[bf7b071ad3326fb4]::util::run_in_thread_pool_with_globals<rustc_interface[bf7b071ad3326fb4]::interface::run_compiler<(), rustc_driver_impl[66fe8e6405ecffa8]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[a01f64186d0a4116]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  32:     0x7fbfb5c4e2ac - <std[19a6abdc00a85c0a]::sys::thread::unix::Thread>::new::thread_start
  33:     0x7fbfaf6a598b - <unknown>
  34:     0x7fbfaf729a0c - <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.96.0-nightly (620e36a8d 2026-03-14) 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 `main::{constant#0}::{closure#0}`
#1 [items_of_instance] collecting items used by `main::{constant#0}::{closure#0}`
#2 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack

thread 'rustc' (115826) panicked at /rustc-dev/620e36a8d1fc2f9e5694ce83c2631877651a962c/compiler/rustc_mir_transform/src/lib.rs:806:24:
do not use `optimized_mir` for constants: Const { inline: true }
stack backtrace:
   0:     0x7fbfb4333edb - <<std[19a6abdc00a85c0a]::sys::backtrace::BacktraceLock>::print::DisplayBacktrace as core[a01f64186d0a4116]::fmt::Display>::fmt
   1:     0x7fbfb4a1d608 - core[a01f64186d0a4116]::fmt::write
   2:     0x7fbfb434aee6 - <std[19a6abdc00a85c0a]::sys::stdio::unix::Stderr as std[19a6abdc00a85c0a]::io::Write>::write_fmt
   3:     0x7fbfb4309fc8 - std[19a6abdc00a85c0a]::panicking::default_hook::{closure#0}
   4:     0x7fbfb4327353 - std[19a6abdc00a85c0a]::panicking::default_hook
   5:     0x7fbfb332304c - std[19a6abdc00a85c0a]::panicking::update_hook::<alloc[2a768ccbf5681b63]::boxed::Box<rustc_driver_impl[66fe8e6405ecffa8]::install_ice_hook::{closure#1}>>::{closure#0}
   6:     0x7fbfb4327632 - std[19a6abdc00a85c0a]::panicking::panic_with_hook
   7:     0x7fbfb430a088 - std[19a6abdc00a85c0a]::panicking::panic_handler::{closure#0}
   8:     0x7fbfb42fe5d9 - std[19a6abdc00a85c0a]::sys::backtrace::__rust_end_short_backtrace::<std[19a6abdc00a85c0a]::panicking::panic_handler::{closure#0}, !>
   9:     0x7fbfb430baed - __rustc[8095b879eff51ae9]::rust_begin_unwind
  10:     0x7fbfb0eeef3c - core[a01f64186d0a4116]::panicking::panic_fmt
  11:     0x7fbfb4f6d655 - rustc_mir_transform[6f2825949973c21d]::optimized_mir
  12:     0x7fbfb4f6cee3 - rustc_query_impl[2a0fe4806a816c14]::query_impl::optimized_mir::invoke_provider_fn::__rust_begin_short_backtrace
  13:     0x7fbfb4a5d68a - rustc_query_impl[2a0fe4806a816c14]::execution::try_execute_query::<rustc_middle[fd09a3981aae0ca9]::query::caches::DefIdCache<rustc_middle[fd09a3981aae0ca9]::query::erase::ErasedData<[u8; 8usize]>>, false>
  14:     0x7fbfb4a5cc0e - rustc_query_impl[2a0fe4806a816c14]::query_impl::optimized_mir::execute_query_non_incr::__rust_end_short_backtrace
  15:     0x7fbfb563f7ce - <rustc_middle[fd09a3981aae0ca9]::ty::context::TyCtxt>::instance_mir
  16:     0x7fbfb564408b - rustc_monomorphize[8c46c46852f8fc83]::collector::items_of_instance
  17:     0x7fbfb5644027 - rustc_query_impl[2a0fe4806a816c14]::query_impl::items_of_instance::invoke_provider_fn::__rust_begin_short_backtrace
  18:     0x7fbfb5640b1a - rustc_query_impl[2a0fe4806a816c14]::execution::try_execute_query::<rustc_middle[fd09a3981aae0ca9]::query::caches::DefaultCache<(rustc_middle[fd09a3981aae0ca9]::ty::instance::Instance, rustc_middle[fd09a3981aae0ca9]::mir::mono::CollectionMode), rustc_middle[fd09a3981aae0ca9]::query::erase::ErasedData<[u8; 32usize]>>, false>
  19:     0x7fbfb5640761 - rustc_query_impl[2a0fe4806a816c14]::query_impl::items_of_instance::execute_query_non_incr::__rust_end_short_backtrace
  20:     0x7fbfb5f13dce - rustc_monomorphize[8c46c46852f8fc83]::collector::collect_items_rec
  21:     0x7fbfb4eeee25 - rustc_monomorphize[8c46c46852f8fc83]::collector::collect_crate_mono_items::{closure#1}::{closure#0}
  22:     0x7fbfb5189879 - rustc_monomorphize[8c46c46852f8fc83]::partitioning::collect_and_partition_mono_items
  23:     0x7fbfb5189516 - rustc_query_impl[2a0fe4806a816c14]::query_impl::collect_and_partition_mono_items::invoke_provider_fn::__rust_begin_short_backtrace
  24:     0x7fbfb5c6952c - rustc_query_impl[2a0fe4806a816c14]::execution::try_execute_query::<rustc_middle[fd09a3981aae0ca9]::query::caches::SingleCache<rustc_middle[fd09a3981aae0ca9]::query::erase::ErasedData<[u8; 24usize]>>, false>
  25:     0x7fbfb5c692bc - rustc_query_impl[2a0fe4806a816c14]::query_impl::collect_and_partition_mono_items::execute_query_non_incr::__rust_end_short_backtrace
  26:     0x7fbfb5d47f79 - rustc_codegen_ssa[b421984d3b93326d]::base::codegen_crate::<rustc_codegen_llvm[35ad57f6c7ed31a0]::LlvmCodegenBackend>
  27:     0x7fbfb5d47c8d - <rustc_codegen_llvm[35ad57f6c7ed31a0]::LlvmCodegenBackend as rustc_codegen_ssa[b421984d3b93326d]::traits::backend::CodegenBackend>::codegen_crate
  28:     0x7fbfb5c1d63a - <rustc_interface[bf7b071ad3326fb4]::queries::Linker>::codegen_and_build_linker
  29:     0x7fbfb5c18101 - rustc_interface[bf7b071ad3326fb4]::interface::run_compiler::<(), rustc_driver_impl[66fe8e6405ecffa8]::run_compiler::{closure#0}>::{closure#1}
  30:     0x7fbfb5c4cb7e - std[19a6abdc00a85c0a]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[bf7b071ad3326fb4]::util::run_in_thread_with_globals<rustc_interface[bf7b071ad3326fb4]::util::run_in_thread_pool_with_globals<rustc_interface[bf7b071ad3326fb4]::interface::run_compiler<(), rustc_driver_impl[66fe8e6405ecffa8]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  31:     0x7fbfb5c4d420 - <std[19a6abdc00a85c0a]::thread::lifecycle::spawn_unchecked<rustc_interface[bf7b071ad3326fb4]::util::run_in_thread_with_globals<rustc_interface[bf7b071ad3326fb4]::util::run_in_thread_pool_with_globals<rustc_interface[bf7b071ad3326fb4]::interface::run_compiler<(), rustc_driver_impl[66fe8e6405ecffa8]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[a01f64186d0a4116]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  32:     0x7fbfb5c4e2ac - <std[19a6abdc00a85c0a]::sys::thread::unix::Thread>::new::thread_start
  33:     0x7fbfaf6a598b - <unknown>
  34:     0x7fbfaf729a0c - <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.96.0-nightly (620e36a8d 2026-03-14) 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 `main::{constant#1}::{closure#0}`
#1 [items_of_instance] collecting items used by `main::{constant#1}::{closure#0}`
#2 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
warning: 3 warnings emitted


@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)]`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.

    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