<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/148610>148610</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang] Compiler fails to detect infinite recursion in `A<T, T>`-style nested templates
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          GitSparTV
      </td>
    </tr>
</table>

<pre>
    ```cpp
template <typename T, typename U = T>
class A {};

template <typename T>
void RecursiveFunction(T t) {
    RecursiveFunction(A<T>());
}

int main() { RecursiveFunction(42); }
```

> Demo: https://godbolt.org/z/jbTWh1Kej

Compiler is able to detect recursion in cases like $\text{A<...A\<T\>...>}$, but not in $\text{A<...A\<T\>..., ...A\<T\>...>}$.
In other words this is what happens in the example: $T_0 = \texttt{B}, \quad T_{n+1} = \texttt{A<}T_n,\ T_n\texttt{>}$

All major compilers don't generate any information, just eating RAM and stack space.

Another example where the generated template can be examined: https://godbolt.org/z/MKdcqbqjW

Here you can see `Add` function is instantiated with:
`unique_ptr<D, D>` -> `unique_ptr<unique_ptr<D, D>, unique_ptr<D, D>>` -> `unique_ptr<unique_ptr<unique_ptr<D, D>, unique_ptr<D, D>>, unique_ptr<unique_ptr<D, D>, unique_ptr<D, D>>>` -> and so on

This bug was found when using real `std::unique_ptr`, since it has 2 parameters both of which are dependent on `T`, current recursion detection doesn't work.

Real example: https://godbolt.org/z/6TK94576j
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVU1v4zYQ_TXjyyCCRH3ZBx1kOW6LYC-p2j0GlDS26FCkQlLxpr--oORN3HTTBgsYkCxy3sx78zjk1oqjIiog3UK6W_HJ9doUvwj3-8hN_eeq0d1LAVm4_NpxhLB0NIySO0KIK_cykuIDYQ2swtd_fyDEO6whvoWwbCW3FkuEfAv5DuIthOXHMHPIsxYd3lM7GS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