Skip to content

[ICE]: invalid immediate for given destination place: scalar value has wrong size #153236

Closed

Description

@matthiaskrgr

auto-reduced (treereduce-rust):

#![feature(field_projections)]
use std::field::{field_of, Field};
use std::mem::offset_of;

#[repr(C)]
pub struct Struct {
    a: i32,
    b: i64,
}

fn main() {
    assert_eq!(<field_of!(Struct, b)>::OFFSET, offset_of!((), ()));
}
original code

original:

//@ revisions: old next
//@ [next] compile-flags: -Znext-solver
//@ run-pass
#![expect(incomplete_features)]
#![feature(field_projections)]

use std::field::{Field, field_of};
use std::mem::offset_of;
use std::ptr;

#[repr(C)]
pub struct Struct {
    a: i32,
    b: i64,
}

// FIXME(FRTs): need to mark these fields as used by the `field_of!` macro.
#[expect(dead_code)]
pub union Union {
    a: i32,
    b: i64,
}

#[repr(C, i8)]
pub enum Enum {
    A { a: i32, b: i64 },
    B { x: i64, y: i32 },
}

fn project_ref<'a, T, F: Field<Base = T>>(r: &'a T) -> &'a F::Type {
    unsafe { &*ptr::from_ref(r).byte_add(F::OFFSET).cast() }
}

fn main() {
    assert_eq!(<field_of!(Struct, a)>::OFFSET, offset_of!(Struct, a));
    assert_eq!(<field_of!(Struct, b)>::OFFSET, offset_of!((), ()));

    let _: field_of!(Union, a);
    let _: field_of!(Union, b);

    let _: field_of!(Enum, A.a);
    let _: field_of!(Enum, A.b);
    let _: field_of!(Enum, B.x);
    let _: field_of!(Enum, B.y);

    let s = Struct { a: 42, b: 24 };
    let r = &s;
    let a = project_ref::<Struct, field_of!(Struct, a)>(r);
    let b = project_ref::<Struct, field_of!(Struct, b)>(r);
    assert_eq!(*a, 42);
    assert_eq!(*b, 24);
}

Version information

rustc 1.96.0-nightly (ba1567989 2026-02-28)
binary: rustc
commit-hash: ba1567989ee7774a1fb53aa680a8e4e8daa0f519
commit-date: 2026-02-28
host: x86_64-unknown-linux-gnu
release: 1.96.0-nightly
LLVM version: 22.1.0

