Skip to content

Commit 6f92313

Browse files
authored
[Clang][P1061] Fix template arguments in local classes (#121225)
In the development of P1061 (Structured Bindings Introduce a Patch), I found this bug in the template instantiation of a local class. The issue is caused by the instantiation of the original template and not the partially instantiated template. In the example (sans the fix) the instantiation uses the first template parameter from the previous instantiation and not the current one so the error hits an assertion when it is expecting an NTTP. If they were both types then it might gladly accept the type from the wrong template which is kind of scary. In the test, the reference to `i` is substituted with a placeholder AST object that represents the resolved value when instantiating `g`. However, since the old template is used, the instantiation sees an AST object that only contains the template argument index in the context of instantiating the lambda which has a type template parameter (ie auto). I question if we should use `getTemplateInstantiationPattern` at all here. Other errors involving local classes in nested templates could also be caused by the misuse of this function (because it gets the uninstantiated template).
1 parent e2ddd14 commit 6f92313

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ Bug Fixes to C++ Support
955955
consistently treat the initializer as manifestly constant-evaluated.
956956
(#GH135281)
957957
- Fix a crash in the presence of invalid base classes. (#GH147186)
958+
- Fix a crash with NTTP when instantiating local class.
958959

959960
Bug Fixes to AST Handling
960961
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4412,8 +4412,12 @@ Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
44124412
// No need to instantiate in-class initializers during explicit
44134413
// instantiation.
44144414
if (Field->hasInClassInitializer() && TSK == TSK_ImplicitInstantiation) {
4415+
// Handle local classes which could have substituted template params.
44154416
CXXRecordDecl *ClassPattern =
4416-
Instantiation->getTemplateInstantiationPattern();
4417+
Instantiation->isLocalClass()
4418+
? Instantiation->getInstantiatedFromMemberClass()
4419+
: Instantiation->getTemplateInstantiationPattern();
4420+
44174421
DeclContext::lookup_result Lookup =
44184422
ClassPattern->lookup(Field->getDeclName());
44194423
FieldDecl *Pattern = Lookup.find_first<FieldDecl>();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -fsyntax-only %s -verify
2+
// expected-no-diagnostics
3+
4+
template <int i>
5+
int g() {
6+
return [] (auto) -> int {
7+
struct L {
8+
int m = i;
9+
};
10+
return 0;
11+
} (42);
12+
}
13+
14+
int v = g<1>();

0 commit comments

Comments
 (0)