Skip to content

ICE: You can't project to field 0 of DynMetadata because layout is weird and thinks it doesn't have fields. #125506

Closed
@matthiaskrgr

Description

@matthiaskrgr
Member

auto-reduced (treereduce-rust):

#![feature(ptr_metadata)]

trait Foo {}

struct Bar;

impl Foo for Bar {}

fn main() {
    let a: *mut dyn Foo = &mut Bar;

    let d = a.to_raw_parts().0 as usize;
}

original:

// run-pass

#![feature(ptr_metadata)]

trait Foo {
    fn foo(&self) {}
}

struct Bar;

impl Foo for Bar {}

fn main() {
    // Test we can turn a fat pointer to array back into a thin pointer.
    let a: *const [i32] = &[1, 2, 3];
    let b = a as *const [i32; 2];
    unsafe {
        assert_eq!(*b, [1, 2]);
    }

    // Test conversion to an address (usize).
    let a: *const [i32; 3] = &[1, 2, 3];
    let b: *const [i32] = a;
    assert_eq!(a as usize, b as *const () as usize);

    // And conversion to a void pointer/address for trait objects too.
    let a: *mut dyn Foo = &mut Bar;
    let b = a as *mut () as usize;
    let c = a as *const () as usize;
    let d = a.to_raw_parts().0 as usize;

    assert_eq!(b, d);
    assert_eq!(c, d);
}

Version information

rustc 1.80.0-nightly (213ad10c8 2024-05-24)
binary: rustc
commit-hash: 213ad10c8f0fc275648552366275dc4e07f97462
commit-date: 2024-05-24
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.6

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc -Zmir-opt-level=5 -Zvalidate-mir

Program output

warning: unused variable: `d`
  --> /tmp/icemaker_global_tempdir.MOmdI3aUIa6V/rustc_testrunner_tmpdir_reporting.HOhhbRhhlSye/mvce.rs:12:9
   |
12 |     let d = a.to_raw_parts().0 as usize;
   |         ^ help: if this is intentional, prefix it with an underscore: `_d`
   |
   = note: `#[warn(unused_variables)]` on by default