Possibly related line of code:

}
/// Assert that this immediate is a valid value for the given ABI.
pub fn assert_matches_abi(self, abi: BackendRepr, msg: &str, cx: &impl HasDataLayout) {
match (self, abi) {
(Immediate::Scalar(scalar), BackendRepr::Scalar(s)) => {
assert_eq!(scalar.size(), s.size(cx), "{msg}: scalar value has wrong size");
if !matches!(s.primitive(), abi::Primitive::Pointer(..)) {
// This is not a pointer, it should not carry provenance.
assert!(
matches!(scalar, Scalar::Int(..)),
"{msg}: scalar value should be an integer, but has provenance"
);

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

Program output

error: offset_of expects dot-separated field and variant names
  --> b.rs:13:63
   |
13 |     assert_eq!(<field_of!(Struct, b)>::OFFSET, offset_of!((), ()));
   |                                                               ^^

warning: the feature `field_projections` is incomplete and may not be safe to use and/or cause compiler crashes
 --> b.rs:1:12
  |
1 | #![feature(field_projections)]
  |            ^^^^^^^^^^^^^^^^^
  |
  = note: see issue #145383 <https://github.com/rust-lang/rust/issues/145383> for more information
  = note: `#[warn(incomplete_features)]` on by default


thread 'rustc' (2095317) panicked at /rustc-dev/ba1567989ee7774a1fb53aa680a8e4e8daa0f519/compiler/rustc_const_eval/src/interpret/operand.rs:122:17:
assertion `left == right` failed: invalid immediate for given destination place: scalar value has wrong size
  left: Size(4 bytes)
 right: Size(8 bytes)
stack backtrace:
   0:     0x7f13cf9aa67b - <<std[fec5da352f41df64]::sys::backtrace::BacktraceLock>::print::DisplayBacktrace as core[655421083fc59668]::fmt::Display>::fmt
   1:     0x7f13d0020288 - core[655421083fc59668]::fmt::write
   2:     0x7f13cf9c1686 - <std[fec5da352f41df64]::sys::stdio::unix::Stderr as std[fec5da352f41df64]::io::Write>::write_fmt
   3:     0x7f13cf980758 - std[fec5da352f41df64]::panicking::default_hook::{closure#0}
   4:     0x7f13cf99dae3 - std[fec5da352f41df64]::panicking::default_hook
   5:     0x7f13ce975070 - std[fec5da352f41df64]::panicking::update_hook::<alloc[c081135bcfa9263a]::boxed::Box<rustc_driver_impl[a0a5e0825e3525c3]::install_ice_hook::{closure#1}>>::{closure#0}
   6:     0x7f13cf99ddc2 - std[fec5da352f41df64]::panicking::panic_with_hook
   7:     0x7f13cf980818 - std[fec5da352f41df64]::panicking::panic_handler::{closure#0}
   8:     0x7f13cf977549 - std[fec5da352f41df64]::sys::backtrace::__rust_end_short_backtrace::<std[fec5da352f41df64]::panicking::panic_handler::{closure#0}, !>
   9:     0x7f13cf98227d - __rustc[70948dbd69585b06]::rust_begin_unwind
  10:     0x7f13cc51fefc - core[655421083fc59668]::panicking::panic_fmt
  11:     0x7f13ce2c0243 - core[655421083fc59668]::panicking::assert_failed_inner
  12:     0x7f13cef743ce - core[655421083fc59668]::panicking::assert_failed::<rustc_abi[efb49786af623574]::Size, rustc_abi[efb49786af623574]::Size>
  13:     0x7f13d046cbd7 - <rustc_const_eval[60546f5659a7e5b8]::interpret::eval_context::InterpCx<rustc_const_eval[60546f5659a7e5b8]::const_eval::machine::CompileTimeMachine>>::copy_op_no_validate::<rustc_const_eval[60546f5659a7e5b8]::interpret::operand::OpTy, rustc_const_eval[60546f5659a7e5b8]::interpret::place::PlaceTy>
  14:     0x7f13d10ac948 - rustc_const_eval[60546f5659a7e5b8]::const_eval::eval_queries::eval_to_allocation_raw_provider
  15:     0x7f13d10aae1c - rustc_query_impl[83437f7fc475934b]::query_impl::eval_to_allocation_raw::invoke_provider_fn::__rust_begin_short_backtrace
  16:     0x7f13d0e6df83 - rustc_query_impl[83437f7fc475934b]::execution::try_execute_query::<rustc_middle[487438de6df3e06a]::query::caches::DefaultCache<rustc_middle[487438de6df3e06a]::ty::PseudoCanonicalInput<rustc_middle[487438de6df3e06a]::mir::interpret::GlobalId>, rustc_middle[487438de6df3e06a]::query::erase::ErasedData<[u8; 24usize]>>, false>
  17:     0x7f13d0e6d8eb - rustc_query_impl[83437f7fc475934b]::query_impl::eval_to_allocation_raw::execute_query_non_incr::__rust_end_short_backtrace
  18:     0x7f13d0e6f521 - rustc_const_eval[60546f5659a7e5b8]::const_eval::eval_queries::eval_to_const_value_raw_provider
  19:     0x7f13d0e6f0a8 - rustc_query_impl[83437f7fc475934b]::query_impl::eval_to_const_value_raw::invoke_provider_fn::__rust_begin_short_backtrace
  20:     0x7f13d0e6de87 - rustc_query_impl[83437f7fc475934b]::execution::try_execute_query::<rustc_middle[487438de6df3e06a]::query::caches::DefaultCache<rustc_middle[487438de6df3e06a]::ty::PseudoCanonicalInput<rustc_middle[487438de6df3e06a]::mir::interpret::GlobalId>, rustc_middle[487438de6df3e06a]::query::erase::ErasedData<[u8; 24usize]>>, false>
  21:     0x7f13d0e6d9eb - rustc_query_impl[83437f7fc475934b]::query_impl::eval_to_const_value_raw::execute_query_non_incr::__rust_end_short_backtrace
  22:     0x7f13d0c5db20 - <rustc_middle[487438de6df3e06a]::ty::context::TyCtxt>::const_eval_resolve
  23:     0x7f13d0761d8e - <rustc_mir_transform[375a1252ba647aa9]::known_panics_lint::ConstPropagator as rustc_middle[487438de6df3e06a]::mir::visit::Visitor>::visit_assign
  24:     0x7f13d075dc36 - <rustc_mir_transform[375a1252ba647aa9]::known_panics_lint::ConstPropagator as rustc_middle[487438de6df3e06a]::mir::visit::Visitor>::visit_body
  25:     0x7f13d075afe8 - <rustc_mir_transform[375a1252ba647aa9]::known_panics_lint::KnownPanicsLint as rustc_mir_transform[375a1252ba647aa9]::pass_manager::MirLint>::run_lint
  26:     0x7f13d000ca6a - rustc_mir_transform[375a1252ba647aa9]::run_analysis_to_runtime_passes
  27:     0x7f13d0de3034 - rustc_mir_transform[375a1252ba647aa9]::promoted_mir
  28:     0x7f13d0de2cb3 - rustc_query_impl[83437f7fc475934b]::query_impl::promoted_mir::invoke_provider_fn::__rust_begin_short_backtrace
  29:     0x7f13d005a4e8 - rustc_query_impl[83437f7fc475934b]::execution::try_execute_query::<rustc_middle[487438de6df3e06a]::query::caches::DefIdCache<rustc_middle[487438de6df3e06a]::query::erase::ErasedData<[u8; 8usize]>>, false>
  30:     0x7f13d0059764 - rustc_query_impl[83437f7fc475934b]::query_impl::promoted_mir::execute_query_non_incr::__rust_end_short_backtrace
  31:     0x7f13d10aa9e4 - <rustc_const_eval[60546f5659a7e5b8]::interpret::eval_context::InterpCx<rustc_const_eval[60546f5659a7e5b8]::const_eval::machine::CompileTimeMachine>>::load_mir
  32:     0x7f13d10ab59a - rustc_const_eval[60546f5659a7e5b8]::const_eval::eval_queries::eval_to_allocation_raw_provider
  33:     0x7f13d10aae1c - rustc_query_impl[83437f7fc475934b]::query_impl::eval_to_allocation_raw::invoke_provider_fn::__rust_begin_short_backtrace
  34:     0x7f13d0e6df83 - rustc_query_impl[83437f7fc475934b]::execution::try_execute_query::<rustc_middle[487438de6df3e06a]::query::caches::DefaultCache<rustc_middle[487438de6df3e06a]::ty::PseudoCanonicalInput<rustc_middle[487438de6df3e06a]::mir::interpret::GlobalId>, rustc_middle[487438de6df3e06a]::query::erase::ErasedData<[u8; 24usize]>>, false>
  35:     0x7f13d0e6d8eb - rustc_query_impl[83437f7fc475934b]::query_impl::eval_to_allocation_raw::execute_query_non_incr::__rust_end_short_backtrace
  36:     0x7f13d0e6f521 - rustc_const_eval[60546f5659a7e5b8]::const_eval::eval_queries::eval_to_const_value_raw_provider
  37:     0x7f13d0e6f0a8 - rustc_query_impl[83437f7fc475934b]::query_impl::eval_to_const_value_raw::invoke_provider_fn::__rust_begin_short_backtrace
  38:     0x7f13d0e6de87 - rustc_query_impl[83437f7fc475934b]::execution::try_execute_query::<rustc_middle[487438de6df3e06a]::query::caches::DefaultCache<rustc_middle[487438de6df3e06a]::ty::PseudoCanonicalInput<rustc_middle[487438de6df3e06a]::mir::interpret::GlobalId>, rustc_middle[487438de6df3e06a]::query::erase::ErasedData<[u8; 24usize]>>, false>
  39:     0x7f13d0e6d9eb - rustc_query_impl[83437f7fc475934b]::query_impl::eval_to_const_value_raw::execute_query_non_incr::__rust_end_short_backtrace
  40:     0x7f13d0c5db20 - <rustc_middle[487438de6df3e06a]::ty::context::TyCtxt>::const_eval_resolve
  41:     0x7f13d0761d8e - <rustc_mir_transform[375a1252ba647aa9]::known_panics_lint::ConstPropagator as rustc_middle[487438de6df3e06a]::mir::visit::Visitor>::visit_assign
  42:     0x7f13d075dc36 - <rustc_mir_transform[375a1252ba647aa9]::known_panics_lint::ConstPropagator as rustc_middle[487438de6df3e06a]::mir::visit::Visitor>::visit_body
  43:     0x7f13d075afe8 - <rustc_mir_transform[375a1252ba647aa9]::known_panics_lint::KnownPanicsLint as rustc_mir_transform[375a1252ba647aa9]::pass_manager::MirLint>::run_lint
  44:     0x7f13d000ca6a - rustc_mir_transform[375a1252ba647aa9]::run_analysis_to_runtime_passes
  45:     0x7f13d0de1784 - rustc_mir_transform[375a1252ba647aa9]::mir_drops_elaborated_and_const_checked
  46:     0x7f13d0de11e1 - rustc_query_impl[83437f7fc475934b]::query_impl::mir_drops_elaborated_and_const_checked::invoke_provider_fn::__rust_begin_short_backtrace
  47:     0x7f13d016790a - rustc_query_impl[83437f7fc475934b]::execution::try_execute_query::<rustc_data_structures[e05ccdf9cc6e0e20]::vec_cache::VecCache<rustc_span[8c85abb5df2f449]::def_id::LocalDefId, rustc_middle[487438de6df3e06a]::query::erase::ErasedData<[u8; 8usize]>, rustc_middle[487438de6df3e06a]::dep_graph::graph::DepNodeIndex>, false>
  48:     0x7f13d0167043 - rustc_query_impl[83437f7fc475934b]::query_impl::mir_drops_elaborated_and_const_checked::execute_query_non_incr::__rust_end_short_backtrace
  49:     0x7f13d01653c8 - <rustc_middle[487438de6df3e06a]::ty::context::TyCtxt>::par_hir_body_owners::<rustc_interface[b660c6c936fc555e]::passes::run_required_analyses::{closure#1}::{closure#0}>::{closure#0}
  50:     0x7f13d01647af - rustc_interface[b660c6c936fc555e]::passes::analysis
  51:     0x7f13d120925a - rustc_query_impl[83437f7fc475934b]::execution::try_execute_query::<rustc_middle[487438de6df3e06a]::query::caches::SingleCache<rustc_middle[487438de6df3e06a]::query::erase::ErasedData<[u8; 0usize]>>, false>
  52:     0x7f13d1209029 - rustc_query_impl[83437f7fc475934b]::query_impl::analysis::execute_query_non_incr::__rust_end_short_backtrace
  53:     0x7f13d1229c2d - rustc_interface[b660c6c936fc555e]::interface::run_compiler::<(), rustc_driver_impl[a0a5e0825e3525c3]::run_compiler::{closure#0}>::{closure#1}
  54:     0x7f13d11f653e - std[fec5da352f41df64]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[b660c6c936fc555e]::util::run_in_thread_with_globals<rustc_interface[b660c6c936fc555e]::util::run_in_thread_pool_with_globals<rustc_interface[b660c6c936fc555e]::interface::run_compiler<(), rustc_driver_impl[a0a5e0825e3525c3]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  55:     0x7f13d11f6ba0 - <std[fec5da352f41df64]::thread::lifecycle::spawn_unchecked<rustc_interface[b660c6c936fc555e]::util::run_in_thread_with_globals<rustc_interface[b660c6c936fc555e]::util::run_in_thread_pool_with_globals<rustc_interface[b660c6c936fc555e]::interface::run_compiler<(), rustc_driver_impl[a0a5e0825e3525c3]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[655421083fc59668]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  56:     0x7f13d11f7a2c - <std[fec5da352f41df64]::sys::thread::unix::Thread>::new::thread_start
  57:     0x7f13caca598b - <unknown>
  58:     0x7f13cad29a0c - <unknown>
  59:                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: please attach the file at `/tmp/im/rustc-ice-2026-02-28T20_48_10-2095315.txt` to your bug report

note: rustc 1.96.0-nightly (ba1567989 2026-02-28) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `main::{constant#0}`
#1 [eval_to_const_value_raw] simplifying constant for the type system `main::{constant#0}`
  |
  = note: this failure-note originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
#2 [promoted_mir] optimizing promoted MIR for `main`
#3 [eval_to_allocation_raw] const-evaluating + checking `main::promoted[1]`
#4 [eval_to_const_value_raw] simplifying constant for the type system `main::promoted[1]`
#5 [mir_drops_elaborated_and_const_checked] elaborating drops for `main`
#6 [analysis] running analysis passes on crate `b`
end of query stack
error: aborting due to 1 previous error; 1 warning emitted

@rustbot label +F-field_projections

Activity

  1. added
    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.
    C-bugCategory: This is a bug.
    on Feb 28, 2026
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 28, 2026
  3. matthiaskrgr commented on Mar 2, 2026

    @matthiaskrgr
    MemberAuthor

    seems to trigger without field projections as well:

    use std::mem::offset_of;
    
    struct S {
    	x: ()
    }
    
    impl S {
        fn a() {
            offset_of!(Self, Self::x);
        }
    }
    
    fn main() {}

    bisects to #148151

  4. self-assigned this
    on Mar 3, 2026
  5. added 2 commits that reference this issue on Mar 6, 2026
    798ceb2
    ac9bd09
  6. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Mar 6, 2026
  7. added a commit that references this issue on Mar 6, 2026
    58e257e
  8. added a commit that references this issue on Mar 9, 2026
    bc18456
  9. added a commit that references this issue on Aug 7, 2026
    f645dbe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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