[clang] [Clang] Fix a wrong diagnostic about a local variable inside a lambda in unevaluated context need capture (PR #165098)

Zhikai Zeng via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 1 03:34:56 PDT 2025


https://github.com/Backl1ght updated https://github.com/llvm/llvm-project/pull/165098

>From 146a9932614479017c418bb134f27c1b03c29fe1 Mon Sep 17 00:00:00 2001
From: Backl1ght <backlight.zzk at gmail.com>
Date: Sat, 25 Oct 2025 14:25:55 +0000
Subject: [PATCH 1/4] fix

---
 clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 28925cca8f956..439a5317f06f1 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -7061,8 +7061,15 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
     // anonymous unions in class templates).
   }
 
-  if (!ParentDependsOnArgs)
+  if (!ParentDependsOnArgs) {
+    if (auto Found =
+            CurrentInstantiationScope
+                ? CurrentInstantiationScope->getInstantiationOfIfExists(D)
+                : nullptr) {
+      return cast<NamedDecl>(Found->dyn_cast<Decl *>());
+    }
     return D;
+  }
 
   ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
   if (!ParentDC)

>From 237619a4f44aa5d2b8e6976d189751f228ad39c2 Mon Sep 17 00:00:00 2001
From: Backl1ght <backlight.zzk at gmail.com>
Date: Sat, 25 Oct 2025 15:18:42 +0000
Subject: [PATCH 2/4] add UT and release note

---
 clang/docs/ReleaseNotes.rst               |  2 ++
 clang/test/SemaCXX/lambda-unevaluated.cpp | 19 +++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 92fc9381a5868..73fd07927cd2c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -432,6 +432,8 @@ Bug Fixes in This Version
 - Fixed a failed assertion with empty filename arguments in ``__has_embed``. (#GH159898)
 - Fixed a failed assertion with empty filename in ``#embed`` directive. (#GH162951)
 - Fixed a crash triggered by unterminated ``__has_embed``. (#GH162953)
+- Fixed a wrong diagnostic about a local variable inside a lambda in unevaluated
+  contexts need capture. (#GH163837)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/test/SemaCXX/lambda-unevaluated.cpp b/clang/test/SemaCXX/lambda-unevaluated.cpp
index 9289fc9ec1243..4b0e1abdb38f0 100644
--- a/clang/test/SemaCXX/lambda-unevaluated.cpp
+++ b/clang/test/SemaCXX/lambda-unevaluated.cpp
@@ -282,3 +282,22 @@ static_assert(__is_same_as(int, helper<int>));
 
 
 } // namespace GH138018
+
+namespace GH163837 {
+
+template<int N>
+void f();
+
+template<class>
+struct X {
+    using type = decltype([](auto) {
+        f<[]{
+            int result = 0;
+            return result;
+        }()>();
+    }(0));
+};
+
+X<void> l;
+
+} // namespace GH163837

>From a9379c781ec9c9cc3c08605697c05a1712e931ba Mon Sep 17 00:00:00 2001
From: Backl1ght <backlight.zzk at gmail.com>
Date: Sat, 25 Oct 2025 15:44:00 +0000
Subject: [PATCH 3/4] fix typo

---
 clang/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 73fd07927cd2c..d3fc850d48fb1 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -433,7 +433,7 @@ Bug Fixes in This Version
 - Fixed a failed assertion with empty filename in ``#embed`` directive. (#GH162951)
 - Fixed a crash triggered by unterminated ``__has_embed``. (#GH162953)
 - Fixed a wrong diagnostic about a local variable inside a lambda in unevaluated
-  contexts need capture. (#GH163837)
+  context needs capture. (#GH163837)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

>From 8bae15e65e80a1595f796701b9db98f2e63ac993 Mon Sep 17 00:00:00 2001
From: Backl1ght <backlight.zzk at gmail.com>
Date: Sat, 1 Nov 2025 10:31:02 +0000
Subject: [PATCH 4/4] address comment

---
 clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 439a5317f06f1..da91f09c25d51 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -7062,11 +7062,12 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
   }
 
   if (!ParentDependsOnArgs) {
-    if (auto Found =
-            CurrentInstantiationScope
-                ? CurrentInstantiationScope->getInstantiationOfIfExists(D)
-                : nullptr) {
-      return cast<NamedDecl>(Found->dyn_cast<Decl *>());
+    if (CurrentInstantiationScope) {
+      if (llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *Found =
+          CurrentInstantiationScope->getInstantiationOfIfExists(D)) {
+        if (Decl* FD = Found->dyn_cast<Decl *>())
+          return cast<NamedDecl>(FD);
+      }
     }
     return D;
   }



More information about the cfe-commits mailing list