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

    <tr>
        <th>Summary</th>
        <td>
            CTAD: crash on erroneous alias templates
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            c++20,
            clang:frontend,
            crash-on-invalid
      </td>
    </tr>

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

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

<pre>
    Given the following incorrect code:

```
template <typename T>
class Foo {};

template <typename T>
template <typename K>
using Bar2 = Foo<K>; // error: Extraneous template parameter list in alias template declaration

Bar2 b = 1;
```

The alias template declaration is invalid because of the extra template parameter list, clang still builds the decl for error recovery, the AST looks like:

```
-TypeAliasTemplateDecl 0x55d41b04fc00 <line:33:1, line:35:19> col:1 invalid Bar2
| |-TemplateTypeParmDecl 0x55d41b04f910 <line:33:10, col:19> col:19 typename depth 0 index 0 T
| `-TypeAliasDecl 0x55d41b04fba0 <line:35:1, col:19> col:7 Bar2 'Foo<K>'
| `-ElaboratedType 0x55d41b04fb30 'Foo<K>' sugar dependent
| `-TemplateSpecializationType 0x55d41b04fae0 'Foo<K>' dependent Foo
| `-TemplateArgument type 'K'
|         `-TemplateTypeParmType 0x55d41b04fa30 'K' dependent depth 1 index 0
| `-TemplateTypeParm 0x55d41b04f9a8 'K'
```

The `TypeAliasTemplateDecl` is ill-formed for this case -- the template parameter list of the alias decl is the first written one (which is `T`), while in the alias decl body, we have nodes that refer to the template parameter `K`. And the invalid bit of the alias decl is not flagged.

This ill-formed AST node breaks invariant for the downstream AST consumer. The crash inside CTAD is a symptom. We should at least mark the decl invalid for this case.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVUtv6jgU_jVmcwRybAhkkQWPMotuRrpIs3bsE-KpYyPb6WN-_chJoIS291YVLT72933nLULQZ4tYktWOMNa4F9SWMEZW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