Repository: rust
Issue: ICE: `write_immediate_to_mplace: invalid Scalar layout: TyAndLayout`
Labels: I-ICE,T-compiler,C-bug,S-bug-has-test

<!--
[31mICE[0m: Rustc ./a.rs '-Zmir-opt-level=5 -Zvalidate-mir -ooutputfile -Zdump-mir-dir=dir' 'error: internal compiler error: /rustc/fbce03b195c02e425fbb12276b8f02349048a75f/compiler/rustc_const_eval/src/interpret/place.rs:693:21: write_immediate_to_mplace: invalid Scalar layout: TyAndLayout {', 'error: internal compiler error: /rustc/fbce03b195c02e425fbb12276b8f02349048a75f/compiler/rustc_const_eval/src/interpret/place.rs:693:21: write_immediate_to_mplace: invalid Scalar layout: TyAndLayout {'
File: /tmp/im/a.rs
-->

auto-reduced (treereduce-rust):
````rust
extern "C" {
    pub static mut symbol: [i8];

}

fn main() {
    println!("C", unsafe { &symbol });
}


````

original:
````rust
extern "C" {
    pub static mut symbol: [i8];
    //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs]
}

fn main() {
    println!("C", unsafe { &symbol });
    //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs]
}

````

Version information
````
rustc 1.82.0-nightly (fbce03b19 2024-08-14)
binary: rustc
commit-hash: fbce03b195c02e425fbb12276b8f02349048a75f
commit-date: 2024-08-14
host: x86_64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 19.1.0

````

Command:
`/home/matthias/.rustup/toolchains/master/bin/rustc -Zmir-opt-level=5 -Zvalidate-mir`

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary><strong>Program output</strong></summary>
<p>

```
error: argument never used
 --> /tmp/icemaker_global_tempdir.gKK7s1KquQAJ/rustc_testrunner_tmpdir_reporting.JRe5Vq1I3rjY/mvce.rs:7:19
  |
7 |     println!("C", unsafe { &symbol });
  |              ---  ^^^^^^^^^^^^^^^^^^ argument never used
  |              |
  |              formatting specifier missing

error[E0277]: the size for values of type `[i8]` cannot be known at compilation time
 --> /tmp/icemaker_global_tempdir.gKK7s1KquQAJ/rustc_testrunner_tmpdir_reporting.JRe5Vq1I3rjY/mvce.rs:2:28
  |
2 |     pub static mut symbol: [i8];
  |                            ^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[i8]`

warning: creating a shared reference to mutable static is discouraged
 --> /tmp/icemaker_global_tempdir.gKK7s1KquQAJ/rustc_testrunner_tmpdir_reporting.JRe5Vq1I3rjY/mvce.rs:7:28
  |
7 |     println!("C", unsafe { &symbol });
  |                            ^^^^^^^ shared reference to mutable static
  |
  = note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
  = note: this will be a hard error in the 2024 edition
  = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior
  = note: `#[warn(static_mut_refs)]` on by default
help: use `addr_of!` instead to create a raw pointer
  |
7 |     println!("C", unsafe { addr_of!(symbol) });
  |                            ~~~~~~~~~      +

error: internal compiler error: /rustc/fbce03b195c02e425fbb12276b8f02349048a75f/compiler/rustc_const_eval/src/interpret/place.rs:693:21: write_immediate_to_mplace: invalid Scalar layout: TyAndLayout {
                                    ty: &[i8],
                                    layout: Layout {
                                        size: Size(16 bytes),
                                        align: AbiAndPrefAlign {
                                            abi: Align(8 bytes),
                                            pref: Align(8 bytes),
                                        },
                                        abi: ScalarPair(
                                            Initialized {
                                                value: Pointer(
                                                    AddressSpace(
                                                        0,
                                                    ),
                                                ),
                                                valid_range: 1..=18446744073709551615,
                                            },
                                            Initialized {
                                                value: Int(
                                                    I64,
                                                    false,
                                                ),
                                                valid_range: 0..=18446744073709551615,
                                            },
                                        ),
                                        fields: Arbitrary {
                                            offsets: [
                                                Size(0 bytes),
                                                Size(8 bytes),
                                            ],
                                            memory_index: [
                                                0,
                                                1,
                                            ],
                                        },
                                        largest_niche: Some(
                                            Niche {
                                                offset: Size(0 bytes),
                                                value: Pointer(
                                                    AddressSpace(
                                                        0,
                                                    ),
                                                ),
                                                valid_range: 1..=18446744073709551615,
                                            },
                                        ),
                                        variants: Single {
                                            index: 0,
                                        },
                                        max_repr_align: None,
                                        unadjusted_abi_align: Align(8 bytes),
                                    },
                                }

thread 'rustc' panicked at /rustc/fbce03b195c02e425fbb12276b8f02349048a75f/compiler/rustc_const_eval/src/interpret/place.rs:693:21:
Box<dyn Any>
stack backtrace:
   0:     0x7dabe2fb666d - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h560b4d28c137b093
   1:     0x7dabe3804f2f - core::fmt::write::h99766545c4efef9d
   2:     0x7dabe47b0ed1 - std::io::Write::write_fmt::h8e3cbf6208612263
   3:     0x7dabe2fb8d4b - std::panicking::default_hook::{{closure}}::h6bf6ccd32e88a7b7
   4:     0x7dabe2fb89be - std::panicking::default_hook::ha56d0025017107a4
   5:     0x7dabe2146299 - std[265a0665abe5e043]::panicking::update_hook::<alloc[f7eab8ff756c9dca]::boxed::Box<rustc_driver_impl[18c1de8e11281817]::install_ice_hook::{closure#0}>>::{closure#0}
   6:     0x7dabe2fb9667 - std::panicking::rust_panic_with_hook::h53b891e816ad5807
   7:     0x7dabe2180b41 - std[265a0665abe5e043]::panicking::begin_panic::<rustc_errors[99e98b0bbb24b16c]::ExplicitBug>::{closure#0}
   8:     0x7dabe2173d26 - std[265a0665abe5e043]::sys::backtrace::__rust_end_short_backtrace::<std[265a0665abe5e043]::panicking::begin_panic<rustc_errors[99e98b0bbb24b16c]::ExplicitBug>::{closure#0}, !>
   9:     0x7dabe2173aa6 - std[265a0665abe5e043]::panicking::begin_panic::<rustc_errors[99e98b0bbb24b16c]::ExplicitBug>
  10:     0x7dabe2189cc1 - <rustc_errors[99e98b0bbb24b16c]::diagnostic::BugAbort as rustc_errors[99e98b0bbb24b16c]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  11:     0x7dabe283efcd - <rustc_errors[99e98b0bbb24b16c]::DiagCtxtHandle>::span_bug::<rustc_span[e524fe640245e945]::span_encoding::Span, alloc[f7eab8ff756c9dca]::string::String>
  12:     0x7dabe28719f8 - rustc_middle[d7f4792719c666e4]::util::bug::opt_span_bug_fmt::<rustc_span[e524fe640245e945]::span_encoding::Span>::{closure#0}
  13:     0x7dabe2871a2a - rustc_middle[d7f4792719c666e4]::ty::context::tls::with_opt::<rustc_middle[d7f4792719c666e4]::util::bug::opt_span_bug_fmt<rustc_span[e524fe640245e945]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  14:     0x7dabe285dd0b - rustc_middle[d7f4792719c666e4]::ty::context::tls::with_context_opt::<rustc_middle[d7f4792719c666e4]::ty::context::tls::with_opt<rustc_middle[d7f4792719c666e4]::util::bug::opt_span_bug_fmt<rustc_span[e524fe640245e945]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  15:     0x7dabe285cb27 - rustc_middle[d7f4792719c666e4]::util::bug::span_bug_fmt::<rustc_span[e524fe640245e945]::span_encoding::Span>
  16:     0x7dabe42a8335 - <rustc_const_eval[4703571d7d15c956]::interpret::eval_context::InterpCx<rustc_const_eval[4703571d7d15c956]::const_eval::dummy_machine::DummyMachine>>::write_immediate_to_mplace_no_validate
  17:     0x7dabe1324a74 - <rustc_mir_transform[4b24bac2940eeb7a]::gvn::GVN as rustc_middle[d7f4792719c666e4]::mir::MirPass>::run_pass
  18:     0x7dabe3802151 - rustc_mir_transform[4b24bac2940eeb7a]::pass_manager::run_passes_inner
  19:     0x7dabe432ccb3 - rustc_mir_transform[4b24bac2940eeb7a]::optimized_mir
  20:     0x7dabe435039b - rustc_query_impl[c2f5f95cecf69337]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[c2f5f95cecf69337]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[d7f4792719c666e4]::query::erase::Erased<[u8; 8usize]>>
  21:     0x7dabe382af27 - rustc_query_system[f8c10878fe801c76]::query::plumbing::try_execute_query::<rustc_query_impl[c2f5f95cecf69337]::DynamicConfig<rustc_query_system[f8c10878fe801c76]::query::caches::DefIdCache<rustc_middle[d7f4792719c666e4]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[c2f5f95cecf69337]::plumbing::QueryCtxt, false>
  22:     0x7dabe382a4df - rustc_query_impl[c2f5f95cecf69337]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
  23:     0x7dabe0184814 - <rustc_middle[d7f4792719c666e4]::ty::context::TyCtxt>::instance_mir
  24:     0x7dabe3bb63f9 - rustc_interface[8b2190a255f69c87]::passes::run_required_analyses
  25:     0x7dabe43663de - rustc_interface[8b2190a255f69c87]::passes::analysis
  26:     0x7dabe43663b1 - rustc_query_impl[c2f5f95cecf69337]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[c2f5f95cecf69337]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[d7f4792719c666e4]::query::erase::Erased<[u8; 1usize]>>
  27:     0x7dabe47706ee - rustc_query_system[f8c10878fe801c76]::query::plumbing::try_execute_query::<rustc_query_impl[c2f5f95cecf69337]::DynamicConfig<rustc_query_system[f8c10878fe801c76]::query::caches::SingleCache<rustc_middle[d7f4792719c666e4]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[c2f5f95cecf69337]::plumbing::QueryCtxt, false>
  28:     0x7dabe477044f - rustc_query_impl[c2f5f95cecf69337]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  29:     0x7dabe45dd169 - rustc_interface[8b2190a255f69c87]::interface::run_compiler::<core[12164080e42249fc]::result::Result<(), rustc_span[e524fe640245e945]::ErrorGuaranteed>, rustc_driver_impl[18c1de8e11281817]::run_compiler::{closure#0}>::{closure#1}
  30:     0x7dabe45025d0 - std[265a0665abe5e043]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[8b2190a255f69c87]::util::run_in_thread_with_globals<rustc_interface[8b2190a255f69c87]::util::run_in_thread_pool_with_globals<rustc_interface[8b2190a255f69c87]::interface::run_compiler<core[12164080e42249fc]::result::Result<(), rustc_span[e524fe640245e945]::ErrorGuaranteed>, rustc_driver_impl[18c1de8e11281817]::run_compiler::{closure#0}>::{closure#1}, core[12164080e42249fc]::result::Result<(), rustc_span[e524fe640245e945]::ErrorGuaranteed>>::{closure#0}, core[12164080e42249fc]::result::Result<(), rustc_span[e524fe640245e945]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[12164080e42249fc]::result::Result<(), rustc_span[e524fe640245e945]::ErrorGuaranteed>>
  31:     0x7dabe4502c3a - <<std[265a0665abe5e043]::thread::Builder>::spawn_unchecked_<rustc_interface[8b2190a255f69c87]::util::run_in_thread_with_globals<rustc_interface[8b2190a255f69c87]::util::run_in_thread_pool_with_globals<rustc_interface[8b2190a255f69c87]::interface::run_compiler<core[12164080e42249fc]::result::Result<(), rustc_span[e524fe640245e945]::ErrorGuaranteed>, rustc_driver_impl[18c1de8e11281817]::run_compiler::{closure#0}>::{closure#1}, core[12164080e42249fc]::result::Result<(), rustc_span[e524fe640245e945]::ErrorGuaranteed>>::{closure#0}, core[12164080e42249fc]::result::Result<(), rustc_span[e524fe640245e945]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[12164080e42249fc]::result::Result<(), rustc_span[e524fe640245e945]::ErrorGuaranteed>>::{closure#1} as core[12164080e42249fc]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  32:     0x7dabe4502fab - std::sys::pal::unix::thread::Thread::new::thread_start::hbf34cdaead1142d4
  33:     0x7dabe5dab39d - <unknown>
  34:     0x7dabe5e3049c - <unknown>
  35:                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: rustc 1.82.0-nightly (fbce03b19 2024-08-14) 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 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 3 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0277`.

```

</p>
</details>

<!--
query stack:
#0 [optimized_mir] optimizing MIR for `main`
#1 [analysis] running analysis passes on this crate

-->