thread 'rustc' panicked at compiler/rustc_const_eval/src/transform/validate.rs:90:25:
broken MIR in Item(DefId(0:7 ~ mvce[a8d7]::main)) (after pass ScalarReplacementOfAggregates) at bb0[6]:
You can't project to field 0 of `DynMetadata` because layout is weird and thinks it doesn't have fields.
stack backtrace:
   0:     0x7d28bd152a15 - std::backtrace_rs::backtrace::libunwind::trace::ha14a3c487f1e072b
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x7d28bd152a15 - std::backtrace_rs::backtrace::trace_unsynchronized::hf4889a75ccd505ee
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7d28bd152a15 - std::sys_common::backtrace::_print_fmt::h9590c8d63b078677
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7d28bd152a15 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hd126fab93aa88efe
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7d28bd1a1aeb - core::fmt::rt::Argument::fmt::hf17f29f90df880a1
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/core/src/fmt/rt.rs:165:63
   5:     0x7d28bd1a1aeb - core::fmt::write::h0ebd95f36610e669
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/core/src/fmt/mod.rs:1172:21
   6:     0x7d28bd14776f - std::io::Write::write_fmt::h3eef362c47ded0c4
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/std/src/io/mod.rs:1835:15
   7:     0x7d28bd1527ee - std::sys_common::backtrace::_print::h6ff81878b57bf5c8
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7d28bd1527ee - std::sys_common::backtrace::print::hdc5e68f22314f4a9
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7d28bd155219 - std::panicking::default_hook::{{closure}}::h03d46e664671b447
  10:     0x7d28bd154fba - std::panicking::default_hook::h1ae40dc0fea72a33
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/std/src/panicking.rs:298:9
  11:     0x7d28b9b1709f - std[b9bb1320dc5ae1a4]::panicking::update_hook::<alloc[b0def3f22af53808]::boxed::Box<rustc_driver_impl[9effea81b954d024]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7d28bd15594b - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h00416b40e105786e
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/alloc/src/boxed.rs:2077:9
  13:     0x7d28bd15594b - std::panicking::rust_panic_with_hook::h7af4ff1dc1e8071b
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/std/src/panicking.rs:799:13
  14:     0x7d28bd1556c4 - std::panicking::begin_panic_handler::{{closure}}::heb4d90acdc12b443
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/std/src/panicking.rs:664:13
  15:     0x7d28bd152ed9 - std::sys_common::backtrace::__rust_end_short_backtrace::h533bec95ab2736b2
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x7d28bd1553f7 - rust_begin_unwind
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/std/src/panicking.rs:652:5
  17:     0x7d28bd19e0b3 - core::panicking::panic_fmt::h71bc10306699fad0
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/core/src/panicking.rs:72:14
  18:     0x7d28b8ed02b9 - <rustc_const_eval[9b69744695b0afef]::transform::validate::CfgChecker>::fail::<alloc[b0def3f22af53808]::string::String>
  19:     0x7d28b8ecef04 - <rustc_const_eval[9b69744695b0afef]::transform::validate::Validator as rustc_middle[c8c87ebb1fd33f9c]::mir::MirPass>::run_pass
  20:     0x7d28b9546940 - rustc_mir_transform[250cc2f4a89ad9b0]::pass_manager::validate_body
  21:     0x7d28bae01d45 - rustc_mir_transform[250cc2f4a89ad9b0]::pass_manager::run_passes_inner
  22:     0x7d28bb914b26 - rustc_mir_transform[250cc2f4a89ad9b0]::optimized_mir
  23:     0x7d28bb913c6b - rustc_query_impl[91ea4ef0d9b42432]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[91ea4ef0d9b42432]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c8c87ebb1fd33f9c]::query::erase::Erased<[u8; 8usize]>>
  24:     0x7d28baf91da7 - rustc_query_system[442df03e8028498c]::query::plumbing::try_execute_query::<rustc_query_impl[91ea4ef0d9b42432]::DynamicConfig<rustc_query_system[442df03e8028498c]::query::caches::DefIdCache<rustc_middle[c8c87ebb1fd33f9c]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[91ea4ef0d9b42432]::plumbing::QueryCtxt, false>
  25:     0x7d28baf915a0 - rustc_query_impl[91ea4ef0d9b42432]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
  26:     0x7d28baf995b7 - <rustc_middle[c8c87ebb1fd33f9c]::ty::context::TyCtxt>::instance_mir
  27:     0x7d28bbb8e29a - rustc_monomorphize[5c6307a2377ff607]::collector::collect_items_rec::{closure#0}
  28:     0x7d28baf6208d - rustc_monomorphize[5c6307a2377ff607]::collector::collect_items_rec
  29:     0x7d28bb1cb391 - rustc_monomorphize[5c6307a2377ff607]::partitioning::collect_and_partition_mono_items
  30:     0x7d28bb1ca9d6 - rustc_query_impl[91ea4ef0d9b42432]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[91ea4ef0d9b42432]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c8c87ebb1fd33f9c]::query::erase::Erased<[u8; 24usize]>>
  31:     0x7d28bb1ca9a9 - <rustc_query_impl[91ea4ef0d9b42432]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2} as core[a480ab4ddb292070]::ops::function::FnOnce<(rustc_middle[c8c87ebb1fd33f9c]::ty::context::TyCtxt, ())>>::call_once
  32:     0x7d28bbc70fc8 - rustc_query_system[442df03e8028498c]::query::plumbing::try_execute_query::<rustc_query_impl[91ea4ef0d9b42432]::DynamicConfig<rustc_query_system[442df03e8028498c]::query::caches::SingleCache<rustc_middle[c8c87ebb1fd33f9c]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[91ea4ef0d9b42432]::plumbing::QueryCtxt, false>
  33:     0x7d28bbc70ce1 - rustc_query_impl[91ea4ef0d9b42432]::query_impl::collect_and_partition_mono_items::get_query_non_incr::__rust_end_short_backtrace
  34:     0x7d28bbc35c94 - rustc_codegen_ssa[786e474d3d8fe57a]::base::codegen_crate::<rustc_codegen_llvm[b358019a521b9791]::LlvmCodegenBackend>
  35:     0x7d28bbc296cc - <rustc_codegen_llvm[b358019a521b9791]::LlvmCodegenBackend as rustc_codegen_ssa[786e474d3d8fe57a]::traits::backend::CodegenBackend>::codegen_crate
  36:     0x7d28bbc2905f - rustc_interface[90b491fb83b45114]::passes::start_codegen
  37:     0x7d28bbc286da - <rustc_interface[90b491fb83b45114]::queries::Queries>::codegen_and_build_linker
  38:     0x7d28bb9c19ee - rustc_interface[90b491fb83b45114]::interface::run_compiler::<core[a480ab4ddb292070]::result::Result<(), rustc_span[c256c11c98aa3170]::ErrorGuaranteed>, rustc_driver_impl[9effea81b954d024]::run_compiler::{closure#0}>::{closure#1}
  39:     0x7d28bb9b5fe7 - std[b9bb1320dc5ae1a4]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[90b491fb83b45114]::util::run_in_thread_with_globals<rustc_interface[90b491fb83b45114]::util::run_in_thread_pool_with_globals<rustc_interface[90b491fb83b45114]::interface::run_compiler<core[a480ab4ddb292070]::result::Result<(), rustc_span[c256c11c98aa3170]::ErrorGuaranteed>, rustc_driver_impl[9effea81b954d024]::run_compiler::{closure#0}>::{closure#1}, core[a480ab4ddb292070]::result::Result<(), rustc_span[c256c11c98aa3170]::ErrorGuaranteed>>::{closure#0}, core[a480ab4ddb292070]::result::Result<(), rustc_span[c256c11c98aa3170]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[a480ab4ddb292070]::result::Result<(), rustc_span[c256c11c98aa3170]::ErrorGuaranteed>>
  40:     0x7d28bb9b5daa - <<std[b9bb1320dc5ae1a4]::thread::Builder>::spawn_unchecked_<rustc_interface[90b491fb83b45114]::util::run_in_thread_with_globals<rustc_interface[90b491fb83b45114]::util::run_in_thread_pool_with_globals<rustc_interface[90b491fb83b45114]::interface::run_compiler<core[a480ab4ddb292070]::result::Result<(), rustc_span[c256c11c98aa3170]::ErrorGuaranteed>, rustc_driver_impl[9effea81b954d024]::run_compiler::{closure#0}>::{closure#1}, core[a480ab4ddb292070]::result::Result<(), rustc_span[c256c11c98aa3170]::ErrorGuaranteed>>::{closure#0}, core[a480ab4ddb292070]::result::Result<(), rustc_span[c256c11c98aa3170]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[a480ab4ddb292070]::result::Result<(), rustc_span[c256c11c98aa3170]::ErrorGuaranteed>>::{closure#2} as core[a480ab4ddb292070]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  41:     0x7d28bd15f72b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h89a54a9f8510c705
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/alloc/src/boxed.rs:2063:9
  42:     0x7d28bd15f72b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h195cf2ba72e01fdb
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/alloc/src/boxed.rs:2063:9
  43:     0x7d28bd15f72b - std::sys::pal::unix::thread::Thread::new::thread_start::h98d3876c0f9ad401
                               at /rustc/213ad10c8f0fc275648552366275dc4e07f97462/library/std/src/sys/pal/unix/thread.rs:108:17
  44:     0x7d28bcefd1cf - <unknown>
  45:     0x7d28bcf7e6ec - <unknown>
  46:                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: rustc 1.80.0-nightly (213ad10c8 2024-05-24) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z mir-opt-level=5 -Z validate-mir -Z dump-mir-dir=dir

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


@rustbot label +F-ptr_metadata

Activity

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 May 24, 2024
added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on May 24, 2024
matthiaskrgr

matthiaskrgr commented on May 24, 2024

@matthiaskrgr
MemberAuthor
scottmcm

scottmcm commented on May 24, 2024

@scottmcm
Member

Thanks!

I have a fix for this in #124251 -- I didn't know it was possible without that change. I'll pull it out to a separate PR.

added a commit that references this issue on May 25, 2024

Rollup merge of rust-lang#125508 - scottmcm:fix-125506, r=Nilstrieb

f9d3571
added a commit that references this issue on May 26, 2024

Rollup merge of rust-lang#125508 - scottmcm:fix-125506, r=Nilstrieb

54c18c3
added a commit that references this issue on May 26, 2024

Rollup merge of rust-lang#125508 - scottmcm:fix-125506, r=Nilstrieb

efa5760
added 3 commits that reference this issue on May 26, 2024

Rollup merge of rust-lang#125508 - scottmcm:fix-125506, r=Nilstrieb

aeb6171

Rollup merge of rust-lang#125508 - scottmcm:fix-125506, r=Nilstrieb

376001d

Rollup merge of rust-lang#125508 - scottmcm:fix-125506, r=Nilstrieb

aba81b5
added a commit that references this issue on May 26, 2024

Auto merge of rust-lang#125508 - scottmcm:fix-125506, r=Nilstrieb

8768a6a
added a commit that references this issue on May 26, 2024

Rollup merge of rust-lang#125508 - scottmcm:fix-125506, r=Nilstrieb

5fef6c5
added a commit that references this issue on May 26, 2024
7545461
removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @matthiaskrgr@saethlin@scottmcm@rustbot

      Issue actions

        ICE: You can't project to field 0 of `DynMetadata` because layout is weird and thinks it doesn't have fields. · Issue #125506 · rust-lang/rust