rust-lang / rust-lang/rust

ICE: mir opts crash rustc with -Zmir-preserve-ub in compiler/rustc_mir_transform/src/simplify_comparison_integral.rs

Open
#141,454 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-mir-opt C-bug I-ICE T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code

MIRIFLAGS=" -Zmir-opt-level=1" cargo miri run

#![feature(never_type)]

fn main() {
    let x: &[!] = &[];

    match x {
        &[] => (),
        &[..] => (),
    };

}

Meta

rustc --version --verbose:

rustc 1.89.0-nightly (2eef47813 2025-05-22)
binary: rustc
commit-hash: 2eef47813f25df637026ce3288880e5c587abd92
commit-date: 2025-05-22
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5
Error output
<output>
Backtrace

WARNING: Ignoring `RUSTC_WRAPPER` environment variable, Miri does not support wrapping.
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
     Running `/home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo-miri runner target/miri/x86_64-unknown-linux-gnu/debug/f`
warning: You have explicitly enabled MIR optimizations, overriding Miri's default which is to completely disable them. Any optimizations may hide UB that Miri would otherwise detect, and it is not necessarily possible to predict what kind of UB will be missed. If you are enabling optimizations to make Miri run faster, we advise using cfg(miri) to shrink your workload instead. The performance benefit of enabling MIR optimizations is usually marginal at best.


thread 'rustc' panicked at compiler/rustc_mir_transform/src/simplify_comparison_integral.rs:58:57:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0:     0x7b8cd835e4c3 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h791cca59c2eeee28
   1:     0x7b8cd8a057f7 - core::fmt::write::h97a7af820072d676
   2:     0x7b8cd9ae9cd1 - std::io::Write::write_fmt::h1245a83c9368b241
   3:     0x7b8cd835e322 - std::sys::backtrace::BacktraceLock::print::h7ac4e02696d52df5
   4:     0x7b8cd8361f1a - std::panicking::default_hook::{{closure}}::h8eb31dccbdecdf8a
   5:     0x7b8cd8361a9f - std::panicking::default_hook::h91e0d8dc8f06c0a3
   6:     0x7b8cd7389c83 - std[4968f2f03965d2e2]::panicking::update_hook::<alloc[f94e5fe99def039a]::boxed::Box<rustc_driver_impl[7dbfdb670cae9b1b]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x7b8cd8362793 - std::panicking::rust_panic_with_hook::h361c0cc0b5542574
   8:     0x7b8cd8362456 - std::panicking::begin_panic_handler::{{closure}}::hc4fbc10250e9e65d
   9:     0x7b8cd835e999 - std::sys::backtrace::__rust_end_short_backtrace::hc3ffdf64e78c2fbe
  10:     0x7b8cd836214d - __rustc[ed2da27af75a8cb8]::rust_begin_unwind
  11:     0x7b8cd4c88de0 - core::panicking::panic_fmt::hf0c30228c7d5a750
  12:     0x7b8cd4c8d21c - core::panicking::panic::h4836dc76500b11ed
  13:     0x7b8cd4c89819 - core::option::unwrap_failed::hb8f099542654f599
  14:     0x7b8cd8bf9a20 - <rustc_mir_transform[d1ca8fc46b54c91b]::simplify_comparison_integral::SimplifyComparisonIntegral as rustc_mir_transform[d1ca8fc46b54c91b]::pass_manager::MirPass>::run_pass
  15:     0x7b8cd8a036bd - rustc_mir_transform[d1ca8fc46b54c91b]::pass_manager::run_passes_inner
  16:     0x7b8cd96c587d - rustc_mir_transform[d1ca8fc46b54c91b]::run_optimization_passes
  17:     0x7b8cd96c323b - rustc_mir_transform[d1ca8fc46b54c91b]::optimized_mir
  18:     0x7b8cd96c2ddd - rustc_query_impl[8d9b4f0db2c9623]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[8d9b4f0db2c9623]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[d8138b7bea7b651b]::query::erase::Erased<[u8; 8usize]>>
  19:     0x7b8cd8cf857f - rustc_query_system[eaf5d02d08bb0d89]::query::plumbing::try_execute_query::<rustc_query_impl[8d9b4f0db2c9623]::DynamicConfig<rustc_query_system[eaf5d02d08bb0d89]::query::caches::DefIdCache<rustc_middle[d8138b7bea7b651b]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[8d9b4f0db2c9623]::plumbing::QueryCtxt, true>
  20:     0x7b8cd8cf58d0 - rustc_query_impl[8d9b4f0db2c9623]::query_impl::optimized_mir::get_query_incr::__rust_end_short_backtrace
  21:     0x7b8cd92c0bb5 - <rustc_middle[d8138b7bea7b651b]::ty::context::TyCtxt>::instance_mir
  22:     0x62daca283e8a - <rustc_const_eval[c16e0604a393eedb]::interpret::eval_context::InterpCx<miri[478955529d09d100]::machine::MiriMachine>>::init_fn_call
  23:     0x62daca2e7a10 - <rustc_const_eval[c16e0604a393eedb]::interpret::eval_context::InterpCx<miri[478955529d09d100]::machine::MiriMachine> as miri[478955529d09d100]::concurrency::thread::EvalContextExt>::run_threads
  24:     0x62daca2f9cc4 - miri[478955529d09d100]::eval::eval_entry
  25:     0x62daca41c699 - <miri[4a277e2e70c10989]::MiriCompilerCalls as rustc_driver_impl[7dbfdb670cae9b1b]::Callbacks>::after_analysis
  26:     0x7b8cd9bb3822 - rustc_interface[1a7f6db5e55683ee]::passes::create_and_enter_global_ctxt::<core[2747f52fe7825488]::option::Option<rustc_interface[1a7f6db5e55683ee]::queries::Linker>, rustc_driver_impl[7dbfdb670cae9b1b]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  27:     0x7b8cd9af50cf - rustc_interface[1a7f6db5e55683ee]::interface::run_compiler::<(), rustc_driver_impl[7dbfdb670cae9b1b]::run_compiler::{closure#0}>::{closure#1}
  28:     0x7b8cd9ab23c6 - std[4968f2f03965d2e2]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[1a7f6db5e55683ee]::util::run_in_thread_with_globals<rustc_interface[1a7f6db5e55683ee]::util::run_in_thread_pool_with_globals<rustc_interface[1a7f6db5e55683ee]::interface::run_compiler<(), rustc_driver_impl[7dbfdb670cae9b1b]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  29:     0x7b8cd9ab206b - <<std[4968f2f03965d2e2]::thread::Builder>::spawn_unchecked_<rustc_interface[1a7f6db5e55683ee]::util::run_in_thread_with_globals<rustc_interface[1a7f6db5e55683ee]::util::run_in_thread_pool_with_globals<rustc_interface[1a7f6db5e55683ee]::interface::run_compiler<(), rustc_driver_impl[7dbfdb670cae9b1b]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[2747f52fe7825488]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  30:     0x7b8cd9ab33bd - std::sys::pal::unix::thread::Thread::new::thread_start::h580e41b37d177b5f
  31:     0x7b8cd37ad7eb - <unknown>
  32:     0x7b8cd383118c - <unknown>
  33:                0x0 - <unknown>

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

note: we would appreciate a bug report: https://github.com/rust-lang/miri/issues/new

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

note: please attach the file at `/tmp/im/f/rustc-ice-2025-05-23T15_15_42-1289116.txt` to your bug report

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED] -C target-cpu=native -Z mir-opt-level=1

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [optimized_mir] optimizing MIR for `main`
end of query stack

