[clang] [Clang] prevent assertion failure by avoiding always-dependent lambdas in constraint-related nested scopes (PR #173776)
Oleksandr T. via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 5 03:56:06 PST 2026
https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/173776
>From 0e6bc4ff732cfcaf5d2ba445dd25f8064da128e3 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <oleksandr.tarasiuk at outlook.com>
Date: Sun, 28 Dec 2025 14:15:42 +0200
Subject: [PATCH 1/3] [Clang] prevent assertion failure by avoiding
always-dependent lambdas in constraint-related nested scopes
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaLambda.cpp | 14 +++++++++-----
clang/test/SemaCXX/lambda-unevaluated.cpp | 9 ++++++++-
3 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2319ff13f7864..1ae44bf610221 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -623,6 +623,7 @@ Bug Fixes to C++ Support
- Fix the result of ``__is_pointer_interconvertible_base_of`` when arguments are qualified and passed via template parameters. (#GH135273)
- Fixed a crash when evaluating nested requirements in requires-expressions that reference invented parameters. (#GH166325)
- Fixed a crash when standard comparison categories (e.g. ``std::partial_ordering``) are defined with incorrect static member types. (#GH170015) (#GH56571)
+- Fixed an assertion failure triggered by nested lambdas during capture handling. (#GH172814)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index 81ec0b18dedfd..53f2612a46bf4 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -1102,7 +1102,7 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
CXXRecordDecl::LDK_Unknown;
if (CurScope->getTemplateParamParent() != nullptr) {
LambdaDependencyKind = CXXRecordDecl::LDK_AlwaysDependent;
- } else if (Scope *P = CurScope->getParent()) {
+ } else if (Scope *ParentScope = CurScope->getParent()) {
// Given a lambda defined inside a requires expression,
//
// struct S {
@@ -1113,10 +1113,14 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
// The parameter var is not injected into the function Decl at the point of
// parsing lambda. In such scenarios, perceiving it as dependent could
// result in the constraint being evaluated, which matches what GCC does.
- while (P->getEntity() && P->getEntity()->isRequiresExprBody())
- P = P->getParent();
- if (P->isFunctionDeclarationScope() &&
- llvm::any_of(P->decls(), [](Decl *D) {
+ Scope *LookupScope = ParentScope;
+ while (LookupScope->getEntity() &&
+ LookupScope->getEntity()->isRequiresExprBody())
+ LookupScope = LookupScope->getParent();
+
+ if (LookupScope != ParentScope &&
+ LookupScope->isFunctionDeclarationScope() &&
+ llvm::any_of(LookupScope->decls(), [](Decl *D) {
return isa<ParmVarDecl>(D) &&
cast<ParmVarDecl>(D)->getType()->isTemplateTypeParmType();
}))
diff --git a/clang/test/SemaCXX/lambda-unevaluated.cpp b/clang/test/SemaCXX/lambda-unevaluated.cpp
index 9289fc9ec1243..e3dffa00cd39a 100644
--- a/clang/test/SemaCXX/lambda-unevaluated.cpp
+++ b/clang/test/SemaCXX/lambda-unevaluated.cpp
@@ -1,7 +1,6 @@
// RUN: %clang_cc1 -std=c++20 %s -Wno-c++23-extensions -verify
// RUN: %clang_cc1 -std=c++23 %s -verify
-
template <auto> struct Nothing {};
Nothing<[]() { return 0; }()> nothing;
@@ -282,3 +281,11 @@ static_assert(__is_same_as(int, helper<int>));
} // namespace GH138018
+
+namespace GH172814 {
+auto t() {
+ int x = 0;
+ return [](auto w=[&] { x += w(); }); // expected-error {{lambda expression in default argument cannot capture any entity}} \
+ // expected-error {{expected body of lambda expression}}
+};
+}
>From d50bbaa5f931829961d6b5c24c4bfbe22645b541 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <oleksandr.tarasiuk at outlook.com>
Date: Mon, 29 Dec 2025 11:23:51 +0200
Subject: [PATCH 2/3] add additional test case
---
clang/test/SemaCXX/lambda-unevaluated.cpp | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/clang/test/SemaCXX/lambda-unevaluated.cpp b/clang/test/SemaCXX/lambda-unevaluated.cpp
index e3dffa00cd39a..d02eabf11edb4 100644
--- a/clang/test/SemaCXX/lambda-unevaluated.cpp
+++ b/clang/test/SemaCXX/lambda-unevaluated.cpp
@@ -283,9 +283,13 @@ static_assert(__is_same_as(int, helper<int>));
} // namespace GH138018
namespace GH172814 {
-auto t() {
+auto t1() {
int x = 0;
return [](auto w=[&] { x += w(); }); // expected-error {{lambda expression in default argument cannot capture any entity}} \
// expected-error {{expected body of lambda expression}}
};
+auto t2() {
+ int x = 0;
+ return [](auto w = [&] { return x; }) { }; // expected-error {{lambda expression in default argument cannot capture any entity}}
+};
}
>From 1121701a83adfe40f9958bbd13e840b5e537fe82 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <oleksandr.tarasiuk at outlook.com>
Date: Mon, 29 Dec 2025 14:03:46 +0200
Subject: [PATCH 3/3] remove unnecessary test
---
clang/test/SemaCXX/lambda-unevaluated.cpp | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)
diff --git a/clang/test/SemaCXX/lambda-unevaluated.cpp b/clang/test/SemaCXX/lambda-unevaluated.cpp
index d02eabf11edb4..55a4bc7aff1b5 100644
--- a/clang/test/SemaCXX/lambda-unevaluated.cpp
+++ b/clang/test/SemaCXX/lambda-unevaluated.cpp
@@ -283,12 +283,7 @@ static_assert(__is_same_as(int, helper<int>));
} // namespace GH138018
namespace GH172814 {
-auto t1() {
- int x = 0;
- return [](auto w=[&] { x += w(); }); // expected-error {{lambda expression in default argument cannot capture any entity}} \
- // expected-error {{expected body of lambda expression}}
-};
-auto t2() {
+auto t() {
int x = 0;
return [](auto w = [&] { return x; }) { }; // expected-error {{lambda expression in default argument cannot capture any entity}}
};
More information about the cfe-commits
mailing list