Repository: rust
Issue: ICE: const eval: `ImmTy { .. } input to a fat-to-thin cast´
Labels: I-ICE,T-compiler,C-bug,A-const-eval,-Zpolymorphize,S-bug-has-test

<!--
[31mICE[0m: Rustc ./a.rs '-Zmir-opt-level=5 -Zpolymorphize=on -ooutputfile -Zdump-mir-dir=dir' 'error: internal compiler error: /rustc/39b7669347b02f25a36da610822fb3c1e03bac6c/compiler/rustc_const_eval/src/interpret/cast.rs:216:42: ImmTy { imm: Scalar(alloc1), ty: *const dyn Sync } input to a fat-to-thin cast (*const dyn Sync -> *const usize)', 'error: internal compiler error: /rustc/39b7669347b02f25a36da610822fb3c1e03bac6c/compiler/rustc_const_eval/src/interpret/cast.rs:216:42: ImmTy { imm: Scalar(alloc1), ty: *const dyn Sync } input to a fat-to-thin cast (*const dyn Sync -> *const usize)'
File: /tmp/im/a.rs
-->

auto-reduced (treereduce-rust):
````rust
struct S<T>(T)
where
    [T; (
        |_: u8| {
            static FOO: Sync = AtomicUsize::new(0);
            unsafe { &*(&FOO as *const _ as *const usize) }
        },
        1,
    )
        .1]: Copy;


````

original:
````rust
struct S<T>(T) where [T; (|_: u8|{
    static FOO: Sync = AtomicUsize::new(0);
    unsafe { &*(&FOO as *const _ as *const usize) }
}, 1).1]: Copy;
fn main() {}

````

Version information
````
rustc 1.83.0-nightly (39b766934 2024-09-16)
binary: rustc
commit-hash: 39b7669347b02f25a36da610822fb3c1e03bac6c
commit-date: 2024-09-16
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

````

Command:
`/home/matthias/.rustup/toolchains/master/bin/rustc -Zmir-opt-level=5 -Zpolymorphize=on`

<!--
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[E0433]: failed to resolve: use of undeclared type `AtomicUsize`
 --> /tmp/icemaker_global_tempdir.VG6dBz3gFVPN/rustc_testrunner_tmpdir_reporting.n3Yk4XznZfno/mvce.rs:5:32
  |
5 |             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.VG6dBz3gFVPN/rustc_testrunner_tmpdir_reporting.n3Yk4XznZfno/mvce.rs:10:19
   |
10 |         .1]: Copy;
   |                   ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.VG6dBz3gFVPN/rustc_testrunner_tmpdir_reporting.n3Yk4XznZfno/mvce.rs`

warning: trait objects without an explicit `dyn` are deprecated
 --> /tmp/icemaker_global_tempdir.VG6dBz3gFVPN/rustc_testrunner_tmpdir_reporting.n3Yk4XznZfno/mvce.rs:5:25
  |