Miri caused an ICE during evaluation. Here's the interpreter backtrace at the time of the panic:
note: the place in the program where the ICE was triggered
   --> /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
    |
250 |     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: BACKTRACE:
    = note: inside `<fn() as std::ops::FnOnce<()>>::call_once - shim(fn())` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5: 250:71
    = note: inside `std::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/backtrace.rs:152:18: 152:21
    = note: inside closure at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:206:18: 206:75
    = note: inside `std::ops::function::impls::<impl std::ops::FnOnce<()> for &dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe>::call_once` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:284:13: 284:31
    = note: inside `std::panicking::r#try::do_call::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:589:40: 589:43
    = note: inside `std::panicking::r#try::<i32, &dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:552:19: 552:88
    = note: inside `std::panic::catch_unwind::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:359:14: 359:33
    = note: inside closure at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:175:24: 175:49
    = note: inside `std::panicking::r#try::do_call::<{closure@std::rt::lang_start_internal::{closure#0}}, isize>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:589:40: 589:43
    = note: inside `std::panicking::r#try::<isize, {closure@std::rt::lang_start_internal::{closure#0}}>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:552:19: 552:88
    = note: inside `std::panic::catch_unwind::<{closure@std::rt::lang_start_internal::{closure#0}}, isize>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:359:14: 359:33
    = note: inside `std::rt::lang_start_internal` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:171:5: 193:7
    = note: inside `std::rt::lang_start::<()>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:205:5: 210:6

warning: 1 warning emitted

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with compiler/rustc_mir_transform/src/simplify_comparison_integral.rs, especially the unwrap at line 58, and reproduce the ICE with the provided never-type example using MIR optimization. Trace why the comparison pass receives None for this MIR shape. Done means the reproducer no longer panics and the compiler handles it correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.