[clang] don't instantiate templates with injected arguments
authorMatheus Izvekov <mizvekov@gmail.com>
Wed, 29 Sep 2021 13:23:30 +0000 (15:23 +0200)
committerMatheus Izvekov <mizvekov@gmail.com>
Wed, 29 Sep 2021 21:19:13 +0000 (23:19 +0200)
commitaf10d6f350ff3e92c6ffae66cc9dac36884cdd55
tree3d415b7a3aa0f002f15dd8c1148c8b0e7c2640d0
parentb852013dd729a74b57cfb098c441f84ea3431498
[clang] don't instantiate templates with injected arguments

There is a special situation with templates in local classes,
as can be seen in this example with generic lambdas in function scope:
```
template<class T1> void foo() {
    (void)[]<class T2>() {
      struct S {
        void bar() { (void)[]<class T3>(T2) {}; }
      };
    };
};
template void foo<int>();
```

As a consequence of the resolution of DR1484, bar is instantiated during the
substitution of foo, and in this context we would substitute the lambda within
it with it's own parameters "injected" (turned into arguments).

This can't be properly dealt with for at least a couple of reasons:
* The 'TemplateTypeParm' type itself can only deal with canonical replacement
  types, which the injected arguments are not.
* If T3 were constrained in the example above, our (non-conforming) eager
  substitution of type constraints would just leave that parameter dangling.

Instead of substituting with injected parameters, this patch just leaves those
inner levels unreplaced.

Since injected arguments appear to be unused within the users of
`getTemplateInstantiationArgs`, this patch just removes that support there and
leaves a couple of asserts in place.

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>
Reviewed By: rsmith

Differential Revision: https://reviews.llvm.org/D110727
clang/lib/Sema/SemaTemplateInstantiate.cpp
clang/test/SemaTemplate/generic-lambda.cpp [new file with mode: 0644]