Repository: rust
Issue: ICE: `SizeSkeleton::compute` layout errored in `layout.rs`
Labels: I-ICE,T-compiler,C-bug,A-layout,S-has-mcve,S-bug-has-test,F-impl_trait_in_assoc_type

<!--
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(impl_trait_in_assoc_type)]

trait Trait: Sized {
    type Assoc2;
}

impl Trait for Bar {
    type Assoc2 = impl std::fmt::Debug;
}

struct Foo {
    field: <Bar as Trait>::Assoc2,
}

enum Bar {
    C = 42,
    D = 99,
}

static BAR: u8 = 42;

static FOO2: (&Foo, &<Bar as Trait>::Assoc2) =
    unsafe { (std::mem::transmute(&BAR), std::mem::transmute(&BAR)) };

fn main() {}
```

<details><summary><strong>(original)</strong></summary>
<p>

```Rust
//! This test demonstrates a bug where we accidentally
//! detected opaque types in struct fields, but only if nested
//! in projections of another opaque type.

#![feature(impl_trait_in_assoc_type)]

struct Bar;

trait Trait: Sized {
    type Assoc2;
    type Assoc;
    fn foo() -> Self::Assoc;
}

impl Trait for Bar {
    type Assoc2 = impl std::fmt::Debug;
    type Assoc = impl Iterator<Item = Foo>;
    fn foo() -> Self::Assoc {
        vec![Foo { field: () }].into_iter()
        //~^ ERROR mismatched types
    }
}

struct Foo {
    field: <Bar as Trait>::Assoc2,
}

fn main() {}


//@ check-pass

enum Foo {
    A = 5,
    B = 42,
}
enum Bar {
    C = 42,
    D = 99,
}
#[repr(C)]
union Union {
    foo: &'static Foo,
    bar: &'static Bar,
    u8: &'static u8,
}
static BAR: u8 = 42;
static FOO: (&Foo, &Bar) = unsafe {(
    Union { u8: &BAR }.foo,
    Union { u8: &BAR }.bar,
)};

static FOO2: (&Foo, &<Bar as Trait>::Assoc2) = unsafe {(std::mem::transmute(&BAR), std::mem::transmute(&BAR))};

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.80.0-nightly (debd22da6 2024-05-29)
binary: rustc
commit-hash: debd22da66cfa97c74040ebf68e420672ac8560e
commit-date: 2024-05-29
host: x86_64-apple-darwin
release: 1.80.0-nightly
LLVM version: 18.1.6
```

### Error output

Command: `rustc`

```
error: unconstrained opaque type
 --> r_layout_4A229A.rs:8:19
  |
8 |     type Assoc2 = impl std::fmt::Debug;
  |                   ^^^^^^^^^^^^^^^^^^^^
  |
  = note: `Assoc2` must be used in combination with a concrete type within the same impl
```

<!--
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_middle/src/ty/layout.rs:382:26: SizeSkeleton::compute(&Foo): layout errored (Unknown(Alias(Projection, AliasTy { args: [Foo], def_id: DefId(2:2141 ~ core[cbba]::ptr::metadata::Pointee::Metadata) }))), yet tail `<Bar as Trait>::Assoc2` is not a type parameter or a projection

thread 'rustc' panicked at compiler/rustc_middle/src/ty/layout.rs:382:26:
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_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
   3: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}
   4: 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}, !>
   5: rustc_middle::util::bug::bug_fmt
   6: <rustc_middle::ty::layout::SizeSkeleton>::compute
   7: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_transmutes
   8: rustc_hir_typeck::typeck
      [... omitted 1 frame ...]
   9: rustc_hir_analysis::check::check::check_item_type
  10: rustc_hir_analysis::check::wfcheck::check_well_formed
      [... omitted 1 frame ...]
  11: 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]>>, ()>
  12: rustc_hir_analysis::check::wfcheck::check_mod_type_wf
      [... omitted 1 frame ...]
  13: rustc_hir_analysis::check_crate
  14: rustc_interface::passes::run_required_analyses
  15: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  16: <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}>
  17: 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/240529_100chaos_tree_combine_typ/icefiles/rustc-ice-2024-05-30T06_54_29-96455.txt` to your bug report

query stack during panic:
#0 [typeck] type-checking `FOO2`
#1 [check_well_formed] checking that `FOO2` is well-formed
#2 [check_mod_type_wf] checking that types are well-formed in top-level module
#3 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 2 previous errors
```

</p>
</details>


### Note
- ICE location: `compiler/rustc_middle/src/ty/layout.rs L382-L385`
  https://github.com/rust-lang/rust/blob/debd22da66cfa97c74040ebf68e420672ac8560e/compiler/rustc_middle/src/ty/layout.rs#L373-L385
- Related ICE issues that points to identical ICE location:
  - #124031 (Fixed)
  - #109078 (Fixed)