Repository: rust
Issue: Ordering of bounds matters with GAT/HRTB
Labels: A-lifetimes,A-trait-system,T-compiler,A-inference,C-bug,A-GATs,A-higher-ranked

<!--
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:

([playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=90f4f6ce698b33668ee8dd34e379cb04))

```rust
#![feature(generic_associated_types)]
use std::marker::PhantomData as PhD;

pub trait Universe: 'static {
    type Ty<'a>;
}

impl Universe for &'static u8 {
    type Ty<'a> = &'a u8;
}

trait Service<Req> {}

struct BadCombinator<ReqU, S>(PhD<ReqU>, S);

impl<'c, ReqU, S> Service<ReqU::Ty<'c>> for BadCombinator<ReqU, S>
where
    ReqU: Universe,
    S: for<'a> Service<ReqU::Ty<'a>>,
    S: Service<ReqU>, // swapping the last two bounds works!
{
}

fn test(svc: impl for<'a> Service<&'a u8>) {
    fn assert_good(_: impl for<'a> Service<&'a u8>) {}

   // assert_good(BadCombinator(PhD::<&u8>, svc)); //<- works
    assert_good(BadCombinator(PhD, svc));
}
```

I expected to see this happen: program either compiles or requires type annotation for `ReqU`.

Instead, this happened: The program fails with a message unrelated to type inference. While reording the bounds as noted or annotating for `ReqU` makes it compile.

The error message looks pretty confusing as it seems that the compiler was able to infer `ReqU`!

<details><summary><b>Error Output</b></summary>
<p>

```
error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): the trait bound `for<'a> impl for<'a> Service<&'a u8>: Service<<&u8 as Universe>::Ty<'a>>` is not satisfied
  [--> src/lib.rs:28:17
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=7d6db0f5036d46fc55706aed2d6eb64c#)   |
28 |     assert_good(BadCombinator(PhD, svc));
   |     ----------- ^^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'a> Service<<&u8 as Universe>::Ty<'a>>` is not implemented for `impl for<'a> Service<&'a u8>`
   |     |
   |     required by a bound introduced by this call
   |
note: required because of the requirements on the impl of `for<'a> Service<&'a u8>` for `BadCombinator<&u8, impl for<'a> Service<&'a u8>>`
  [--> src/lib.rs:16:19
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=7d6db0f5036d46fc55706aed2d6eb64c#)   |
16 | impl<'c, ReqU, S> Service<ReqU::Ty<'c>> for BadCombinator<ReqU, S>
   |                   ^^^^^^^^^^^^^^^^^^^^^     ^^^^^^^^^^^^^^^^^^^^^^
note: required by a bound in `assert_good`
  [--> src/lib.rs:25:28
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=7d6db0f5036d46fc55706aed2d6eb64c#)   |
25 |     fn assert_good(_: impl for<'a> Service<&'a u8>) {}
   |                            ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `assert_good`
help: consider further restricting this bound
   |
24 | pub fn test(svc: impl for<'a> Service<&'a u8> + for<'a> Service<<&u8 as Universe>::Ty<'a>>) {
   |                                               ++++++++++++++++++++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error
```

</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.
-->

Reproduced on nightly versions back to `2020-05-01`

Current nightly:
```
1.62.0-nightly (2022-04-21 de1bc0008be096cf7ed6)
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->

@rustbot label  F-generic_associated_types A-inference T-compiler