Repository: rust
Issue: Compiler hangs indefinitely when using associated types instead of their explicit type.
Labels: A-trait-system,A-associated-items,T-compiler,C-bug,I-hang,T-types,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::marker::PhantomData;

pub trait DynQueriable<'a>: Sized
where
    QueryIter<'a, Self::Parts, Self::Raw, Self>: Iterator<Item = Self>,
{
    type Parts;
    type Raw: ?Sized + 'a;
}

pub trait QueryVariant<'a, Component: ?Sized + 'a> {}

pub struct QueryIter<'a, T, C: ?Sized, V: DynQueriable<'a, Raw = C>>
where
    QueryIter<'a, <V as DynQueriable<'a>>::Parts, C, V>: Iterator<Item = V>,
{
    p: PhantomData<fn(T, &'a C, V) -> (T, &'a C, V)>,
}

impl<'a, C: ?Sized, V: QueryVariant<'a, C>> Iterator for QueryIter<'a, &'a V, C, &'a C>
where
    &'a C: DynQueriable<'a, Raw = C>,
    QueryIter<'a, <&'a C as DynQueriable<'a>>::Parts, C, &'a C>: Iterator<Item = &'a C>,
{
    type Item = &'a C;
    fn next(&mut self) -> Option<Self::Item> {
        todo!()
    }
}

trait MyMarker {}

struct A;
impl MyMarker for A {}
impl<'a> QueryVariant<'a, dyn MyMarker + 'a> for A {}

impl<'a> DynQueriable<'a> for &'a dyn MyMarker {
    type Parts = &'a A;
    type Raw = dyn MyMarker + 'a;
}
```

I expected to see this happen: successful compilation.

Instead, this happened: The compiler hung and did not make any progress (i let it run for at least 5 minutes).

A slight modification of the above code results in it compiling without a problem:
line diffs:
```rust
// line 13 
pub struct QueryIter<'a, T, C: ?Sized, V: DynQueriable<'a, Raw = C>>
// replaced by
pub struct QueryIter<'a, T, C: ?Sized, V: DynQueriable<'a, Raw = C, Parts = T>>

// line 15
     QueryIter<'a, <V as DynQueriable<'a>>::Parts, C, V>: Iterator<Item = V>,
// replaced by
     Self: Iterator<Item = V>,

// lines 22-23
     &'a C: DynQueriable<'a, Raw = C>,
     QueryIter<'a, <&'a C as DynQueriable<'a>>::Parts, C, &'a C>: Iterator<Item = &'a C>,
// replaced by
     &'a C: DynQueriable<'a, Raw = C, Parts = &'a V>,
```

so the total modified code is:
```rust
use std::marker::PhantomData;

pub trait DynQueriable<'a>: Sized
where
    QueryIter<'a, Self::Parts, Self::Raw, Self>: Iterator<Item = Self>,
{
    type Parts;
    type Raw: ?Sized + 'a;
}

pub trait QueryVariant<'a, Component: ?Sized + 'a> {}

pub struct QueryIter<'a, T, C: ?Sized, V: DynQueriable<'a, Raw = C, Parts = T>>
where
    Self: Iterator<Item = V>,
{
    p: PhantomData<fn(T, &'a C, V) -> (T, &'a C, V)>,
}

impl<'a, C: ?Sized, V: QueryVariant<'a, C>> Iterator for QueryIter<'a, &'a V, C, &'a C>
where
    &'a C: DynQueriable<'a, Raw = C, Parts = &'a V>,
{
    type Item = &'a C;
    fn next(&mut self) -> Option<Self::Item> {
        todo!()
    }
}

trait MyMarker {}

struct A;
impl MyMarker for A {}
impl<'a> QueryVariant<'a, dyn MyMarker + 'a> for A {}

impl<'a> DynQueriable<'a> for &'a dyn MyMarker {
    type Parts = &'a A;
    type Raw = dyn MyMarker + 'a;
}
```
And that compiles without issues.

I searched through some issues and found #91498, the problem mentioned in [this](https://github.com/rust-lang/rust/issues/91498#issuecomment-998899433) comment seems related, however in my case the complier does not emit an error.

### 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.61.0-nightly (1d9c262ee 2022-03-26)
binary: rustc
commit-hash: 1d9c262eea411ec5230f8a4c9ba50b3647064da4
commit-date: 2022-03-26
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0
```