[clang] [Clang][Sema] Fix lambdas mistakenly inheriting InImmediateFunctionContext (PR #232458)
Ferdinand Bachmann via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 15 11:56:55 PDT 2026
https://github.com/Ferdi265 created https://github.com/llvm/llvm-project/pull/232458
This PR fixes #183415.
As per [expr.const]/p14.1, only the innermost enclosing function parameter scope matters for determining whether a function or lambda is in an immediate function context. It does not matter whether the parent is an immediate function or not.
This fixes assertions on valid programs and miscompilations related to immediate functions being callable from non-immediate contexts due to missed escalation of lambdas to immediate functions.
With this fix all examples from the linked issue produce identical behaviour as GCC. I am currently rebuilding Clang/LLVM to then run the full clang test suite to verify I did not break anything.
This is my first LLVM contribution, so please tell me if I have missed anything.
>From 4e240a7dcace90651924a73ca194234a7bd45015 Mon Sep 17 00:00:00 2001
From: Ferdinand Bachmann <ferdinand.bachmann at yrlf.at>
Date: Thu, 15 Oct 2026 20:39:08 +0200
Subject: [PATCH] [Clang][Sema] Fix lambdas mistakenly inheriting
InImmediateFunctionContext
As per [expr.const]/p14.1, only the innermost enclosing function
parameter scope matters for determining whether a function or lambda is
in an immediate function context. It does not matter whether the parent
is an immediate function or not.
This fixes assertions on valid programs and miscompilations related to
immediate functions being callable from non-immediate contexts due to
missed escalation of lambdas to immediate functions.
Fixes #183415.
Signed-off-by: Ferdinand Bachmann <ferdinand.bachmann at yrlf.at>
---
clang/lib/Sema/SemaExpr.cpp | 10 +---------
1 file changed, 1 insertion(+), 9 deletions(-)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index e29caaab8e3f3..ba12685b96f0f 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -18527,8 +18527,6 @@ void Sema::PushExpressionEvaluationContextForFunction(
FD && FD->isConsteval()
? ExpressionEvaluationContext::ImmediateFunctionContext
: NewContext);
- const Sema::ExpressionEvaluationContextRecord &Parent =
- parentEvaluationContext();
Sema::ExpressionEvaluationContextRecord &Current = currentEvaluationContext();
Current.InDiscardedStatement = false;
@@ -18546,13 +18544,7 @@ 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();
+ Current.InImmediateFunctionContext = FD->isConsteval();
}
}
More information about the cfe-commits
mailing list