https://github.com/StefanPaulet created 
https://github.com/llvm/llvm-project/pull/246670

Resolves #221564

The problem was introduced in #211811. It mistakenly assumes that when the 
`DeclContext` of a lambda call operator is encountered, a corresponding 
`LambdaScopeInfo` is present on the `Sema.FunctionScopes` stack.
This assumption holds only during the definition of a lambda, and not if the 
call operator is explicitly referenced after the lambda is defined:
```cpp
auto l = []() { return __func__; }; //this is fine
using L = decltype(l);
constexpr auto L::operator()() const { return __func__; } //no LambdaScopeInfo 
here
```
To fix this, the `DeclContext` in `getPredefinedExprDecl` is adjusted only if 
it matches with the call operator of a `LambdaScopeInfo` currently on the stack.


>From c59a30c2ec9c9c9c27ffd14bcb5019c75dfcf3f1 Mon Sep 17 00:00:00 2001
From: StefanPaulet <[email protected]>
Date: Thu, 26 Nov 2026 11:22:34 +0000
Subject: [PATCH] Properly adjust lambda decl context in predefined expressions

---
 clang/lib/Sema/SemaExpr.cpp            | 21 +++++++++++----------
 clang/test/Sema/ms_predefined_expr.cpp |  7 +++++++
 2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index d3deecb3ee74c6..baa4fb7f1e2357 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2125,17 +2125,18 @@ static PredefinedIdentKind 
getPredefinedExprKind(tok::TokenKind Kind) {
 /// to determine the value of a PredefinedExpr. This can be either a
 /// block, lambda, captured statement, function, otherwise a nullptr.
 static Decl *getPredefinedExprDecl(Sema &S, DeclContext *DC) {
-  auto LSI = S.FunctionScopes.rbegin();
-
-  auto tryAdjustLambdaContext = [&S, &LSI](DeclContext *&DC) {
+  auto tryAdjustLambdaContext = [&S](DeclContext *&DC) {
     if (isLambdaCallOperator(DC)) {
-      auto E = S.FunctionScopes.rend();
-      while (LSI != E && !isa<LambdaScopeInfo>(*LSI))
-        ++LSI;
-      assert(LSI != E && "Should be in a lambda scope info");
-      if (dyn_cast<LambdaScopeInfo>(*LSI)->BeforeCompoundStatement)
-        DC = DC->getParent();
-      ++LSI;
+      for (auto FS : llvm::reverse(S.FunctionScopes)) {
+        if (auto *LSI = dyn_cast<LambdaScopeInfo>(FS); LSI != nullptr) {
+          assert(LSI->CallOperator != nullptr &&
+                 "A LambdaScopeInfo should have the call operator");
+          if (LSI->CallOperator == DC && LSI->BeforeCompoundStatement) {
+            DC = DC->getParent();
+            return;
+          }
+        }
+      }
     }
   };
 
diff --git a/clang/test/Sema/ms_predefined_expr.cpp 
b/clang/test/Sema/ms_predefined_expr.cpp
index b42a494beef98f..5b4bded659bd77 100644
--- a/clang/test/Sema/ms_predefined_expr.cpp
+++ b/clang/test/Sema/ms_predefined_expr.cpp
@@ -206,3 +206,10 @@ void test_in_constexpr_struct_init() {
   } constexpr c1 = { { "F:" __FUNCTION__ } }; // expected-warning{{expansion 
of predefined identifier '__FUNCTION__' to a string literal is a Microsoft 
extension}}
   ASSERT_EQ("F:" __FUNCTION__, c1.s.F); // expected-warning{{expansion of 
predefined identifier '__FUNCTION__' to a string literal is a Microsoft 
extension}}
 }
+
+namespace GH221564 {
+  auto l = [](auto a) { return 42; }; // expected-note {{defined here}}
+  using L = decltype(l);
+
+  auto L::operator()() const { return {"<="}; } // expected-error-re 
{{out-of-line definition of 'operator()' does not match any declaration in 
'GH221564::(lambda at {{.*}})'}}
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to