Skip to content

ICE: ImmTy { imm: Scalar(alloc1), ty: *const dyn Sync } input to a fat-to-thin cast (*const dyn Sync -> *const usize / expected wide pointer extra data (e.g. slice length or trait object vtable) #121413

Closed
@matthiaskrgr

Description

@matthiaskrgr

auto-reduced (treereduce-rust):

#![feature(const_refs_to_static)]
const REF_INTERIOR_MUT: &usize = {
    static FOO: Sync = AtomicUsize::new(0);
    unsafe { &*(&FOO as *const _ as *const usize) }
};
original code

original:

//@ compile-flags: -Zunleash-the-miri-inside-of-you
//@ normalize-stderr-test "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
//@ normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> "HEX_DUMP"

use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;

const MUTATE_INTERIOR_MUT: usize = {
    static FOO: AtomicUsize = EMPTY_SLICE.as_ptr().guaranteed_eq(&[] as *const _);
    FOO.fetch_add(1, Ordering::Relaxed) //~ERROR evaluation of constant value failed
};

const READ_INTERIOR_MUT: usize = {
    static FOO: AtomicUsize = AtomicUsize::new(0);
    unsafe { *(&FOO as *const _ as *const usize) } // These pass the lifetime checks because of the "tail expression" / "outer scope" rule.
};

static mut MUTABLE: u32 = 0;
const READ_MUT: u32 = unsafe { MUTABLE }; //~ERROR evaluation of constant value failed

// Evaluating this does not read anything mutable, but validation does, so this should error.
const REF_INTERIOR_MUT: &usize = { //~ ERROR undefined behavior
    //~| encountered reference to mutable memory
    static FOO: Sync = AtomicUsize::new(0);
    unsafe { &*(&FOO as *const _ as *const usize) }
};

// Not actually reading from anything mutable, so these are fine.
static MY_STATIC: u8 = 4;
const REF_IMMUT: &u8 = &MY_STATIC;
const READ_IMMUT: u8 = *REF_IMMUT;

fn main() {}

Version information

rustc 1.78.0-nightly (f8131a48a 2024-02-21)
binary: rustc
commit-hash: f8131a48a46ac3bc8a3d0fe0477055b132cffdc3
commit-date: 2024-02-21
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc -Zcrate-attr=feature(const_refs_to_static)

Program output

error[E0433]: failed to resolve: use of undeclared type `AtomicUsize`
 --> /tmp/icemaker_global_tempdir.d8C7FanUdfBa/rustc_testrunner_tmpdir_reporting.T2pxA13oISa0/mvce.rs:2:24
  |
2 |     static FOO: Sync = AtomicUsize::new(0);
  |                        ^^^^^^^^^^^ use of undeclared type `AtomicUsize`
  |
help: consider importing this struct
  |
1 + use std::sync::atomic::AtomicUsize;
  |

error[E0601]: `main` function not found in crate `mvce`
 --> /tmp/icemaker_global_tempdir.d8C7FanUdfBa/rustc_testrunner_tmpdir_reporting.T2pxA13oISa0/mvce.rs:4:3
  |
4 | };
  |   ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.d8C7FanUdfBa/rustc_testrunner_tmpdir_reporting.T2pxA13oISa0/mvce.rs`

warning: trait objects without an explicit `dyn` are deprecated
 --> /tmp/icemaker_global_tempdir.d8C7FanUdfBa/rustc_testrunner_tmpdir_reporting.T2pxA13oISa0/mvce.rs:2:17
  |
2 |     static FOO: Sync = AtomicUsize::new(0);
  |                 ^^^^
  |
  = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
  = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
  = note: `#[warn(bare_trait_objects)]` on by default
help: if this is an object-safe trait, use `dyn`
  |
2 |     static FOO: dyn Sync = AtomicUsize::new(0);
  |                 +++

error[E0277]: the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
 --> /tmp/icemaker_global_tempdir.d8C7FanUdfBa/rustc_testrunner_tmpdir_reporting.T2pxA13oISa0/mvce.rs:2:17
  |
2 |     static FOO: Sync = AtomicUsize::new(0);
  |                 ^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn Sync + 'static)`

error[E0277]: the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
 --> /tmp/icemaker_global_tempdir.d8C7FanUdfBa/rustc_testrunner_tmpdir_reporting.T2pxA13oISa0/mvce.rs:2:24
  |
2 |     static FOO: Sync = AtomicUsize::new(0);
  |                        ^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn Sync + 'static)`
  = note: constant expressions must have a statically known size

error: internal compiler error: compiler/rustc_const_eval/src/interpret/cast.rs:215:42: ImmTy { imm: Scalar(alloc1), ty: *const dyn Sync } input to a fat-to-thin cast (*const dyn Sync -> *const usize)
 --> /tmp/icemaker_global_tempdir.d8C7FanUdfBa/rustc_testrunner_tmpdir_reporting.T2pxA13oISa0/mvce.rs:3:16
  |
3 |     unsafe { &*(&FOO as *const _ as *const usize) }
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

