[clang] [Clang] Do not inherit immediate-function context in lambda bodies (PR #214002)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 5 19:55:56 PDT 2026
https://github.com/term-est updated https://github.com/llvm/llvm-project/pull/214002
>From fcba5fc86df76161420027750f6fca5caae34e67 Mon Sep 17 00:00:00 2001
From: term-est <eurasia.sely at gmail.com>
Date: Tue, 4 Aug 2026 19:23:57 +0300
Subject: [PATCH 1/4] [clang] Linker regression with lambda NTTP containing
consteval function call #169139
---
clang/lib/Sema/SemaLambda.cpp | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index bbe93f6ab8a40..91016e3523387 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -1601,6 +1601,9 @@ void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro,
// cleanups from the enclosing full-expression.
PushExpressionEvaluationContextForFunction(
ExpressionEvaluationContext::PotentiallyEvaluated, LSI->CallOperator);
+
+ currentEvaluationContext().InImmediateFunctionContext =
+ LSI->CallOperator->isConsteval();
}
void Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope,
>From acb12ac095020e2be17655cea25c50273087e64c Mon Sep 17 00:00:00 2001
From: term-est <eurasia.sely at gmail.com>
Date: Tue, 4 Aug 2026 21:26:58 +0300
Subject: [PATCH 2/4] Add a regression test.
---
clang/test/CodeGen/consteval-lambda-nttp.cpp | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
create mode 100644 clang/test/CodeGen/consteval-lambda-nttp.cpp
diff --git a/clang/test/CodeGen/consteval-lambda-nttp.cpp b/clang/test/CodeGen/consteval-lambda-nttp.cpp
new file mode 100644
index 0000000000000..71ff0c2d9013d
--- /dev/null
+++ b/clang/test/CodeGen/consteval-lambda-nttp.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu \
+// RUN: -emit-llvm -o - %s | FileCheck %s
+
+consteval int foo() {
+ return 42;
+}
+
+template <auto Fn>
+int bar() {
+ return Fn();
+}
+
+int test() {
+ return bar<[] { return foo(); }>();
+}
+
+// CHECK-NOT: @_Z3foov
+// CHECK: define{{.*}} i32 @_Z4testv
+// CHECK-NOT: @_Z3foov
\ No newline at end of file
>From 54c9b7818eeaa5f35e4ea7b7120da1f45a202094 Mon Sep 17 00:00:00 2001
From: term-est <eurasia.sely at gmail.com>
Date: Tue, 4 Aug 2026 21:42:31 +0300
Subject: [PATCH 3/4] Add comments and fix formatting
---
clang/lib/Sema/SemaLambda.cpp | 2 ++
clang/test/CodeGen/consteval-lambda-nttp.cpp | 2 +-
2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index 91016e3523387..5960f8a26ae06 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -1602,6 +1602,8 @@ void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro,
PushExpressionEvaluationContextForFunction(
ExpressionEvaluationContext::PotentiallyEvaluated, LSI->CallOperator);
+ // A lambda body does not inherit the immediate-function context of the
+ // enclosing lambda-expression.
currentEvaluationContext().InImmediateFunctionContext =
LSI->CallOperator->isConsteval();
}
diff --git a/clang/test/CodeGen/consteval-lambda-nttp.cpp b/clang/test/CodeGen/consteval-lambda-nttp.cpp
index 71ff0c2d9013d..19b0e7f23e3df 100644
--- a/clang/test/CodeGen/consteval-lambda-nttp.cpp
+++ b/clang/test/CodeGen/consteval-lambda-nttp.cpp
@@ -16,4 +16,4 @@ int test() {
// CHECK-NOT: @_Z3foov
// CHECK: define{{.*}} i32 @_Z4testv
-// CHECK-NOT: @_Z3foov
\ No newline at end of file
+// CHECK-NOT: @_Z3foov
>From 62df2318459e473c9ad715f13099565af147a3e6 Mon Sep 17 00:00:00 2001
From: term-est <eurasia.sely at gmail.com>
Date: Thu, 6 Aug 2026 05:55:05 +0300
Subject: [PATCH 4/4] Fix the issue in
PushExpressionEvaluationContextForFunction instead
---
clang/lib/Sema/SemaExpr.cpp | 18 +++++++++++-------
clang/lib/Sema/SemaLambda.cpp | 5 -----
2 files changed, 11 insertions(+), 12 deletions(-)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 59b8c9b60663c..fb17d029ea64a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -18273,13 +18273,17 @@ void Sema::PushExpressionEvaluationContextForFunction(
Current.InImmediateEscalatingFunctionContext =
getLangOpts().CPlusPlus20 && FD->isImmediateEscalating();
- if (isLambdaMethod(FD))
- Current.InImmediateFunctionContext =
- FD->isConsteval() ||
- (isLambdaMethod(FD) && (Parent.isConstantEvaluated() ||
- Parent.isImmediateFunctionContext()));
- else
- Current.InImmediateFunctionContext = FD->isConsteval();
+ const bool InheritParentImmediateContext =
+ isLambdaMethod(FD) && !isLambdaCallOperator(FD);
+
+ // A lambda call operator body is not a subexpression of the enclosing
+ // lambda-expression. Other lambda methods may be synthesized while
+ // processing the lambda and need to inherit the enclosing evaluation
+ // context.
+ Current.InImmediateFunctionContext =
+ FD->isConsteval() ||
+ (InheritParentImmediateContext &&
+ (Parent.isConstantEvaluated() || Parent.isImmediateFunctionContext()));
}
}
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index 5960f8a26ae06..bbe93f6ab8a40 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -1601,11 +1601,6 @@ void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro,
// cleanups from the enclosing full-expression.
PushExpressionEvaluationContextForFunction(
ExpressionEvaluationContext::PotentiallyEvaluated, LSI->CallOperator);
-
- // A lambda body does not inherit the immediate-function context of the
- // enclosing lambda-expression.
- currentEvaluationContext().InImmediateFunctionContext =
- LSI->CallOperator->isConsteval();
}
void Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope,
More information about the cfe-commits
mailing list