Skip to content

ICE: invalid pointer unsizing &i32 -> i32 #138265

Open
@matthiaskrgr

Description

@matthiaskrgr

auto-reduced (treereduce-rust):

#![feature(coerce_unsized)]
#![crate_type = "lib"]
impl<A> std::ops::CoerceUnsized<A> for A {}
pub fn f() {
    [0; {
        let mut c = &0;
        c = &0;
        0
    }]
}

original:

impl<A> std::ops::CoerceUnsized<A> for A {}

fn main_ref() {
    let array = [(); {
        let mut x = &0;
        let mut n = 0;
        while n < 5 {
            x = &0;
        }
        0
    }];

    let mut ptrs: Vec<*const [u8]> = vec![&array[0..0], &array[0..1], &array, &array[1..]];
}

Version information

rustc 1.87.0-nightly (efea9896f 2025-03-08)
binary: rustc
commit-hash: efea9896f506baa08f40444e07774e827646d57a
commit-date: 2025-03-08
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Possibly related line of code:

}
_ => {
// Do not ICE if we are not monomorphic enough.
ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
ensure_monomorphic_enough(*self.tcx, cast_ty.ty)?;
span_bug!(
self.cur_span(),
"unsize_into: invalid conversion: {:?} -> {:?}",
src.layout,
dest.layout
)
}

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc

Program output

error[E0210]: type parameter `A` must be used as the type parameter for some local type (e.g., `MyStruct<A>`)
 --> code.rs:3:6
  |
3 | impl<A> std::ops::CoerceUnsized<A> for A {}
  |      ^ type parameter `A` must be used as the type parameter for some local type
  |
  = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
  = note: only traits defined in the current crate can be implemented for a type parameter

error[E0377]: the trait `CoerceUnsized` may only be implemented for a coercion between structures
 --> code.rs:3:1
  |
