Repository: rust
Issue: Infinite recursion when solving bounds
Labels: A-trait-system,T-compiler,C-bug



I tried this [code](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=2c2d11cfd7b21de02275d948acbf36ff):

```rust
use std::fmt::{self, Debug};

pub trait ExtendableCommon {
    type Item;
    type SubA;
    type SubB;
    
    type ExtendableSubtype: ExtendableSubtype;
}

pub trait ExtendableSubtype {
    type SubA;
    type SubB;

    type ExtendableSubA: ExtendableSubA;
    type ExtendableSubB: ExtendableSubB;
}

pub trait ExtendableSubA {
    type Item;
    type ExtendableCommon: ExtendableCommon;
}

pub trait ExtendableSubB {
    type Item;
    type ExtendableCommon: ExtendableCommon;
}

pub struct Common<Other: ExtendableCommon> {
    pub subtype: Subtype<Other::ExtendableSubtype>,
    pub other: Other::Item,
}

pub enum Subtype<Other: ExtendableSubtype> {
    A(SubA<Other::ExtendableSubA>),
    B(SubB<Other::ExtendableSubB>),
}

pub struct SubA<Other: ExtendableSubA> {
    pub a: i32,
    pub children: Vec<Common<Other::ExtendableCommon>>,
    pub other: Other::Item,
}

pub struct SubB<Other: ExtendableSubB> {
    pub b: f64,
    pub children: Vec<Common<Other::ExtendableCommon>>,
    pub other: Other::Item,
}

impl<Other> Debug for Common<Other>
where
    Other: ExtendableCommon,
    Other::Item: Debug,
    Subtype<Other::ExtendableSubtype>: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Common")
            .field("subtype", &self.subtype)
            .field("other", &self.other)
            .finish()
    }
}

impl<Other> Debug for Subtype<Other>
where
    Other: ExtendableSubtype,
    SubA<Other::ExtendableSubA>: Debug,
    SubB<Other::ExtendableSubB>: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::A(arg0) => f.debug_tuple("A").field(arg0).finish(),
            Self::B(arg0) => f.debug_tuple("B").field(arg0).finish(),
        }
    }
}

impl<Other> Debug for SubA<Other>
where
    Other: ExtendableSubA,
    Other::Item: Debug,
    Other::ExtendableCommon: ExtendableCommon,
    Common<Other::ExtendableCommon>: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SubA")
            .field("a", &self.a)
            .field("children", &self.children)
            .field("other", &self.other)
            .finish()
    }
}

impl<Other> Debug for SubB<Other>
where
    Other: ExtendableSubB,
    Other::Item: Debug,
    Other::ExtendableCommon: ExtendableCommon,
    Common<Other::ExtendableCommon>: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SubB")
            .field("b", &self.b)
            .field("children", &self.children)
            .field("other", &self.other)
            .finish()
    }
}

pub fn test() {
    dbg!(Common {
        subtype: Subtype::A(SubA {
            a: 42,
            other: (),
            children: vec![Common {
                subtype: Subtype::B(SubB {
                    b: 32.,
                    other: (),
                    children: vec![],
                }),
                other: (),
            }],
        }),
        other: (),
    });
}
```

I expected to see this happen: 
> The code builds or rustc explains why the bounds are impossible/wrong.

Instead, this happened: 
> rustc seems to keep using ~100% cpu and consumes all the memory available.

Reproduced with current nightly (`rustc 1.64.0-nightly (7665c3543 2022-07-06)`) and stable (`rustc 1.62.0 (a8314ef7d 2022-06-27)`).

CC: @dodomorandi that original found the problem and minimized it