https://github.com/ricejasonf updated 
https://github.com/llvm/llvm-project/pull/121225

>From 5be8b4b38cb4d595d5e1ec53bdcda3bb24333644 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/docs/ReleaseNotes.rst                        |  1 +
 clang/lib/Sema/SemaTemplateInstantiate.cpp         |  6 +++++-
 .../SemaCXX/local-class-template-param-crash.cpp   | 14 ++++++++++++++
 3 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/local-class-template-param-crash.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8f72553acfa4c..8ba493b2ca89b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -955,6 +955,7 @@ Bug Fixes to C++ Support
   consistently treat the initializer as manifestly constant-evaluated.
   (#GH135281)
 - Fix a crash in the presence of invalid base classes. (#GH147186)
+- Fix a crash with NTTP when instantiating local class.
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp 
b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index f04b01f64b960..20bac0e56b195 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>();
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 0000000000000..ffa8590eaf77d
--- /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