https://github.com/ricejasonf created 
https://github.com/llvm/llvm-project/pull/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).


>From f1058d13315682b8bd6c3ac06a3225060d11ec61 Mon Sep 17 00:00:00 2001
From: Jason Rice <ricejas...@gmail.com>
Date: Mon, 5 Aug 2024 13:53:33 -0700
Subject: [PATCH] [Clang][P1061] Fix template arguments in local classes

---
 clang/lib/Sema/SemaTemplateInstantiate.cpp         |  6 +++++-
 .../SemaCXX/local-class-template-param-crash.cpp   | 14 ++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/local-class-template-param-crash.cpp

diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp 
b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index fb0f38df62a744..baa5ff35295349 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -4433,8 +4433,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>();
diff --git a/clang/test/SemaCXX/local-class-template-param-crash.cpp 
b/clang/test/SemaCXX/local-class-template-param-crash.cpp
new file mode 100644
index 00000000000000..ffa8590eaf77d8
--- /dev/null
+++ b/clang/test/SemaCXX/local-class-template-param-crash.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fsyntax-only %s -verify
+// expected-no-diagnostics
+
+template <int i>
+int g() {
+  return [] (auto) -> int {
+    struct L {
+      int m = i;
+    };
+    return 0;
+  } (42);
+}
+
+int v = g<1>();

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to