Repository: rust
Issue: -Znext-solver=coherence: stack overflow
Labels: T-compiler,C-bug,I-hang,WG-trait-system-refactor

<!--
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
#![recursion_limit="1024"]
#![allow(dead_code)]

use std::mem;

pub struct S0<T>(T,T);
pub struct S1<T>(Option<Box<S0<S0<T>>>>,Option<Box<S0<S0<S1<S1<T>>>>>>);
pub struct S2<T>(Option<Box<T>>,Option<S3<T>>);
pub struct S3<T>(S5<T>,Option<Box<S2<S2<T>>>>);
pub struct S4<T>(Option<Box<S3<S3<T>>>>,Option<Box<S3<S3<T>>>>);
pub struct S5<T>(Option<Box<S4<S4<T>>>>,Option<Box<S4<S4<T>>>>,Option<T>);

trait Foo { fn xxx(&self); }
/// some local of #[fundamental] trait
trait Bar {}

impl<T> Foo for T where T: Sync, T: Bar {
    fn xxx(&self) {}
}

impl Foo for S5<u8> { fn xxx(&self) {} }

fn main() {
    let s = S5(None,None,None);
    s.xxx();
    assert_eq!(mem::size_of_val(&s.2), mem::size_of::<Option<u8>>());
}

```


### 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.77.0-nightly (ce1f2ccf5 2024-01-12)
binary: rustc
commit-hash: ce1f2ccf5a5ac9343623bd115a05e4151d93af0d
commit-date: 2024-01-12
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6
```

### 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: rustc interrupted by SIGSEGV, printing backtrace

/home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-b35442033d0dc6f2.so(+0x2d6cc16)[0x7f1df876cc16]
/usr/lib/libc.so.6(+0x3e710)[0x7f1df585c710]
/home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-b35442033d0dc6f2.so(+0x37dff57)[0x7f1df91dff57]

### cycle encountered after 3 frames with period 6
/home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-b35442033d0dc6f2.so(+0x378a11c)[0x7f1df918a11c]
/home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-b35442033d0dc6f2.so(+0x379b388)[0x7f1df919b388]
/home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-b35442033d0dc6f2.so(+0x37e05ec)[0x7f1df91e05ec]
/home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-b35442033d0dc6f2.so(+0x378a11c)[0x7f1df918a11c]
/home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-b35442033d0dc6f2.so(+0x379b388)[0x7f1df919b388]
/home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-b35442033d0dc6f2.so(+0x37e05ec)[0x7f1df91e05ec]
### recursed 42 times

/home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-b35442033d0dc6f2.so(+0x378a11c)[0x7f1df918a11c]

note: rustc unexpectedly overflowed its stack! this is a bug
note: maximum backtrace depth reached, frames may have been lost
note: we would appreciate a report at https://github.com/rust-lang/rust
[1]    3560606 segmentation fault  rustc ff1f66b05db64ab43484975ddfc23cfa38b36db7.rs -Znext-solver=coherence
```

</p>
</details>