[clang] [clang] Fix name resolution bug with -fdelayed-template-parsing (PR #241134)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 10 23:14:32 PST 2026


https://github.com/higher-performance updated https://github.com/llvm/llvm-project/pull/241134

>From d7f9fe76e8eca9b63bbd0470450d528be944a34d Mon Sep 17 00:00:00 2001
From: higher-performance <higher.performance.github at gmail.com>
Date: Wed, 11 Nov 2026 00:23:40 -0500
Subject: [PATCH] [clang] Fix name resolution bug with
 -fdelayed-template-parsing

Fixes #94585
---
 clang/docs/ReleaseNotes.md                   |  6 +++
 clang/lib/Sema/SemaTemplate.cpp              | 15 ++------
 clang/test/Parser/DelayedTemplateParsing.cpp | 39 ++++++++++++++++++++
 3 files changed, 49 insertions(+), 11 deletions(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 0f9e7e01636d71..f4cf6910044093 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1221,6 +1221,12 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
   argument of `std::addressof`, and an unresolvable overload set is diagnosed.
   (#GH77928)
 
+- Fixed a bug where, with `-fdelayed-template-parsing`, a member function of a
+  local class could not name declarations local to an enclosing templated
+  function when the class was nested in a lambda, in a member function of
+  another local class, or in a non-template member function of a class
+  template.
+
 #### Bug Fixes to AST Handling
 
 - Fixed a non-deterministic ordering of unused local typedefs that made
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 0caf7593ca9d5b..dafc865021c22a 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -11846,17 +11846,10 @@ void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) {
 }
 
 bool Sema::IsInsideALocalClassWithinATemplateFunction() {
-  DeclContext *DC = CurContext;
-
-  while (DC) {
-    if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
-      const FunctionDecl *FD = RD->isLocalClass();
-      return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
-    } else if (DC->isTranslationUnit() || DC->isNamespace())
-      return false;
-
-    DC = DC->getParent();
-  }
+  for (DeclContext *DC = CurContext; DC && !DC->isFileContext();
+       DC = DC->getParent())
+    if (DC->isFunctionOrMethod() && DC->isDependentContext())
+      return true;
   return false;
 }
 
diff --git a/clang/test/Parser/DelayedTemplateParsing.cpp b/clang/test/Parser/DelayedTemplateParsing.cpp
index 072c7ce5162ee3..237565af7241fd 100644
--- a/clang/test/Parser/DelayedTemplateParsing.cpp
+++ b/clang/test/Parser/DelayedTemplateParsing.cpp
@@ -204,3 +204,42 @@ namespace LateParsedAttrs {
     f<int>(1); // expected-error {{foo}}
   }
 } // namespace LateParsedAttrs
+
+namespace LocalClassUsesEnclosingLocals {
+struct InLambda {
+  template <class = void> static void foo() {
+    struct S {};
+    auto const g = []() {
+      struct H {
+        static void f() { typedef S S; }
+      };
+    };
+  }
+};
+
+template <class> struct InClassTemplateMember {
+  static void foo() {
+    struct S {};
+    struct H {
+      static void f() { typedef S S; }
+    };
+  }
+};
+
+template <class = void> void InLocalClassMember() {
+  struct S {};
+  struct A {
+    static void m() {
+      struct H {
+        static void f() { typedef S S; }
+      };
+    }
+  };
+}
+
+void trigger() {
+  InLambda::foo();
+  InClassTemplateMember<int>::foo();
+  InLocalClassMember();
+}
+} // namespace LocalClassUsesEnclosingLocals



More information about the cfe-commits mailing list