Repository: rust
Issue: ICE: `assertion left == right failed` in `rustc_const_eval/src/interpret/cast.rs unsize_into`
Labels: I-ICE,T-compiler,C-bug,F-coerce_unsized,S-bug-has-test

<!--
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

```Rust
#![feature(coerce_unsized)]
use std::ops::CoerceUnsized;

struct Foo<T: ?Sized> {
    a: T,
}

impl<T, U> CoerceUnsized<U> for Foo<T> {}

union U {
    a: usize,
}

const C: U = Foo { a: 10 };

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.81.0-nightly (fda509e81 2024-06-25)
binary: rustc
commit-hash: fda509e817abeeecb5b76bc1de844f355675c81e
commit-date: 2024-06-25
host: x86_64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7
```

### Error output

```
error[E0376]: the trait `CoerceUnsized` may only be implemented for a coercion between structures
 --> cast442.rs:8:1
  |
8 | impl<T, U> CoerceUnsized<U> for Foo<T> {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

<!--
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>

```
thread 'rustc' panicked at compiler/rustc_const_eval/src/interpret/cast.rs:442:17:
assertion `left == right` failed
  left: Foo
 right: U
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::assert_failed_inner
   3: core::panicking::assert_failed::<&rustc_middle::ty::adt::AdtDef, &rustc_middle::ty::adt::AdtDef>
   4: <rustc_const_eval::interpret::eval_context::InterpCx<rustc_const_eval::const_eval::machine::CompileTimeMachine>>::unsize_into
   5: <rustc_const_eval::interpret::eval_context::InterpCx<rustc_const_eval::const_eval::machine::CompileTimeMachine>>::statement
   6: rustc_const_eval::const_eval::eval_queries::eval_to_allocation_raw_provider
      [... omitted 2 frames ...]
   7: rustc_middle::query::plumbing::query_get_at::<rustc_query_system::query::caches::DefaultCache<rustc_middle::ty::ParamEnvAnd<rustc_middle::mir::interpret::GlobalId>, rustc_middle::query::erase::Erased<[u8; 24]>>>
   8: rustc_const_eval::const_eval::eval_queries::eval_to_const_value_raw_provider
      [... omitted 2 frames ...]
   9: <rustc_middle::hir::map::Map>::par_body_owners::<rustc_hir_analysis::check_crate::{closure#3}>::{closure#0}
  10: rustc_hir_analysis::check_crate
  11: rustc_interface::passes::run_required_analyses
  12: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  13: <rustc_interface::queries::QueryResult<&rustc_middle::ty::context::GlobalCtxt>>::enter::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}::{closure#1}::{closure#5}>
  14: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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: please attach the file at `/Volumes/T7/workspace/placeholder_rustexec/0625/rustc-ice-2024-06-26T06_34_30-28249.txt` to your bug report

query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `C`
#1 [eval_to_const_value_raw] simplifying constant for the type system `C`
#2 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0376`.
```

</p>
</details>

### Note
- ICE location: `compiler/rustc_const_eval/src/interpret/cast.rs#L442`
  https://github.com/rust-lang/rust/blob/fda509e817abeeecb5b76bc1de844f355675c81e/compiler/rustc_const_eval/src/interpret/cast.rs#L431-L442
- #126269 might be related


@rustbot label +F-coerce_unsized