thread 'rustc' panicked at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/compiler/rustc_middle/src/util/bug.rs:34:50:
Box<dyn Any>
stack backtrace:
   0:     0x7fc94378caf6 - std::backtrace_rs::backtrace::libunwind::trace::hbec2edc838b433e2
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x7fc94378caf6 - std::backtrace_rs::backtrace::trace_unsynchronized::h88c30257ee0dd166
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7fc94378caf6 - std::sys_common::backtrace::_print_fmt::h9176654d509e3088
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7fc94378caf6 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h671feaee1776bf19
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7fc9437dda7c - core::fmt::rt::Argument::fmt::h59605c83ae32d935
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/core/src/fmt/rt.rs:142:9
   5:     0x7fc9437dda7c - core::fmt::write::hf7af8dfcfd20db5b
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/core/src/fmt/mod.rs:1120:17
   6:     0x7fc9437814bf - std::io::Write::write_fmt::h92a5dbf489025e71
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/io/mod.rs:1846:15
   7:     0x7fc94378c8a4 - std::sys_common::backtrace::_print::h00080302d23998ae
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7fc94378c8a4 - std::sys_common::backtrace::print::h6b23124445400245
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7fc94378f5eb - std::panicking::default_hook::{{closure}}::hd67b2f9d36675a28
  10:     0x7fc94378f339 - std::panicking::default_hook::h10a39ef11c714e42
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/panicking.rs:292:9
  11:     0x7fc94667a94c - std[5a8e7f94216a9138]::panicking::update_hook::<alloc[e14ab1f516d287ab]::boxed::Box<rustc_driver_impl[e279e4f1fedd9109]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7fc94378fd50 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h37ba37dc48823c1d
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/alloc/src/boxed.rs:2030:9
  13:     0x7fc94378fd50 - std::panicking::rust_panic_with_hook::he3e3bc7c345bf54b
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/panicking.rs:786:13
  14:     0x7fc9466a7524 - std[5a8e7f94216a9138]::panicking::begin_panic::<rustc_errors[34364e5c712fab73]::ExplicitBug>::{closure#0}
  15:     0x7fc9466a4176 - std[5a8e7f94216a9138]::sys_common::backtrace::__rust_end_short_backtrace::<std[5a8e7f94216a9138]::panicking::begin_panic<rustc_errors[34364e5c712fab73]::ExplicitBug>::{closure#0}, !>
  16:     0x7fc94669f936 - std[5a8e7f94216a9138]::panicking::begin_panic::<rustc_errors[34364e5c712fab73]::ExplicitBug>
  17:     0x7fc945017631 - <rustc_errors[34364e5c712fab73]::diagnostic::BugAbort as rustc_errors[34364e5c712fab73]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  18:     0x7fc946601f60 - rustc_middle[446dd094ad85939e]::util::bug::opt_span_bug_fmt::<rustc_span[adb133ac03280fc1]::span_encoding::Span>::{closure#0}
  19:     0x7fc94660217a - rustc_middle[446dd094ad85939e]::ty::context::tls::with_opt::<rustc_middle[446dd094ad85939e]::util::bug::opt_span_bug_fmt<rustc_span[adb133ac03280fc1]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  20:     0x7fc9465f8e48 - rustc_middle[446dd094ad85939e]::ty::context::tls::with_context_opt::<rustc_middle[446dd094ad85939e]::ty::context::tls::with_opt<rustc_middle[446dd094ad85939e]::util::bug::opt_span_bug_fmt<rustc_span[adb133ac03280fc1]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  21:     0x7fc945c50f54 - rustc_middle[446dd094ad85939e]::util::bug::span_bug_fmt::<rustc_span[adb133ac03280fc1]::span_encoding::Span>
  22:     0x7fc94891a7e0 - rustc_const_eval[b7e9a6566b8f5ff6]::const_eval::eval_queries::eval_body_using_ecx::{closure#0}
  23:     0x7fc948876f23 - rustc_const_eval[b7e9a6566b8f5ff6]::const_eval::eval_queries::eval_in_interpreter
  24:     0x7fc947dfa938 - rustc_const_eval[b7e9a6566b8f5ff6]::const_eval::eval_queries::eval_to_allocation_raw_provider
  25:     0x7fc947dfa706 - rustc_query_impl[9d4c51777e2bb749]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9d4c51777e2bb749]::query_impl::eval_to_allocation_raw::dynamic_query::{closure#2}::{closure#0}, rustc_middle[446dd094ad85939e]::query::erase::Erased<[u8; 24usize]>>
  26:     0x7fc94840ba51 - rustc_query_system[9b2ccfe41ae3bb1b]::query::plumbing::try_execute_query::<rustc_query_impl[9d4c51777e2bb749]::DynamicConfig<rustc_query_system[9b2ccfe41ae3bb1b]::query::caches::DefaultCache<rustc_middle[446dd094ad85939e]::ty::ParamEnvAnd<rustc_middle[446dd094ad85939e]::mir::interpret::GlobalId>, rustc_middle[446dd094ad85939e]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[9d4c51777e2bb749]::plumbing::QueryCtxt, false>
  27:     0x7fc94840b62c - rustc_query_impl[9d4c51777e2bb749]::query_impl::eval_to_allocation_raw::get_query_non_incr::__rust_end_short_backtrace
  28:     0x7fc947dfb60d - rustc_const_eval[b7e9a6566b8f5ff6]::const_eval::eval_queries::eval_to_const_value_raw_provider
  29:     0x7fc947dfb40c - rustc_query_impl[9d4c51777e2bb749]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9d4c51777e2bb749]::query_impl::eval_to_const_value_raw::dynamic_query::{closure#2}::{closure#0}, rustc_middle[446dd094ad85939e]::query::erase::Erased<[u8; 24usize]>>
  30:     0x7fc94840ba14 - rustc_query_system[9b2ccfe41ae3bb1b]::query::plumbing::try_execute_query::<rustc_query_impl[9d4c51777e2bb749]::DynamicConfig<rustc_query_system[9b2ccfe41ae3bb1b]::query::caches::DefaultCache<rustc_middle[446dd094ad85939e]::ty::ParamEnvAnd<rustc_middle[446dd094ad85939e]::mir::interpret::GlobalId>, rustc_middle[446dd094ad85939e]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[9d4c51777e2bb749]::plumbing::QueryCtxt, false>
  31:     0x7fc94840b530 - rustc_query_impl[9d4c51777e2bb749]::query_impl::eval_to_const_value_raw::get_query_non_incr::__rust_end_short_backtrace
  32:     0x7fc9483afe6b - <rustc_middle[446dd094ad85939e]::query::plumbing::TyCtxtEnsure>::const_eval_poly
  33:     0x7fc9483adbd3 - rustc_hir_analysis[71d4294717f7f2b7]::check_crate
  34:     0x7fc9484a2dd3 - rustc_interface[7ca501d7f8f3a34f]::passes::analysis
  35:     0x7fc9484a2a29 - rustc_query_impl[9d4c51777e2bb749]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9d4c51777e2bb749]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[446dd094ad85939e]::query::erase::Erased<[u8; 1usize]>>
  36:     0x7fc94873d4a5 - rustc_query_system[9b2ccfe41ae3bb1b]::query::plumbing::try_execute_query::<rustc_query_impl[9d4c51777e2bb749]::DynamicConfig<rustc_query_system[9b2ccfe41ae3bb1b]::query::caches::SingleCache<rustc_middle[446dd094ad85939e]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[9d4c51777e2bb749]::plumbing::QueryCtxt, false>
  37:     0x7fc94873d209 - rustc_query_impl[9d4c51777e2bb749]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  38:     0x7fc94874c719 - rustc_interface[7ca501d7f8f3a34f]::interface::run_compiler::<core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>, rustc_driver_impl[e279e4f1fedd9109]::run_compiler::{closure#0}>::{closure#0}
  39:     0x7fc9489be705 - std[5a8e7f94216a9138]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[7ca501d7f8f3a34f]::util::run_in_thread_with_globals<rustc_interface[7ca501d7f8f3a34f]::util::run_in_thread_pool_with_globals<rustc_interface[7ca501d7f8f3a34f]::interface::run_compiler<core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>, rustc_driver_impl[e279e4f1fedd9109]::run_compiler::{closure#0}>::{closure#0}, core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>>::{closure#0}, core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>>
  40:     0x7fc9489be532 - <<std[5a8e7f94216a9138]::thread::Builder>::spawn_unchecked_<rustc_interface[7ca501d7f8f3a34f]::util::run_in_thread_with_globals<rustc_interface[7ca501d7f8f3a34f]::util::run_in_thread_pool_with_globals<rustc_interface[7ca501d7f8f3a34f]::interface::run_compiler<core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>, rustc_driver_impl[e279e4f1fedd9109]::run_compiler::{closure#0}>::{closure#0}, core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>>::{closure#0}, core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[85b4fd477ce5bb21]::result::Result<(), rustc_span[adb133ac03280fc1]::ErrorGuaranteed>>::{closure#1} as core[85b4fd477ce5bb21]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  41:     0x7fc943799725 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h1bff3efb61c3fa6b
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/alloc/src/boxed.rs:2016:9
  42:     0x7fc943799725 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h83141cf549dced8a
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/alloc/src/boxed.rs:2016:9
  43:     0x7fc943799725 - std::sys::pal::unix::thread::Thread::new::thread_start::hbe9287fdd7c325bc
                               at /rustc/f8131a48a46ac3bc8a3d0fe0477055b132cffdc3/library/std/src/sys/pal/unix/thread.rs:108:17
  44:     0x7fc9435819eb - <unknown>
  45:     0x7fc9436057cc - <unknown>
  46:                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.78.0-nightly (f8131a48a 2024-02-21) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z crate-attr=feature(const_refs_to_static) -Z dump-mir-dir=dir

query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `REF_INTERIOR_MUT`
#1 [eval_to_const_value_raw] simplifying constant for the type system `REF_INTERIOR_MUT`
#2 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 5 previous errors; 1 warning emitted

Some errors have detailed explanations: E0277, E0433, E0601.
For more information about an error, try `rustc --explain E0277`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.F-const_refs_to_static`#![feature(const_refs_to_static)]`I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-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