Repository: rust
Issue: ICE: `unsize_into: invalid conversion` in `rustc_const_eval/src/interpret/cast.rs`
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

(hand-reduced)

```Rust
#![feature(coerce_unsized)]

pub enum Foo<T> {
    Bar([T; usize::MAX]),
}

use std::ops::CoerceUnsized;

impl<T, U> CoerceUnsized<U> for T {}

fn main() {}
```

<details><summary><strong>(original)</strong></summary>
<p>

```Rust
// Make sure the compiler does not ICE when trying to generate the debuginfo name of a type that
// causes a layout error.
// This version of the test already ICE'd before the commit that introduce the ICE described in
// https://github.com/rust-lang/rust/issues/94961.

//@ compile-flags:-C debuginfo=2
//@ build-fail
//@ error-pattern: too big for the current architecture
//@ normalize-stderr-64bit "18446744073709551615" -> "SIZE"
//@ normalize-stderr-32bit "4294967295" -> "SIZE"

#![crate_type = "rlib"]

pub enum Foo<T> {
    Bar([T; usize::MAX]),
}

pub fn foo() -> usize {
    std::mem::size_of::<Foo<u8>>()
}


#![feature(coerce_unsized)]
use std::ops::CoerceUnsized;

struct Foo<T: ?Sized> {
    a: T,
}

impl<T, U> CoerceUnsized<U> for T {} //~ ERROR E0376

fn main() {}
```

</p>
</details>


### 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 (b5b13568f 2024-06-10)
binary: rustc
commit-hash: b5b13568fb5da4ac988bde370008d6134d3dfe6c
commit-date: 2024-06-10
host: x86_64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7
```

### Error output

Command: `rustc`

```
<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: compiler/rustc_const_eval/src/interpret/cast.rs:476:17: unsize_into: invalid conversion: TyAndLayout { ty: usize, layout: Layout { size: Size(8 bytes), align: AbiAndPrefAlign { abi: Align(8 bytes), pref: Align(8 bytes) }, abi: Scalar(Initialized { value: Int(I64, false), valid_range: 0..=18446744073709551615 }), fields: Primitive, largest_niche: None, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes) } } -> TyAndLayout { ty: usize, layout: Layout { size: Size(8 bytes), align: AbiAndPrefAlign { abi: Align(8 bytes), pref: Align(8 bytes) }, abi: Scalar(Initialized { value: Int(I64, false), valid_range: 0..=18446744073709551615 }), fields: Primitive, largest_niche: None, variants: Single { index: 0 }, max_repr_align: None, unadjusted_abi_align: Align(8 bytes) } }
 --> r_unsize_into_5CF554F2.rs:4:13
  |
4 |     Bar([T; usize::MAX]),
  |             ^^^^^^^^^^

thread 'rustc' panicked at compiler/rustc_const_eval/src/interpret/cast.rs:476:17:
Box<dyn Any>
stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: <rustc_errors::diagnostic::BugAbort as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   2: <rustc_errors::DiagCtxt>::span_bug::<rustc_span::span_encoding::Span, alloc::string::String>
   3: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
   4: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}
   5: rustc_middle::ty::context::tls::with_context_opt::<rustc_middle::ty::context::tls::with_opt<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
   6: rustc_middle::util::bug::span_bug_fmt::<rustc_span::span_encoding::Span>
   7: <rustc_const_eval::interpret::eval_context::InterpCx<rustc_const_eval::const_eval::machine::CompileTimeInterpreter>>::unsize_into
   8: <rustc_const_eval::interpret::eval_context::InterpCx<rustc_const_eval::const_eval::machine::CompileTimeInterpreter>>::statement
   9: rustc_const_eval::const_eval::eval_queries::eval_to_allocation_raw_provider
      [... omitted 2 frames ...]
  10: 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]>>>
  11: rustc_const_eval::const_eval::valtrees::eval_to_valtree
  12: <rustc_const_eval::provide::{closure#0} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_middle::ty::ParamEnvAnd<rustc_middle::mir::interpret::GlobalId>)>>::call_once
      [... omitted 2 frames ...]
  13: 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]>>>
  14: <rustc_middle::ty::context::TyCtxt>::const_eval_global_id_for_typeck
  15: <rustc_middle::ty::context::TyCtxt>::const_eval_resolve_for_typeck
  16: <rustc_middle::ty::consts::Const>::eval
  17: rustc_trait_selection::traits::util::with_replaced_escaping_bound_vars::<rustc_middle::ty::consts::Const, rustc_middle::ty::consts::Const, <rustc_trait_selection::traits::normalize::AssocTypeNormalizer as rustc_type_ir::fold::TypeFolder<rustc_middle::ty::context::TyCtxt>>::fold_const::{closure#0}>
  18: <rustc_middle::ty::Ty as rustc_type_ir::fold::TypeSuperFoldable<rustc_middle::ty::context::TyCtxt>>::try_super_fold_with::<rustc_trait_selection::traits::normalize::AssocTypeNormalizer>
  19: <rustc_infer::infer::at::At as rustc_trait_selection::traits::normalize::NormalizeExt>::normalize::<rustc_middle::ty::Ty>
  20: rustc_hir_analysis::check::wfcheck::check_type_defn
  21: rustc_hir_analysis::check::wfcheck::check_well_formed
      [... omitted 1 frame ...]
  22: rustc_middle::query::plumbing::query_ensure_error_guaranteed::<rustc_query_system::query::caches::VecCache<rustc_hir::hir_id::OwnerId, rustc_middle::query::erase::Erased<[u8; 1]>>, ()>
  23: rustc_hir_analysis::check::wfcheck::check_mod_type_wf
      [... omitted 1 frame ...]
  24: rustc_hir_analysis::check_crate
  25: rustc_interface::passes::run_required_analyses
  26: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  27: <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#3}>
  28: 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.

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/240607_100chaos_tree_combine_typ/icefiles/rustc-ice-2024-06-11T11_46_53-81599.txt` to your bug report

query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `Foo::Bar::0::{constant#0}`
#1 [eval_to_valtree] evaluating type-level constant
#2 [check_well_formed] checking that `Foo` is well-formed
#3 [check_mod_type_wf] checking that types are well-formed in top-level module
#4 [analysis] running analysis passes on this crate
end of query stack
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
 --> r_unsize_into_5CF554F2.rs:9:6
  |
9 | impl<T, U> CoerceUnsized<U> for T {}
  |      ^ type parameter `T` must be used as the type parameter for some local type
  |
  = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
  = note: only traits defined in the current crate can be implemented for a type parameter

error[E0376]: the trait `CoerceUnsized` may only be implemented for a coercion between structures
 --> r_unsize_into_5CF554F2.rs:9:1
  |
9 | impl<T, U> CoerceUnsized<U> for T {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0210, E0376.
For more information about an error, try `rustc --explain E0210`.
```

</p>
</details>

### Notes
- ICE location: `rustc_const_eval/src/interpret/cast.rs L476`
  https://github.com/rust-lang/rust/blob/b5b13568fb5da4ac988bde370008d6134d3dfe6c/compiler/rustc_const_eval/src/interpret/cast.rs#L471-L482

@rustbot label +F-coerce_unsized