Skip to content

Conditional assignment of transmute to a function pointer leads to ICE during monomorphization #149143

@Innf107

Description

@Innf107

Code

(Playground)

use std::mem::transmute;

fn main() {
    unsafe {
        let f = if true { transmute } else { safe_transmute };
        
        let x : i64 = f(5i64);
    }
}

unsafe fn safe_transmute<A,B>(x: A) -> B {
    panic!()
}

Meta

Tested with rustc 1.91.0, 1.93.0-nightly

rustc --version --verbose:

rustc 1.91.1 (ed61e7d7e 2025-11-07)
binary: rustc
commit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb
commit-date: 2025-11-07
host: x86_64-unknown-linux-gnu
release: 1.91.1
LLVM version: 21.1.2

In version 1.80, the issue does not happen and it just fails with this error instead

error[E0308]: `if` and `else` have incompatible types
 --> evil.rs:5:46
  |
5 |         let f = if true { transmute } else { safe_transmute };
  |                           ---------          ^^^^^^^^^^^^^^ cannot coerce intrinsics to function pointers
  |                           |
  |                           expected because of this
  |
  = note: expected fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
             found fn item `unsafe fn(_) -> _ {safe_transmute::<_, _>}`

Error output

error: internal compiler error: compiler/rustc_monomorphize/src/collector.rs:972:17: Instance { def: Intrinsic(DefId(2:1839 ~ core[3960]::intrinsics::transmute)), args: [i64, i64] } being reified
Backtrace

