Repository: rust
Issue: ICE: `failed to get layout for .. ` with mir-opt-level=3, polymorphisation and strict init checks
Labels: I-ICE,T-compiler,C-bug,requires-nightly,-Zpolymorphize

<!--
Thank you for finding an Internal Compiler Error! 🧊  If possible, try to provide
a minimal verifiable example. You can read "Rust Bug Minimization Patterns" for
how to create smaller examples.
http://blog.pnkfx.org/blog/2019/11/18/rust-bug-minimization-patterns/
-->

### Code
`rustc  -Zstrict-init-checks=yes -Zpolymorphize=on  -Zmir-opt-level=3 file.rs`
```Rust
use std::mem::{self};

struct Wrap<T> {
    wrapped: T,
}

fn generic<T: 'static>() {
    unsafe {
        let _val: Wrap<&'static T> = mem::uninitialized();
    }
}

fn main() {}
```


### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```
rustc 1.73.0-nightly (500647fd8 2023-07-27)
binary: rustc
commit-hash: 500647fd8138cc09e87edb08d62f81654fbf6ef8
commit-date: 2023-07-27
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5
```

### Error output

```
<output>
```

<!--
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>Backtrace</strong></summary>
<p>

```
error: internal compiler error: /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/compiler/rustc_middle/src/ty/layout.rs:958:21: failed to get layout for `&T`: Unknown(<T as std::ptr::Pointee>::Metadata),
                                despite it being a field (#0) of an existing layout: TyAndLayout {
                                    ty: Wrap<&T>,
                                    layout: Layout {
                                        size: Size(8 bytes),
                                        align: AbiAndPrefAlign {
                                            abi: Align(8 bytes),
                                            pref: Align(8 bytes),
                                        },
                                        abi: Scalar(
                                            Initialized {
                                                value: Pointer(
                                                    AddressSpace(
                                                        0,
                                                    ),
                                                ),
                                                valid_range: 1..=18446744073709551615,
                                            },
                                        ),
                                        fields: Arbitrary {
                                            offsets: [
                                                Size(0 bytes),
                                            ],
                                            memory_index: [
                                                0,
                                            ],
                                        },
                                        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 'Box<dyn Any>', /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/compiler/rustc_errors/src/lib.rs:1645:9
stack backtrace:
   0:     0x7ff884b62fc1 - std::backtrace_rs::backtrace::libunwind::trace::h757b492de7e38f51
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7ff884b62fc1 - std::backtrace_rs::backtrace::trace_unsynchronized::he91e3190ab8d9c4f
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7ff884b62fc1 - std::sys_common::backtrace::_print_fmt::h6e24477d644418e5
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/std/src/sys_common/backtrace.rs:65:5
   3:     0x7ff884b62fc1 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h3878e1514fe266c9
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7ff884bc949c - core::fmt::rt::Argument::fmt::h407ed1ed725edf0f
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/core/src/fmt/rt.rs:138:9
   5:     0x7ff884bc949c - core::fmt::write::h80ad66db775b495e
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/core/src/fmt/mod.rs:1094:21
   6:     0x7ff884b556be - std::io::Write::write_fmt::h535bb7b7a3791561
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/std/src/io/mod.rs:1714:15
   7:     0x7ff884b62dd5 - std::sys_common::backtrace::_print::hb86dcc235566a2df
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7ff884b62dd5 - std::sys_common::backtrace::print::h720885d4d12f3280
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7ff884b65e2a - std::panicking::panic_hook_with_disk_dump::{{closure}}::h760a0ce73306965a
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/std/src/panicking.rs:278:22
  10:     0x7ff884b65ac3 - std::panicking::panic_hook_with_disk_dump::hd7987642ef347005
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/std/src/panicking.rs:312:9
  11:     0x7ff887d83f09 - rustc_driver_impl[4c3415319b4cba14]::install_ice_hook::{closure#0}
  12:     0x7ff884b666d0 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hc74b41dc32ec0c34
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/alloc/src/boxed.rs:2021:9
  13:     0x7ff884b666d0 - std::panicking::rust_panic_with_hook::h917efb61d3f92bd6
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/std/src/panicking.rs:733:13
  14:     0x7ff88834abb1 - std[5080124dd0971100]::panicking::begin_panic::<rustc_errors[528ecd00bd6aace4]::ExplicitBug>::{closure#0}
  15:     0x7ff888348e46 - std[5080124dd0971100]::sys_common::backtrace::__rust_end_short_backtrace::<std[5080124dd0971100]::panicking::begin_panic<rustc_errors[528ecd00bd6aace4]::ExplicitBug>::{closure#0}, !>
  16:     0x7ff8882bfaa6 - std[5080124dd0971100]::panicking::begin_panic::<rustc_errors[528ecd00bd6aace4]::ExplicitBug>
  17:     0x7ff8882d4dc4 - <rustc_errors[528ecd00bd6aace4]::HandlerInner>::bug::<alloc[4a1a2fec03248e1d]::string::String>
  18:     0x7ff8882d4c48 - <rustc_errors[528ecd00bd6aace4]::Handler>::bug::<alloc[4a1a2fec03248e1d]::string::String>
  19:     0x7ff88831bfec - rustc_middle[b0c4fd38174bce7e]::util::bug::opt_span_bug_fmt::<rustc_span[a85072546ba3a5ab]::span_encoding::Span>::{closure#0}
  20:     0x7ff88831adea - rustc_middle[b0c4fd38174bce7e]::ty::context::tls::with_opt::<rustc_middle[b0c4fd38174bce7e]::util::bug::opt_span_bug_fmt<rustc_span[a85072546ba3a5ab]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  21:     0x7ff88831adb8 - rustc_middle[b0c4fd38174bce7e]::ty::context::tls::with_context_opt::<rustc_middle[b0c4fd38174bce7e]::ty::context::tls::with_opt<rustc_middle[b0c4fd38174bce7e]::util::bug::opt_span_bug_fmt<rustc_span[a85072546ba3a5ab]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  22:     0x7ff886888380 - rustc_middle[b0c4fd38174bce7e]::util::bug::bug_fmt
  23:     0x7ff8864b3c08 - <rustc_const_eval[b104660bb5ced4cb]::interpret::eval_context::InterpCx<rustc_const_eval[b104660bb5ced4cb]::const_eval::machine::CompileTimeInterpreter>>::project_field::<rustc_const_eval[b104660bb5ced4cb]::interpret::operand::OpTy>
  24:     0x7ff8864b0551 - <rustc_const_eval[b104660bb5ced4cb]::interpret::validity::ValidityVisitor<rustc_const_eval[b104660bb5ced4cb]::const_eval::machine::CompileTimeInterpreter> as rustc_const_eval[b104660bb5ced4cb]::interpret::visitor::ValueVisitor<rustc_const_eval[b104660bb5ced4cb]::const_eval::machine::CompileTimeInterpreter>>::walk_value
  25:     0x7ff8862f1676 - <rustc_const_eval[b104660bb5ced4cb]::interpret::eval_context::InterpCx<rustc_const_eval[b104660bb5ced4cb]::const_eval::machine::CompileTimeInterpreter>>::validate_operand_internal
  26:     0x7ff887425915 - rustc_const_eval[b104660bb5ced4cb]::util::check_validity_requirement::check_validity_requirement
  27:     0x7ff887425564 - <rustc_const_eval[b104660bb5ced4cb]::provide::{closure#3} as core[14419025a21183a1]::ops::function::FnOnce<(rustc_middle[b0c4fd38174bce7e]::ty::context::TyCtxt, (rustc_middle[b0c4fd38174bce7e]::ty::layout::ValidityRequirement, rustc_middle[b0c4fd38174bce7e]::ty::ParamEnvAnd<rustc_middle[b0c4fd38174bce7e]::ty::Ty>))>>::call_once
  28:     0x7ff887258c32 - rustc_query_impl[9e9aac2bcfc5a39e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9e9aac2bcfc5a39e]::query_impl::check_validity_requirement::dynamic_query::{closure#2}::{closure#0}, rustc_middle[b0c4fd38174bce7e]::query::erase::Erased<[u8; 16usize]>>
  29:     0x7ff887258bf2 - <rustc_query_impl[9e9aac2bcfc5a39e]::query_impl::check_validity_requirement::dynamic_query::{closure#2} as core[14419025a21183a1]::ops::function::FnOnce<(rustc_middle[b0c4fd38174bce7e]::ty::context::TyCtxt, (rustc_middle[b0c4fd38174bce7e]::ty::layout::ValidityRequirement, rustc_middle[b0c4fd38174bce7e]::ty::ParamEnvAnd<rustc_middle[b0c4fd38174bce7e]::ty::Ty>))>>::call_once
  30:     0x7ff8873ec5fb - rustc_query_system[bd77d5a6524fb1ee]::query::plumbing::try_execute_query::<rustc_query_impl[9e9aac2bcfc5a39e]::DynamicConfig<rustc_query_system[bd77d5a6524fb1ee]::query::caches::DefaultCache<(rustc_middle[b0c4fd38174bce7e]::ty::layout::ValidityRequirement, rustc_middle[b0c4fd38174bce7e]::ty::ParamEnvAnd<rustc_middle[b0c4fd38174bce7e]::ty::Ty>), rustc_middle[b0c4fd38174bce7e]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[9e9aac2bcfc5a39e]::plumbing::QueryCtxt, false>
  31:     0x7ff8873ec27e - rustc_query_impl[9e9aac2bcfc5a39e]::query_impl::check_validity_requirement::get_query_non_incr::__rust_end_short_backtrace
  32:     0x7ff886d679d2 - <rustc_mir_transform[102926042b0adb92]::instsimplify::InstSimplify as rustc_middle[b0c4fd38174bce7e]::mir::MirPass>::run_pass
  33:     0x7ff886b24fdd - rustc_mir_transform[102926042b0adb92]::pass_manager::run_passes
  34:     0x7ff8871d39f7 - rustc_mir_transform[102926042b0adb92]::optimized_mir
  35:     0x7ff885eb6800 - rustc_query_impl[9e9aac2bcfc5a39e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9e9aac2bcfc5a39e]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[b0c4fd38174bce7e]::query::erase::Erased<[u8; 8usize]>>
  36:     0x7ff885eb67c1 - <rustc_query_impl[9e9aac2bcfc5a39e]::query_impl::optimized_mir::dynamic_query::{closure#2} as core[14419025a21183a1]::ops::function::FnOnce<(rustc_middle[b0c4fd38174bce7e]::ty::context::TyCtxt, rustc_span[a85072546ba3a5ab]::def_id::DefId)>>::call_once
  37:     0x7ff885e3b1b5 - rustc_query_system[bd77d5a6524fb1ee]::query::plumbing::try_execute_query::<rustc_query_impl[9e9aac2bcfc5a39e]::DynamicConfig<rustc_query_system[bd77d5a6524fb1ee]::query::caches::DefaultCache<rustc_span[a85072546ba3a5ab]::def_id::DefId, rustc_middle[b0c4fd38174bce7e]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[9e9aac2bcfc5a39e]::plumbing::QueryCtxt, false>
  38:     0x7ff8876dc310 - rustc_query_impl[9e9aac2bcfc5a39e]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
  39:     0x7ff8885777e7 - rustc_middle[b0c4fd38174bce7e]::query::plumbing::query_get_at::<rustc_query_system[bd77d5a6524fb1ee]::query::caches::DefaultCache<rustc_span[a85072546ba3a5ab]::def_id::DefId, rustc_middle[b0c4fd38174bce7e]::query::erase::Erased<[u8; 8usize]>>>
  40:     0x7ff88658bd6f - rustc_query_impl[9e9aac2bcfc5a39e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9e9aac2bcfc5a39e]::query_impl::unused_generic_params::dynamic_query::{closure#2}::{closure#0}, rustc_middle[b0c4fd38174bce7e]::query::erase::Erased<[u8; 4usize]>>
  41:     0x7ff8866b3421 - rustc_query_system[bd77d5a6524fb1ee]::query::plumbing::try_execute_query::<rustc_query_impl[9e9aac2bcfc5a39e]::DynamicConfig<rustc_query_system[bd77d5a6524fb1ee]::query::caches::DefaultCache<rustc_middle[b0c4fd38174bce7e]::ty::instance::InstanceDef, rustc_middle[b0c4fd38174bce7e]::query::erase::Erased<[u8; 4usize]>>, false, false, false>, rustc_query_impl[9e9aac2bcfc5a39e]::plumbing::QueryCtxt, false>
  42:     0x7ff8866b30c7 - rustc_query_impl[9e9aac2bcfc5a39e]::query_impl::unused_generic_params::get_query_non_incr::__rust_end_short_backtrace
  43:     0x7ff887222164 - <rustc_session[f4a3fd7c44936466]::session::Session>::time::<(), rustc_interface[effa57ef6cdf43c8]::passes::analysis::{closure#2}>
  44:     0x7ff88721ee25 - rustc_interface[effa57ef6cdf43c8]::passes::analysis
  45:     0x7ff887279d2a - rustc_query_impl[9e9aac2bcfc5a39e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9e9aac2bcfc5a39e]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[b0c4fd38174bce7e]::query::erase::Erased<[u8; 1usize]>>
  46:     0x7ff887279d19 - <rustc_query_impl[9e9aac2bcfc5a39e]::query_impl::analysis::dynamic_query::{closure#2} as core[14419025a21183a1]::ops::function::FnOnce<(rustc_middle[b0c4fd38174bce7e]::ty::context::TyCtxt, ())>>::call_once
  47:     0x7ff88742aa08 - rustc_query_system[bd77d5a6524fb1ee]::query::plumbing::try_execute_query::<rustc_query_impl[9e9aac2bcfc5a39e]::DynamicConfig<rustc_query_system[bd77d5a6524fb1ee]::query::caches::SingleCache<rustc_middle[b0c4fd38174bce7e]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[9e9aac2bcfc5a39e]::plumbing::QueryCtxt, false>
  48:     0x7ff88742a797 - rustc_query_impl[9e9aac2bcfc5a39e]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  49:     0x7ff8870057f5 - <rustc_middle[b0c4fd38174bce7e]::ty::context::GlobalCtxt>::enter::<rustc_driver_impl[4c3415319b4cba14]::run_compiler::{closure#1}::{closure#2}::{closure#4}, core[14419025a21183a1]::result::Result<(), rustc_span[a85072546ba3a5ab]::ErrorGuaranteed>>
  50:     0x7ff887004e22 - <rustc_interface[effa57ef6cdf43c8]::interface::Compiler>::enter::<rustc_driver_impl[4c3415319b4cba14]::run_compiler::{closure#1}::{closure#2}, core[14419025a21183a1]::result::Result<core[14419025a21183a1]::option::Option<rustc_interface[effa57ef6cdf43c8]::queries::Linker>, rustc_span[a85072546ba3a5ab]::ErrorGuaranteed>>
  51:     0x7ff886ffded8 - std[5080124dd0971100]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[effa57ef6cdf43c8]::util::run_in_thread_pool_with_globals<rustc_interface[effa57ef6cdf43c8]::interface::run_compiler<core[14419025a21183a1]::result::Result<(), rustc_span[a85072546ba3a5ab]::ErrorGuaranteed>, rustc_driver_impl[4c3415319b4cba14]::run_compiler::{closure#1}>::{closure#0}, core[14419025a21183a1]::result::Result<(), rustc_span[a85072546ba3a5ab]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[14419025a21183a1]::result::Result<(), rustc_span[a85072546ba3a5ab]::ErrorGuaranteed>>
  52:     0x7ff886ffd65e - <<std[5080124dd0971100]::thread::Builder>::spawn_unchecked_<rustc_interface[effa57ef6cdf43c8]::util::run_in_thread_pool_with_globals<rustc_interface[effa57ef6cdf43c8]::interface::run_compiler<core[14419025a21183a1]::result::Result<(), rustc_span[a85072546ba3a5ab]::ErrorGuaranteed>, rustc_driver_impl[4c3415319b4cba14]::run_compiler::{closure#1}>::{closure#0}, core[14419025a21183a1]::result::Result<(), rustc_span[a85072546ba3a5ab]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[14419025a21183a1]::result::Result<(), rustc_span[a85072546ba3a5ab]::ErrorGuaranteed>>::{closure#1} as core[14419025a21183a1]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  53:     0x7ff884b70dc5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::heeee2b291a22389d
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/alloc/src/boxed.rs:2007:9
  54:     0x7ff884b70dc5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hb0196af3fff8f8b5
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/alloc/src/boxed.rs:2007:9
  55:     0x7ff884b70dc5 - std::sys::unix::thread::Thread::new::thread_start::hbcdc2efff7ecdfed
                               at /rustc/500647fd8138cc09e87edb08d62f81654fbf6ef8/library/std/src/sys/unix/thread.rs:108:17
  56:     0x7ff8848c944b - <unknown>
  57:     0x7ff88494ce40 - <unknown>
  58:                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 attach the file at `/home/matthias/vcs/github/glacier2/pile/rustc-ice-2023-07-28T20:48:26.790849438Z-984211.txt` to your bug report

note: compiler flags: -Z strict-init-checks=yes -Z polymorphize=on -Z mir-opt-level=3

query stack during panic:
#0 [check_validity_requirement] checking validity requirement for `Wrap<&T>`: allows being left zeroed
#1 [optimized_mir] optimizing MIR for `generic`
#2 [unused_generic_params] determining which generic parameters are unused by `generic`
#3 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to previous error; 7 warnings emitted
```

</p>
</details>