3 | impl<A> std::ops::CoerceUnsized<A> for A {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: variable `c` is assigned to, but never used
 --> code.rs:6:17
  |
6 |         let mut c = &0;
  |                 ^
  |
  = note: consider using `_c` instead
  = note: `#[warn(unused_variables)]` on by default

warning: value assigned to `c` is never read
 --> code.rs:7:9
  |
7 |         c = &0;
  |         ^
  |
  = help: maybe it is overwritten before being read?
  = note: `#[warn(unused_assignments)]` on by default

error: internal compiler error: compiler/rustc_const_eval/src/interpret/cast.rs:463:17: invalid pointer unsizing &i32 -> i32
 --> code.rs:7:9
  |
7 |         c = &0;
  |         ^^^^^^


thread 'rustc' panicked at compiler/rustc_const_eval/src/interpret/cast.rs:463:17:
Box<dyn Any>
stack backtrace:
   0:     0x76b8d79b9884 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h92d4bfce6f763b00
   1:     0x76b8d8205bae - core::fmt::write::hb4817f6dd96fad0f
   2:     0x76b8d938c051 - std::io::Write::write_fmt::h314f413d021b4307
   3:     0x76b8d79b96e2 - std::sys::backtrace::BacktraceLock::print::h68d0c48567bf96cd
   4:     0x76b8d79bc008 - std::panicking::default_hook::{{closure}}::h15992f1912031b9e
   5:     0x76b8d79bbb27 - std::panicking::default_hook::hf25af0ac62c164b1
   6:     0x76b8d6b0bbbf - std[aea8b8668118ef2c]::panicking::update_hook::<alloc[a7bb905a442d6351]::boxed::Box<rustc_driver_impl[329d2250e30901ce]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x76b8d79bc873 - std::panicking::rust_panic_with_hook::he1f54632065b4203
   8:     0x76b8d6b482f1 - std[aea8b8668118ef2c]::panicking::begin_panic::<rustc_errors[cfd456e3f342e961]::ExplicitBug>::{closure#0}
   9:     0x76b8d6b3c816 - std[aea8b8668118ef2c]::sys::backtrace::__rust_end_short_backtrace::<std[aea8b8668118ef2c]::panicking::begin_panic<rustc_errors[cfd456e3f342e961]::ExplicitBug>::{closure#0}, !>
  10:     0x76b8d6b39199 - std[aea8b8668118ef2c]::panicking::begin_panic::<rustc_errors[cfd456e3f342e961]::ExplicitBug>
  11:     0x76b8d6b51e51 - <rustc_errors[cfd456e3f342e961]::diagnostic::BugAbort as rustc_errors[cfd456e3f342e961]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x76b8d70a27ac - <rustc_errors[cfd456e3f342e961]::DiagCtxtHandle>::span_bug::<rustc_span[17cc6a99e0b592f1]::span_encoding::Span, alloc[a7bb905a442d6351]::string::String>
  13:     0x76b8d71291f7 - rustc_middle[abc93db11e30059e]::util::bug::opt_span_bug_fmt::<rustc_span[17cc6a99e0b592f1]::span_encoding::Span>::{closure#0}
  14:     0x76b8d7110d5a - rustc_middle[abc93db11e30059e]::ty::context::tls::with_opt::<rustc_middle[abc93db11e30059e]::util::bug::opt_span_bug_fmt<rustc_span[17cc6a99e0b592f1]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  15:     0x76b8d7110bcb - rustc_middle[abc93db11e30059e]::ty::context::tls::with_context_opt::<rustc_middle[abc93db11e30059e]::ty::context::tls::with_opt<rustc_middle[abc93db11e30059e]::util::bug::opt_span_bug_fmt<rustc_span[17cc6a99e0b592f1]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  16:     0x76b8d59c78c7 - rustc_middle[abc93db11e30059e]::util::bug::span_bug_fmt::<rustc_span[17cc6a99e0b592f1]::span_encoding::Span>
  17:     0x76b8d8283b0a - <rustc_const_eval[5a9fe373e48f7954]::interpret::eval_context::InterpCx<rustc_const_eval[5a9fe373e48f7954]::const_eval::machine::CompileTimeMachine>>::unsize_into_ptr
  18:     0x76b8d8f5a899 - <rustc_const_eval[5a9fe373e48f7954]::interpret::eval_context::InterpCx<rustc_const_eval[5a9fe373e48f7954]::const_eval::machine::CompileTimeMachine>>::eval_rvalue_into_place
  19:     0x76b8d8efd830 - rustc_const_eval[5a9fe373e48f7954]::const_eval::eval_queries::eval_to_allocation_raw_provider
  20:     0x76b8d8efcb7e - rustc_query_impl[35192ced76ef19cc]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[35192ced76ef19cc]::query_impl::eval_to_allocation_raw::dynamic_query::{closure#2}::{closure#0}, rustc_middle[abc93db11e30059e]::query::erase::Erased<[u8; 24usize]>>
  21:     0x76b8d8ef65f6 - rustc_query_system[8750ea9310c2c1bc]::query::plumbing::try_execute_query::<rustc_query_impl[35192ced76ef19cc]::DynamicConfig<rustc_query_system[8750ea9310c2c1bc]::query::caches::DefaultCache<rustc_middle[abc93db11e30059e]::ty::PseudoCanonicalInput<rustc_middle[abc93db11e30059e]::mir::interpret::GlobalId>, rustc_middle[abc93db11e30059e]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[35192ced76ef19cc]::plumbing::QueryCtxt, false>
  22:     0x76b8d8ef6274 - rustc_query_impl[35192ced76ef19cc]::query_impl::eval_to_allocation_raw::get_query_non_incr::__rust_end_short_backtrace
  23:     0x76b8d8ece12b - rustc_const_eval[5a9fe373e48f7954]::const_eval::valtrees::eval_to_valtree
  24:     0x76b8d8ecdefe - <rustc_const_eval[5a9fe373e48f7954]::provide::{closure#0} as core[e60e1eed10c732b4]::ops::function::FnOnce<(rustc_middle[abc93db11e30059e]::ty::context::TyCtxt, rustc_middle[abc93db11e30059e]::ty::PseudoCanonicalInput<rustc_middle[abc93db11e30059e]::mir::interpret::GlobalId>)>>::call_once
  25:     0x76b8d8ecde9a - rustc_query_impl[35192ced76ef19cc]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[35192ced76ef19cc]::query_impl::eval_to_valtree::dynamic_query::{closure#2}::{closure#0}, rustc_middle[abc93db11e30059e]::query::erase::Erased<[u8; 24usize]>>
  26:     0x76b8d8ecde53 - <rustc_query_impl[35192ced76ef19cc]::query_impl::eval_to_valtree::dynamic_query::{closure#2} as core[e60e1eed10c732b4]::ops::function::FnOnce<(rustc_middle[abc93db11e30059e]::ty::context::TyCtxt, rustc_middle[abc93db11e30059e]::ty::PseudoCanonicalInput<rustc_middle[abc93db11e30059e]::mir::interpret::GlobalId>)>>::call_once
  27:     0x76b8d8ef66cd - rustc_query_system[8750ea9310c2c1bc]::query::plumbing::try_execute_query::<rustc_query_impl[35192ced76ef19cc]::DynamicConfig<rustc_query_system[8750ea9310c2c1bc]::query::caches::DefaultCache<rustc_middle[abc93db11e30059e]::ty::PseudoCanonicalInput<rustc_middle[abc93db11e30059e]::mir::interpret::GlobalId>, rustc_middle[abc93db11e30059e]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[35192ced76ef19cc]::plumbing::QueryCtxt, false>
  28:     0x76b8d8ef6174 - rustc_query_impl[35192ced76ef19cc]::query_impl::eval_to_valtree::get_query_non_incr::__rust_end_short_backtrace
  29:     0x76b8d8a02de0 - rustc_middle[abc93db11e30059e]::query::plumbing::query_get_at::<rustc_query_system[8750ea9310c2c1bc]::query::caches::DefaultCache<rustc_middle[abc93db11e30059e]::ty::PseudoCanonicalInput<rustc_middle[abc93db11e30059e]::mir::interpret::GlobalId>, rustc_middle[abc93db11e30059e]::query::erase::Erased<[u8; 24usize]>>>
  30:     0x76b8d8a022c7 - <rustc_middle[abc93db11e30059e]::ty::context::TyCtxt>::const_eval_global_id_for_typeck
  31:     0x76b8d8a02173 - <rustc_middle[abc93db11e30059e]::ty::context::TyCtxt>::const_eval_resolve_for_typeck
  32:     0x76b8d8a01ee5 - rustc_trait_selection[579aa0a2043e7cc4]::traits::try_evaluate_const
  33:     0x76b8d8a01ac3 - <rustc_trait_selection[579aa0a2043e7cc4]::traits::normalize::AssocTypeNormalizer as rustc_type_ir[d3a491106fe18c84]::fold::TypeFolder<rustc_middle[abc93db11e30059e]::ty::context::TyCtxt>>::fold_const
  34:     0x76b8d9286c43 - <rustc_hir_typeck[e894de713c4a67b3]::fn_ctxt::FnCtxt>::normalize::<rustc_middle[abc93db11e30059e]::ty::consts::Const>
  35:     0x76b8d9059bf0 - <rustc_hir_typeck[e894de713c4a67b3]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  36:     0x76b8d904b9c6 - <rustc_hir_typeck[e894de713c4a67b3]::fn_ctxt::FnCtxt>::check_expr_block
  37:     0x76b8d9053443 - <rustc_hir_typeck[e894de713c4a67b3]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  38:     0x76b8d88c6149 - rustc_hir_typeck[e894de713c4a67b3]::check::check_fn
  39:     0x76b8d88b9141 - rustc_hir_typeck[e894de713c4a67b3]::typeck_with_inspect::{closure#0}
  40:     0x76b8d88b8736 - rustc_query_impl[35192ced76ef19cc]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[35192ced76ef19cc]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[abc93db11e30059e]::query::erase::Erased<[u8; 8usize]>>
  41:     0x76b8d8531c0c - rustc_query_system[8750ea9310c2c1bc]::query::plumbing::try_execute_query::<rustc_query_impl[35192ced76ef19cc]::DynamicConfig<rustc_data_structures[5971c652a2f302ea]::vec_cache::VecCache<rustc_span[17cc6a99e0b592f1]::def_id::LocalDefId, rustc_middle[abc93db11e30059e]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[8750ea9310c2c1bc]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[35192ced76ef19cc]::plumbing::QueryCtxt, false>
  42:     0x76b8d8530b1f - rustc_query_impl[35192ced76ef19cc]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  43:     0x76b8d85307bf - <rustc_middle[abc93db11e30059e]::ty::context::TyCtxt>::par_hir_body_owners::<rustc_hir_analysis[7d047a4039fbdcf6]::check_crate::{closure#3}>::{closure#0}
  44:     0x76b8d852fe3f - rustc_hir_analysis[7d047a4039fbdcf6]::check_crate
  45:     0x76b8d85ef2b4 - rustc_interface[4c1044b8b3f8fa90]::passes::run_required_analyses
  46:     0x76b8d916c538 - rustc_interface[4c1044b8b3f8fa90]::passes::analysis
  47:     0x76b8d916c517 - rustc_query_impl[35192ced76ef19cc]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[35192ced76ef19cc]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[abc93db11e30059e]::query::erase::Erased<[u8; 0usize]>>
  48:     0x76b8d92b9581 - rustc_query_system[8750ea9310c2c1bc]::query::plumbing::try_execute_query::<rustc_query_impl[35192ced76ef19cc]::DynamicConfig<rustc_query_system[8750ea9310c2c1bc]::query::caches::SingleCache<rustc_middle[abc93db11e30059e]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[35192ced76ef19cc]::plumbing::QueryCtxt, false>
  49:     0x76b8d92b9274 - rustc_query_impl[35192ced76ef19cc]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  50:     0x76b8d933247c - rustc_interface[4c1044b8b3f8fa90]::passes::create_and_enter_global_ctxt::<core[e60e1eed10c732b4]::option::Option<rustc_interface[4c1044b8b3f8fa90]::queries::Linker>, rustc_driver_impl[329d2250e30901ce]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  51:     0x76b8d932246e - rustc_interface[4c1044b8b3f8fa90]::interface::run_compiler::<(), rustc_driver_impl[329d2250e30901ce]::run_compiler::{closure#0}>::{closure#1}
  52:     0x76b8d91ae7c8 - std[aea8b8668118ef2c]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[4c1044b8b3f8fa90]::util::run_in_thread_with_globals<rustc_interface[4c1044b8b3f8fa90]::util::run_in_thread_pool_with_globals<rustc_interface[4c1044b8b3f8fa90]::interface::run_compiler<(), rustc_driver_impl[329d2250e30901ce]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  53:     0x76b8d91af0b4 - <<std[aea8b8668118ef2c]::thread::Builder>::spawn_unchecked_<rustc_interface[4c1044b8b3f8fa90]::util::run_in_thread_with_globals<rustc_interface[4c1044b8b3f8fa90]::util::run_in_thread_pool_with_globals<rustc_interface[4c1044b8b3f8fa90]::interface::run_compiler<(), rustc_driver_impl[329d2250e30901ce]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[e60e1eed10c732b4]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  54:     0x76b8d91b04ab - std::sys::pal::unix::thread::Thread::new::thread_start::hbcfe3ea4381c14e0
  55:     0x76b8d32a370a - <unknown>
  56:     0x76b8d3327aac - <unknown>
  57:                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: please make sure that you have updated to the latest nightly

note: please attach the file at `/home/matthias/vcs/github/CRED/rustc-ice-2025-03-09T08_46_33-2489083.txt` to your bug report

query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `f::{constant#0}`
#1 [eval_to_valtree] evaluating type-level constant
... and 2 other queries... use `env RUST_BACKTRACE=1` to see the full query stack
error: aborting due to 3 previous errors; 2 warnings emitted

Some errors have detailed explanations: E0210, E0377.
For more information about an error, try `rustc --explain E0210`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-coerce_unsizedThe `CoerceUnsized` traitI-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️S-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.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