thread 'rustc' (27) panicked at compiler/rustc_monomorphize/src/collector.rs:972:17:
Box<dyn Any>
stack backtrace:
   0:     0x7428df24c903 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::haa87a551a4affa55
   1:     0x7428dfa01bd8 - core::fmt::write::h80461e1e45e4fdd2
   2:     0x7428e1005b6f - std::io::Write::write_fmt::h6e6c69b2d6337d9b
   3:     0x7428df211dc2 - std::sys::backtrace::BacktraceLock::print::hf67a46baa621998e
   4:     0x7428df217d19 - std::panicking::default_hook::{{closure}}::h391aa815d5e47ec8
   5:     0x7428df217843 - std::panicking::default_hook::hd6fdcf2489bb807d
   6:     0x7428de1fe9a7 - std[868c2b82e02752c5]::panicking::update_hook::<alloc[2f8cfedda7e895ee]::boxed::Box<rustc_driver_impl[8c00ca3c7ff43d1e]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x7428df21813f - std::panicking::panic_with_hook::h185ddfb86bf14d73
   8:     0x7428de23aeb1 - std[868c2b82e02752c5]::panicking::begin_panic::<rustc_errors[4d8968348e21159]::ExplicitBug>::{closure#0}
   9:     0x7428de22e736 - std[868c2b82e02752c5]::sys::backtrace::__rust_end_short_backtrace::<std[868c2b82e02752c5]::panicking::begin_panic<rustc_errors[4d8968348e21159]::ExplicitBug>::{closure#0}, !>
  10:     0x7428de22e727 - std[868c2b82e02752c5]::panicking::begin_panic::<rustc_errors[4d8968348e21159]::ExplicitBug>
  11:     0x7428de2463d1 - <rustc_errors[4d8968348e21159]::diagnostic::BugAbort as rustc_errors[4d8968348e21159]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x7428de7f431a - rustc_middle[33e32ff2bdd40d7a]::util::bug::opt_span_bug_fmt::<rustc_span[708dd8ee61ad0c72]::span_encoding::Span>::{closure#0}
  13:     0x7428de7f44da - rustc_middle[33e32ff2bdd40d7a]::ty::context::tls::with_opt::<rustc_middle[33e32ff2bdd40d7a]::util::bug::opt_span_bug_fmt<rustc_span[708dd8ee61ad0c72]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  14:     0x7428de7e30ab - rustc_middle[33e32ff2bdd40d7a]::ty::context::tls::with_context_opt::<rustc_middle[33e32ff2bdd40d7a]::ty::context::tls::with_opt<rustc_middle[33e32ff2bdd40d7a]::util::bug::opt_span_bug_fmt<rustc_span[708dd8ee61ad0c72]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  15:     0x7428db8ea6e0 - rustc_middle[33e32ff2bdd40d7a]::util::bug::bug_fmt
  16:     0x7428e0270eeb - rustc_monomorphize[57620993e5f29a4f]::collector::visit_fn_use
  17:     0x7428e028d059 - rustc_monomorphize[57620993e5f29a4f]::collector::items_of_instance
  18:     0x7428e028a072 - rustc_query_impl[2d429222c3b2e6bb]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[2d429222c3b2e6bb]::query_impl::items_of_instance::dynamic_query::{closure#2}::{closure#0}, rustc_middle[33e32ff2bdd40d7a]::query::erase::Erased<[u8; 32usize]>>
  19:     0x7428e02983d0 - rustc_query_system[cd1a210e7e3190c4]::query::plumbing::try_execute_query::<rustc_query_impl[2d429222c3b2e6bb]::DynamicConfig<rustc_query_system[cd1a210e7e3190c4]::query::caches::DefaultCache<(rustc_middle[33e32ff2bdd40d7a]::ty::instance::Instance, rustc_middle[33e32ff2bdd40d7a]::mir::mono::CollectionMode), rustc_middle[33e32ff2bdd40d7a]::query::erase::Erased<[u8; 32usize]>>, false, false, false>, rustc_query_impl[2d429222c3b2e6bb]::plumbing::QueryCtxt, false>
  20:     0x7428e0298003 - rustc_query_impl[2d429222c3b2e6bb]::query_impl::items_of_instance::get_query_non_incr::__rust_end_short_backtrace
  21:     0x7428e029b1e5 - rustc_monomorphize[57620993e5f29a4f]::collector::collect_items_rec
  22:     0x7428dff1dea1 - rustc_monomorphize[57620993e5f29a4f]::collector::collect_crate_mono_items::{closure#1}::{closure#0}
  23:     0x7428e01d543a - rustc_monomorphize[57620993e5f29a4f]::partitioning::collect_and_partition_mono_items
  24:     0x7428e0c74116 - rustc_query_impl[2d429222c3b2e6bb]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[2d429222c3b2e6bb]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle[33e32ff2bdd40d7a]::query::erase::Erased<[u8; 24usize]>>
  25:     0x7428e0c740f1 - <rustc_query_impl[2d429222c3b2e6bb]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2} as core[3960b0be9a8ab2fb]::ops::function::FnOnce<(rustc_middle[33e32ff2bdd40d7a]::ty::context::TyCtxt, ())>>::call_once
  26:     0x7428e0c73c5e - rustc_query_system[cd1a210e7e3190c4]::query::plumbing::try_execute_query::<rustc_query_impl[2d429222c3b2e6bb]::DynamicConfig<rustc_query_system[cd1a210e7e3190c4]::query::caches::SingleCache<rustc_middle[33e32ff2bdd40d7a]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[2d429222c3b2e6bb]::plumbing::QueryCtxt, false>
  27:     0x7428e0c73a03 - rustc_query_impl[2d429222c3b2e6bb]::query_impl::collect_and_partition_mono_items::get_query_non_incr::__rust_end_short_backtrace
  28:     0x7428e0c656de - <rustc_codegen_llvm[4a17616027d59e65]::LlvmCodegenBackend as rustc_codegen_ssa[ed07d4fdeb911617]::traits::backend::CodegenBackend>::codegen_crate
  29:     0x7428e0cf10f0 - <rustc_interface[76e547b88cad6d34]::queries::Linker>::codegen_and_build_linker
  30:     0x7428e0ceec14 - rustc_interface[76e547b88cad6d34]::passes::create_and_enter_global_ctxt::<core[3960b0be9a8ab2fb]::option::Option<rustc_interface[76e547b88cad6d34]::queries::Linker>, rustc_driver_impl[8c00ca3c7ff43d1e]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  31:     0x7428e0cdf83e - rustc_interface[76e547b88cad6d34]::interface::run_compiler::<(), rustc_driver_impl[8c00ca3c7ff43d1e]::run_compiler::{closure#0}>::{closure#1}
  32:     0x7428e0bea938 - std[868c2b82e02752c5]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[76e547b88cad6d34]::util::run_in_thread_with_globals<rustc_interface[76e547b88cad6d34]::util::run_in_thread_pool_with_globals<rustc_interface[76e547b88cad6d34]::interface::run_compiler<(), rustc_driver_impl[8c00ca3c7ff43d1e]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  33:     0x7428e0bea61c - <<std[868c2b82e02752c5]::thread::Builder>::spawn_unchecked_<rustc_interface[76e547b88cad6d34]::util::run_in_thread_with_globals<rustc_interface[76e547b88cad6d34]::util::run_in_thread_pool_with_globals<rustc_interface[76e547b88cad6d34]::interface::run_compiler<(), rustc_driver_impl[8c00ca3c7ff43d1e]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[3960b0be9a8ab2fb]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  34:     0x7428e0bf0c6f - std::sys::thread::unix::Thread::new::thread_start::h10345b7e8309cb92
  35:     0x7428da48aaa4 - <unknown>
  36:     0x7428da517a64 - clone
  37:                0x0 - <unknown>

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.91.1 (ed61e7d7e 2025-11-07) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [items_of_instance] collecting items used by `main`
#1 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
error: aborting due to 1 previous error; 2 warnings emitted

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-intrinsicsArea: IntrinsicsC-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions