Repository: rust
Issue: Infinite recursion in type constraint resolution
Labels: A-trait-system,T-compiler,A-inference,C-bug,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.
-->

I tried this code:

```rust
use std::borrow::Borrow;

trait A {
    const XXX: usize = 1;
}

struct Special<U>(U);

struct Wrapper<T, const N: usize>([T; N]);

impl<T, const N: usize> A for Special<Wrapper<T, N>>
where
    Special<T>: A
{}

impl<T: Borrow<str>> A for Special<T> {
}

fn main() {
    let _ = <Special<Wrapper<String, 1>> as A>::XXX;
}
```

I expected to see this happen: the code should compile

Instead, this happened:

```
error[E0275]: overflow evaluating the requirement `Special<Wrapper<_, _>>: A`
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`blah`)
note: required for `Special<Wrapper<Wrapper<_, _>, _>>` to implement `A`
  --> blah.rs:11:25
   |
11 | impl<T, const N: usize> A for Special<Wrapper<T, N>>
   |                         ^     ^^^^^^^^^^^^^^^^^^^^^^
12 | where
13 |         Special<T>: A
   |                     - unsatisfied trait bound introduced here
   = note: 126 redundant requirements hidden
   = note: required for `Special<Wrapper<Wrapper<Wrapper<Wrapper<Wrapper<..., _>, _>, _>, _>, _>>` to implement `A`
   = note: the full name for the type has been written to 'blah.long-type-12463274173695242483.txt'
   = note: consider using `--verbose` to print the full type name to the console

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0275`.
```

### 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.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary>Backtrace</summary>
<p>

```
error[E0275]: overflow evaluating the requirement `Special<Wrapper<_, _>>: A`
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`blah`)
note: required for `Special<Wrapper<Wrapper<_, _>, _>>` to implement `A`
  --> blah.rs:11:25
   |
11 | impl<T, const N: usize> A for Special<Wrapper<T, N>>
   |                         ^     ^^^^^^^^^^^^^^^^^^^^^^
12 | where
13 |         Special<T>: A
   |                     - unsatisfied trait bound introduced here
   = note: 126 redundant requirements hidden
   = note: required for `Special<Wrapper<Wrapper<Wrapper<Wrapper<Wrapper<..., _>, _>, _>, _>, _>>` to implement `A`
   = note: the full name for the type has been written to 'blah.long-type-12463274173695242483.txt'
   = note: consider using `--verbose` to print the full type name to the console

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0275`.
```

</p>
</details>