Repository: rust
Issue: Compiling into infinite recursion and running out of memory causes the process to exit abnormally.
Labels: A-lifetimes,A-trait-system,A-associated-items,T-compiler,C-bug,I-hang,fixed-by-next-solver

<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.
-->

The following code will fall into infinite recursion and be killed by the system (return code: 137):

```rust
trait MyTrait<A: ?Sized>
where
    for<'a> A: MyTrait<&'a Self>,
    for<'a> &'a Self: MyTrait<Self>,
{
}

impl<U, T> MyTrait<U> for T
where
    for<'a> T: MyTrait<&'a U>,
    for<'a> &'a T: MyTrait<U>,
{
}
fn main(){
    
}
```

We would expect the compiler to automatically terminate the compilation and give us a message telling us to change the implementation of the code when we encounter such a problem, but in fact the compiler runs out of memory during the compilation and causes the process to exit abnormally.🥲

### 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.74.0-nightly (8550f15e1 2023-08-27)
binary: rustc
commit-hash: 8550f15e148407159af401e02b1d9259762b3496
commit-date: 2023-08-27
host: x86_64-unknown-linux-gnu
release: 1.74.0-nightly
LLVM version: 17.0.0
```
When we prefix the code with `#! [recursion_limit = "20"]`, we get the following message:
<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary>Error Output</summary>
<p>

```
error[E0275]: overflow evaluating the requirement `for<'a> &'a &'a &'a &'a &'a &'a &'a ...: Sized`
 --> code.rs:4:16
  |
4 |     for<'a> A: MyTrait<&'a Self>,
  |                ^^^^^^^^^^^^^^^^^
  |
  = help: consider increasing the recursion limit by adding a `#![recursion_limit = "40"]` attribute to your crate (`code`)
note: required for `&'a A` to implement `for<'a, 'a> MyTrait<&'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a A>`
 --> code.rs:9:12
  |
9 | impl<U, T> MyTrait<U> for T
  |      -     ^^^^^^^^^^     ^
  |      |
  |      unsatisfied trait bound introduced here
  = note: 20 redundant requirements hidden
  = note: required for `&'a A` to implement `for<'a> MyTrait<A>`
note: required by a bound in `MyTrait`
 --> code.rs:5:23
  |
2 | trait MyTrait<A:?Sized>
  |       ------- required by a bound in this trait
...
5 |     for<'a> &'a Self: MyTrait<Self>,
  |                       ^^^^^^^^^^^^^ required by this bound in `MyTrait`

error[E0275]: overflow evaluating the requirement `for<'a> T: MyTrait<&'a &'a &'a &'a &'a &'a ...>`
  --> code.rs:9:27
   |
9  | impl<U, T> MyTrait<U> for T
   |                           ^
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "40"]` attribute to your crate (`code`)
note: required for `T` to implement `for<'a> MyTrait<&'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a &'a U>`
  --> code.rs:9:12
   |
9  | impl<U, T> MyTrait<U> for T
   |            ^^^^^^^^^^     ^
10 | where
11 |     for<'a> T: MyTrait<&'a U>,
   |                -------------- unsatisfied trait bound introduced here
   = note: 19 redundant requirements hidden
   = note: required for `T` to implement `MyTrait<U>`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0275`.
```
</p>
</details>
```[tasklist]
### Tasks
```