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

    <tr>
        <th>Summary</th>
        <td>
            Incorrect depth for SubstTemplateTypeParmType for alias template substitution.
        </td>
    </tr>

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

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

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

<pre>
    Our substitution of alias templates is leaving behind a SubstTemplateTypeParmType pointing to TemplateTypeParmType with incorrect depth.

Example: (https://godbolt.org/z/r3GPj7GGn)
```
template<class T> struct C {
  template<class U> using type1 = U (T);
};
using type2 = typename C<int>::type1<void>;
```
AST dump of type2:
```
`-TypeAliasDecl 0x56240cfed3e8 <line:4:1, col:42> col:7 type2 'typename C<int>::type1<void>':'void (int)'
  `-ElaboratedType 0x56240cfed340 'typename C<int>::type1<void>' sugar
    `-TemplateSpecializationType 0x56240cfce350 'type1<void>' sugar alias type1
      |-TemplateArgument type 'void'
      | `-BuiltinType 0x56240cf831b0 'void'
      `-FunctionProtoType 0x56240cfce2f0 'void (int)' cdecl
        |-SubstTemplateTypeParmType 0x56240cfce290 'void' sugar
        | |-TemplateTypeParmType 0x56240cfcdfa0 'U' dependent depth 0 index 0
        | | `-TemplateTypeParm 0x56240cfcdf50 'U'
        | `-BuiltinType 0x56240cf831b0 'void'
        `-SubstTemplateTypeParmType 0x56240cfcdfe0 'int' sugar
          |-TemplateTypeParmType 0x56240cfcd590 'T' dependent depth 0 index 0
          | `-TemplateTypeParm 0x56240cfcd510 'T'
          `-BuiltinType 0x56240cf83250 'int'
```
Ie notice in the AST both T and U are at the same depth, 0.

This happens because we substitute T early into the alias, which pulls U down one level, and then we substitute U.
This flattens everything down to ground level.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJydVdty4jgQ_Rrz0jWUsDGGBz8QSFLztKk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