5 |             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`
  |
5 |             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.VG6dBz3gFVPN/rustc_testrunner_tmpdir_reporting.n3Yk4XznZfno/mvce.rs:5:25
  |
5 |             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.VG6dBz3gFVPN/rustc_testrunner_tmpdir_reporting.n3Yk4XznZfno/mvce.rs:5:32
  |
5 |             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: /rustc/39b7669347b02f25a36da610822fb3c1e03bac6c/compiler/rustc_const_eval/src/interpret/cast.rs:216:42: ImmTy { imm: Scalar(alloc1), ty: *const dyn Sync } input to a fat-to-thin cast (*const dyn Sync -> *const usize)

thread 'rustc' panicked at /rustc/39b7669347b02f25a36da610822fb3c1e03bac6c/compiler/rustc_const_eval/src/interpret/cast.rs:216:42:
Box<dyn Any>
stack backtrace:
   0:     0x7cdf71066b5a - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h688d92dd3e7bfae7
   1:     0x7cdf71803297 - core::fmt::write::h8095b5fc1f2aba3f
   2:     0x7cdf7270c073 - std::io::Write::write_fmt::hdc66ed1666cc8932
   3:     0x7cdf710669b2 - std::sys::backtrace::BacktraceLock::print::h7b49d08d4dea3fff
   4:     0x7cdf71069131 - std::panicking::default_hook::{{closure}}::hc0a5f9ff76fea135
   5:     0x7cdf71068f64 - std::panicking::default_hook::hf28f41a3f75f65f5
   6:     0x7cdf7016b81f - std[5fe423659e5b23cd]::panicking::update_hook::<alloc[fb3eda72240f4d64]::boxed::Box<rustc_driver_impl[8cede590182e427d]::install_ice_hook::{closure#0}>>::{closure#0}
   7:     0x7cdf71069858 - std::panicking::rust_panic_with_hook::h868a1036df76a3e6
   8:     0x7cdf701a5001 - std[5fe423659e5b23cd]::panicking::begin_panic::<rustc_errors[d9a0488990c1eec8]::ExplicitBug>::{closure#0}
   9:     0x7cdf70198676 - std[5fe423659e5b23cd]::sys::backtrace::__rust_end_short_backtrace::<std[5fe423659e5b23cd]::panicking::begin_panic<rustc_errors[d9a0488990c1eec8]::ExplicitBug>::{closure#0}, !>
  10:     0x7cdf70193bb9 - std[5fe423659e5b23cd]::panicking::begin_panic::<rustc_errors[d9a0488990c1eec8]::ExplicitBug>
  11:     0x7cdf701ae181 - <rustc_errors[d9a0488990c1eec8]::diagnostic::BugAbort as rustc_errors[d9a0488990c1eec8]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x7cdf708bdb0d - <rustc_errors[d9a0488990c1eec8]::DiagCtxtHandle>::span_bug::<rustc_span[52d9c6078ded5bad]::span_encoding::Span, alloc[fb3eda72240f4d64]::string::String>
  13:     0x7cdf708f15f8 - rustc_middle[552e10f1b06d458a]::util::bug::opt_span_bug_fmt::<rustc_span[52d9c6078ded5bad]::span_encoding::Span>::{closure#0}
  14:     0x7cdf708f1e7a - rustc_middle[552e10f1b06d458a]::ty::context::tls::with_opt::<rustc_middle[552e10f1b06d458a]::util::bug::opt_span_bug_fmt<rustc_span[52d9c6078ded5bad]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  15:     0x7cdf708ddbeb - rustc_middle[552e10f1b06d458a]::ty::context::tls::with_context_opt::<rustc_middle[552e10f1b06d458a]::ty::context::tls::with_opt<rustc_middle[552e10f1b06d458a]::util::bug::opt_span_bug_fmt<rustc_span[52d9c6078ded5bad]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  16:     0x7cdf708d4a17 - rustc_middle[552e10f1b06d458a]::util::bug::span_bug_fmt::<rustc_span[52d9c6078ded5bad]::span_encoding::Span>
  17:     0x7cdf72244b0d - <rustc_mir_transform[e930eb8a847a145b]::gvn::VnState>::insert
  18:     0x7cdf72238dda - <rustc_mir_transform[e930eb8a847a145b]::gvn::VnState>::simplify_rvalue
  19:     0x7cdf6ee4ae16 - <rustc_mir_transform[e930eb8a847a145b]::gvn::GVN as rustc_mir_transform[e930eb8a847a145b]::pass_manager::MirPass>::run_pass
  20:     0x7cdf7180d84d - rustc_mir_transform[e930eb8a847a145b]::pass_manager::run_passes_inner
  21:     0x7cdf721c9222 - rustc_mir_transform[e930eb8a847a145b]::optimized_mir
  22:     0x7cdf721c7ae1 - rustc_query_impl[eb7750c59ad6b12a]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[eb7750c59ad6b12a]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[552e10f1b06d458a]::query::erase::Erased<[u8; 8usize]>>
  23:     0x7cdf7182f56e - rustc_query_system[d4c8a0ce1f97cbc0]::query::plumbing::try_execute_query::<rustc_query_impl[eb7750c59ad6b12a]::DynamicConfig<rustc_query_system[d4c8a0ce1f97cbc0]::query::caches::DefIdCache<rustc_middle[552e10f1b06d458a]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[eb7750c59ad6b12a]::plumbing::QueryCtxt, false>
  24:     0x7cdf7182eb1f - rustc_query_impl[eb7750c59ad6b12a]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
  25:     0x7cdf721bebe7 - rustc_middle[552e10f1b06d458a]::query::plumbing::query_get_at::<rustc_query_system[d4c8a0ce1f97cbc0]::query::caches::DefIdCache<rustc_middle[552e10f1b06d458a]::query::erase::Erased<[u8; 8usize]>>>
  26:     0x7cdf71cb78cf - rustc_query_impl[eb7750c59ad6b12a]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[eb7750c59ad6b12a]::query_impl::unused_generic_params::dynamic_query::{closure#2}::{closure#0}, rustc_middle[552e10f1b06d458a]::query::erase::Erased<[u8; 4usize]>>
  27:     0x7cdf71cb70af - rustc_query_system[d4c8a0ce1f97cbc0]::query::plumbing::try_execute_query::<rustc_query_impl[eb7750c59ad6b12a]::DynamicConfig<rustc_query_system[d4c8a0ce1f97cbc0]::query::caches::DefaultCache<rustc_middle[552e10f1b06d458a]::ty::instance::InstanceKind, rustc_middle[552e10f1b06d458a]::query::erase::Erased<[u8; 4usize]>>, false, false, false>, rustc_query_impl[eb7750c59ad6b12a]::plumbing::QueryCtxt, false>
  28:     0x7cdf71cb6d90 - rustc_query_impl[eb7750c59ad6b12a]::query_impl::unused_generic_params::get_query_non_incr::__rust_end_short_backtrace
  29:     0x7cdf71cbb414 - rustc_interface[30a05c7cffabd25a]::passes::run_required_analyses
  30:     0x7cdf7251c49e - rustc_interface[30a05c7cffabd25a]::passes::analysis
  31:     0x7cdf7251c471 - rustc_query_impl[eb7750c59ad6b12a]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[eb7750c59ad6b12a]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[552e10f1b06d458a]::query::erase::Erased<[u8; 1usize]>>
  32:     0x7cdf726b9e6e - rustc_query_system[d4c8a0ce1f97cbc0]::query::plumbing::try_execute_query::<rustc_query_impl[eb7750c59ad6b12a]::DynamicConfig<rustc_query_system[d4c8a0ce1f97cbc0]::query::caches::SingleCache<rustc_middle[552e10f1b06d458a]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[eb7750c59ad6b12a]::plumbing::QueryCtxt, false>
  33:     0x7cdf726b9bcf - rustc_query_impl[eb7750c59ad6b12a]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  34:     0x7cdf7250697c - rustc_interface[30a05c7cffabd25a]::interface::run_compiler::<core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>, rustc_driver_impl[8cede590182e427d]::run_compiler::{closure#0}>::{closure#1}
  35:     0x7cdf725c7f90 - std[5fe423659e5b23cd]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[30a05c7cffabd25a]::util::run_in_thread_with_globals<rustc_interface[30a05c7cffabd25a]::util::run_in_thread_pool_with_globals<rustc_interface[30a05c7cffabd25a]::interface::run_compiler<core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>, rustc_driver_impl[8cede590182e427d]::run_compiler::{closure#0}>::{closure#1}, core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>>::{closure#0}, core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>>
  36:     0x7cdf725c85fa - <<std[5fe423659e5b23cd]::thread::Builder>::spawn_unchecked_<rustc_interface[30a05c7cffabd25a]::util::run_in_thread_with_globals<rustc_interface[30a05c7cffabd25a]::util::run_in_thread_pool_with_globals<rustc_interface[30a05c7cffabd25a]::interface::run_compiler<core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>, rustc_driver_impl[8cede590182e427d]::run_compiler::{closure#0}>::{closure#1}, core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>>::{closure#0}, core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[62617e15cf8aa4b2]::result::Result<(), rustc_span[52d9c6078ded5bad]::ErrorGuaranteed>>::{closure#1} as core[62617e15cf8aa4b2]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  37:     0x7cdf725c89eb - std::sys::pal::unix::thread::Thread::new::thread_start::h043ff58bc28d939d
  38:     0x7cdf73ce239d - <unknown>
  39:     0x7cdf73d6749c - <unknown>
  40:                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.83.0-nightly (39b766934 2024-09-16) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z mir-opt-level=5 -Z polymorphize=on -Z dump-mir-dir=dir

query stack during panic:
#0 [optimized_mir] optimizing MIR for `S::{constant#0}::{closure#0}`
#1 [unused_generic_params] determining which generic parameters are unused by `S::{constant#0}::{closure#0}`
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`.

```

</p>
</details>

<!--
query stack:
#0 [optimized_mir] optimizing MIR for `S::{constant#0}::{closure#0}`
#1 [unused_generic_params] determining which generic parameters are unused by `S::{constant#0}::{closure#0}`

-->