Skip to content

ICE: called Option::unwrap() on a None value with refutable patterns #138973

Closed as duplicate of#137467
@cyrgani

Description

@cyrgani

This was found while investigating #138958, but unlike that issue, this code is already an ICE on stable and not a recent regression.

Code

pub fn match_const(x: u32) {
    let _ = || {
        let (0 | _) = x;
    };
}

Meta

rustc --version --verbose:

stable 1.85.1, nightly 1.87.0-nightly (2025-03-25 43f0014ef0f242418674)

Error output

<output>
Backtrace

thread 'rustc' panicked at compiler/rustc_mir_build/src/builder/matches/mod.rs:2087:44:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0:     0x7081242dfb8a - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::hfc616348d9ad0abc
   1:     0x708124a14d26 - core::fmt::write::h7ca648217bc79799
   2:     0x708125918711 - std::io::Write::write_fmt::h7960c58bfa5ccbcb
   3:     0x7081242df9e2 - std::sys::backtrace::BacktraceLock::print::h3fb349e80cbe0423
   4:     0x7081242e1f87 - std::panicking::default_hook::{{closure}}::h3366e5842cba645d
   5:     0x7081242e1d70 - std::panicking::default_hook::hd7573a5d4879884b
   6:     0x708123451c48 - std[26cf95b4f122e720]::panicking::update_hook::<alloc[653926f6abfe8a8a]::boxed::Box<rustc_driver_impl[bcf7efa42268b866]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x7081242e2813 - std::panicking::rust_panic_with_hook::h66e909d048c263a9
   8:     0x7081242e24d6 - std::panicking::begin_panic_handler::{{closure}}::h8d9aa8be7e8634cf
   9:     0x7081242e0069 - std::sys::backtrace::__rust_end_short_backtrace::h7d7e47ef99abf6aa
  10:     0x7081242e21cd - rust_begin_unwind
  11:     0x708120fa0ef0 - core::panicking::panic_fmt::hf8ffc7c15bfb58a0
  12:     0x7081211a958c - core::panicking::panic::h209220e15dcf6e53
  13:     0x70812256d319 - core::option::unwrap_failed::h12717cc50f158ee2
  14:     0x708125a19268 - <rustc_mir_build[b1f93083d90156e8]::builder::Builder>::test_candidates
  15:     0x7081253fc60f - <rustc_mir_build[b1f93083d90156e8]::builder::Builder>::match_candidates
  16:     0x7081253fc6ec - <rustc_mir_build[b1f93083d90156e8]::builder::Builder>::match_candidates
  17:     0x708125402d3b - <rustc_mir_build[b1f93083d90156e8]::builder::Builder>::lower_match_tree
  18:     0x708124c48c91 - <rustc_mir_build[b1f93083d90156e8]::builder::Builder>::place_into_pattern
  19:     0x7081212e1f8e - <rustc_mir_build[b1f93083d90156e8]::builder::Builder>::ast_block_stmts
  20:     0x708124c5a6e6 - <rustc_mir_build[b1f93083d90156e8]::builder::Builder>::expr_into_dest
  21:     0x708124c5c3ba - <rustc_mir_build[b1f93083d90156e8]::builder::Builder>::expr_into_dest
  22:     0x7081253f48ca - rustc_mir_build[b1f93083d90156e8]::builder::mir_build
  23:     0x708124a07304 - rustc_mir_transform[db6cedf3c7b9f288]::mir_built
  24:     0x708124a072c7 - rustc_query_impl[291ba1fcadd92b61]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[291ba1fcadd92b61]::query_impl::mir_built::dynamic_query::{closure#2}::{closure#0}, rustc_middle[9ee9ea6fc8845c53]::query::erase::Erased<[u8; 8usize]>>
  25:     0x708124d0b817 - rustc_query_system[397df17712590249]::query::plumbing::try_execute_query::<rustc_query_impl[291ba1fcadd92b61]::DynamicConfig<rustc_data_structures[f86a4f0d0152e187]::vec_cache::VecCache<rustc_span[59b99cb1407325ff]::def_id::LocalDefId, rustc_middle[9ee9ea6fc8845c53]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[397df17712590249]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[291ba1fcadd92b61]::plumbing::QueryCtxt, false>
  26:     0x708124d0b3cd - rustc_query_impl[291ba1fcadd92b61]::query_impl::mir_built::get_query_non_incr::__rust_end_short_backtrace
  27:     0x708125418751 - <rustc_mir_build[b1f93083d90156e8]::check_unsafety::UnsafetyVisitor>::visit_inner_body
  28:     0x708125419898 - <rustc_mir_build[b1f93083d90156e8]::check_unsafety::UnsafetyVisitor as rustc_middle[9ee9ea6fc8845c53]::thir::visit::Visitor>::visit_expr
  29:     0x7081254193ac - <rustc_mir_build[b1f93083d90156e8]::check_unsafety::UnsafetyVisitor as rustc_middle[9ee9ea6fc8845c53]::thir::visit::Visitor>::visit_block
  30:     0x70812541ac1f - <rustc_mir_build[b1f93083d90156e8]::check_unsafety::UnsafetyVisitor as rustc_middle[9ee9ea6fc8845c53]::thir::visit::Visitor>::visit_expr
  31:     0x708125419898 - <rustc_mir_build[b1f93083d90156e8]::check_unsafety::UnsafetyVisitor as rustc_middle[9ee9ea6fc8845c53]::thir::visit::Visitor>::visit_expr
  32:     0x708121b05b51 - rustc_mir_build[b1f93083d90156e8]::check_unsafety::check_unsafety
  33:     0x708125222dbd - rustc_query_impl[291ba1fcadd92b61]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[291ba1fcadd92b61]::query_impl::check_unsafety::dynamic_query::{closure#2}::{closure#0}, rustc_middle[9ee9ea6fc8845c53]::query::erase::Erased<[u8; 0usize]>>
  34:     0x708125223070 - rustc_query_system[397df17712590249]::query::plumbing::try_execute_query::<rustc_query_impl[291ba1fcadd92b61]::DynamicConfig<rustc_data_structures[f86a4f0d0152e187]::vec_cache::VecCache<rustc_span[59b99cb1407325ff]::def_id::LocalDefId, rustc_middle[9ee9ea6fc8845c53]::query::erase::Erased<[u8; 0usize]>, rustc_query_system[397df17712590249]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[291ba1fcadd92b61]::plumbing::QueryCtxt, false>
  35:     0x708125222d01 - rustc_query_impl[291ba1fcadd92b61]::query_impl::check_unsafety::get_query_non_incr::__rust_end_short_backtrace
  36:     0x708124df91fe - rustc_interface[ad5173bb1fff4f1]::passes::run_required_analyses
  37:     0x70812591c59e - rustc_interface[ad5173bb1fff4f1]::passes::analysis
  38:     0x70812591c56f - rustc_query_impl[291ba1fcadd92b61]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[291ba1fcadd92b61]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[9ee9ea6fc8845c53]::query::erase::Erased<[u8; 0usize]>>
  39:     0x7081259c0415 - rustc_query_system[397df17712590249]::query::plumbing::try_execute_query::<rustc_query_impl[291ba1fcadd92b61]::DynamicConfig<rustc_query_system[397df17712590249]::query::caches::SingleCache<rustc_middle[9ee9ea6fc8845c53]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[291ba1fcadd92b61]::plumbing::QueryCtxt, false>
  40:     0x7081259c014e - rustc_query_impl[291ba1fcadd92b61]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  41:     0x70812595ad9e - rustc_interface[ad5173bb1fff4f1]::passes::create_and_enter_global_ctxt::<core[1d264cc251a564c0]::option::Option<rustc_interface[ad5173bb1fff4f1]::queries::Linker>, rustc_driver_impl[bcf7efa42268b866]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  42:     0x708125999516 - rustc_interface[ad5173bb1fff4f1]::interface::run_compiler::<(), rustc_driver_impl[bcf7efa42268b866]::run_compiler::{closure#0}>::{closure#1}
  43:     0x708125883511 - std[26cf95b4f122e720]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[ad5173bb1fff4f1]::util::run_in_thread_with_globals<rustc_interface[ad5173bb1fff4f1]::util::run_in_thread_pool_with_globals<rustc_interface[ad5173bb1fff4f1]::interface::run_compiler<(), rustc_driver_impl[bcf7efa42268b866]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  44:     0x7081258839c8 - <<std[26cf95b4f122e720]::thread::Builder>::spawn_unchecked_<rustc_interface[ad5173bb1fff4f1]::util::run_in_thread_with_globals<rustc_interface[ad5173bb1fff4f1]::util::run_in_thread_pool_with_globals<rustc_interface[ad5173bb1fff4f1]::interface::run_compiler<(), rustc_driver_impl[bcf7efa42268b866]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[1d264cc251a564c0]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  45:     0x708125884f81 - std::sys::pal::unix::thread::Thread::new::thread_start::hc1bbaaa2227026bb
  46:     0x70812707ca94 - <unknown>
  47:     0x708127109a34 - clone
  48:                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: rustc 1.85.1 (4eb161250 2025-03-15) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2

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

query stack during panic:
#0 [mir_built] building MIR for `match_const::{closure#0}`
#1 [check_unsafety] unsafety-checking `match_const`
end of query stack
error: could not compile `playground` (lib)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions