diff options
author | Jason Rice <ricejasonf@gmail.com> | 2025-07-12 20:17:41 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-13 06:17:41 +0300 |
commit | 6f923134ddf4afc4266c4c32854d7cc2793c23a1 (patch) | |
tree | 5ebee5300900ba0e77f44340d1277a69e9cb42ef /clang/lib/Sema/SemaTemplateInstantiate.cpp | |
parent | e2ddd147a518b151f764a6b4e9ef99ae9e2050a3 (diff) | |
download | llvm-6f923134ddf4afc4266c4c32854d7cc2793c23a1.zip llvm-6f923134ddf4afc4266c4c32854d7cc2793c23a1.tar.gz llvm-6f923134ddf4afc4266c4c32854d7cc2793c23a1.tar.bz2 |
[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).
Diffstat (limited to 'clang/lib/Sema/SemaTemplateInstantiate.cpp')
-rw-r--r-- | clang/lib/Sema/SemaTemplateInstantiate.cpp | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp index f04b01f..20bac0e 100644 --- a/clang/lib/Sema/SemaTemplateInstantiate.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -4412,8 +4412,12 @@ Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, // No need to instantiate in-class initializers during explicit // instantiation. if (Field->hasInClassInitializer() && TSK == TSK_ImplicitInstantiation) { + // Handle local classes which could have substituted template params. CXXRecordDecl *ClassPattern = - Instantiation->getTemplateInstantiationPattern(); + Instantiation->isLocalClass() + ? Instantiation->getInstantiatedFromMemberClass() + : Instantiation->getTemplateInstantiationPattern(); + DeclContext::lookup_result Lookup = ClassPattern->lookup(Field->getDeclName()); FieldDecl *Pattern = Lookup.find_first<FieldDecl>(); |