[clang] [Concepts] Add Decls from the outer scope of the current lambda for conversion function constraints (PR #83420)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 15 07:46:15 PDT 2024


https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/83420

>From 19d97fcf8867554f06973ea6ccb36b47d286ec30 Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Thu, 29 Feb 2024 23:14:46 +0800
Subject: [PATCH 1/2] Limit the scope to lambda conversion function only

---
 clang/docs/ReleaseNotes.rst          |  3 +++
 clang/lib/Sema/SemaConcept.cpp       |  6 +++++-
 clang/test/SemaTemplate/concepts.cpp | 29 ++++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index bac166e6c35627..53f8395b6de69d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -275,6 +275,9 @@ Bug Fixes to C++ Support
   (`#82258 <https://github.com/llvm/llvm-project/issues/82258>`_)
 - Correctly immediate-escalate lambda conversion functions.
   (`#82258 <https://github.com/llvm/llvm-project/issues/82258>`_)
+- Fixed a crash while checking constraints of a trailing requires-expression of a lambda, that the
+  expression references to an entity declared outside of the lambda. This is a reduction from
+  (`#64808 <https://github.com/llvm/llvm-project/issues/64808>`_).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp
index 2878e4d31ee8fe..936ab6b1dc6135 100644
--- a/clang/lib/Sema/SemaConcept.cpp
+++ b/clang/lib/Sema/SemaConcept.cpp
@@ -691,11 +691,15 @@ bool Sema::CheckFunctionConstraints(const FunctionDecl *FD,
   // A lambda conversion operator has the same constraints as the call operator
   // and constraints checking relies on whether we are in a lambda call operator
   // (and may refer to its parameters), so check the call operator instead.
+  // Note that the declarations outside of the lambda should also be
+  // incorporated. Turning on the 'ForOverloadResolution' flag results in the
+  // LocalInstantiationScope not looking into its parents, but we can still
+  // access Decls from the parents while building a lambda RAII scope later.
   if (const auto *MD = dyn_cast<CXXConversionDecl>(FD);
       MD && isLambdaConversionOperator(const_cast<CXXConversionDecl *>(MD)))
     return CheckFunctionConstraints(MD->getParent()->getLambdaCallOperator(),
                                     Satisfaction, UsageLoc,
-                                    ForOverloadResolution);
+                                    /*ShouldAddDeclsFromParentScope=*/true);
 
   DeclContext *CtxToSave = const_cast<FunctionDecl *>(FD);
 
diff --git a/clang/test/SemaTemplate/concepts.cpp b/clang/test/SemaTemplate/concepts.cpp
index bac209a28da912..b7ea0d003a52d7 100644
--- a/clang/test/SemaTemplate/concepts.cpp
+++ b/clang/test/SemaTemplate/concepts.cpp
@@ -1085,3 +1085,32 @@ template void Struct<void>::bar<>();
 template int Struct<void>::field<1, 2>;
 
 }
+
+namespace GH64808 {
+
+template <class T> struct basic_sender {
+  T func;
+  basic_sender(T) : func(T()) {}
+};
+
+auto a = basic_sender{[](auto... __captures) {
+  return []() // #note-a-1
+    requires((__captures, ...), false) // #note-a-2
+  {};
+}()};
+
+auto b = basic_sender{[](auto... __captures) {
+  return []()
+    requires([](int, double) { return true; }(decltype(__captures)()...))
+  {};
+}(1, 2.33)};
+
+void foo() {
+  a.func();
+  // expected-error at -1{{no matching function for call}}
+  // expected-note@#note-a-1{{constraints not satisfied}}
+  // expected-note@#note-a-2{{evaluated to false}}
+  b.func();
+}
+
+} // namespace GH64808

>From 47f6ce907f6d54a26e02ccc062bb5459029501d2 Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Fri, 15 Mar 2024 22:46:09 +0800
Subject: [PATCH 2/2] Address comments

Co-authored-by: cor3ntin <corentinjabot at gmail.com>
---
 clang/docs/ReleaseNotes.rst    | 4 ++--
 clang/lib/Sema/SemaConcept.cpp | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 53f8395b6de69d..a3d0b7f89cc88b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -276,8 +276,8 @@ Bug Fixes to C++ Support
 - Correctly immediate-escalate lambda conversion functions.
   (`#82258 <https://github.com/llvm/llvm-project/issues/82258>`_)
 - Fixed a crash while checking constraints of a trailing requires-expression of a lambda, that the
-  expression references to an entity declared outside of the lambda. This is a reduction from
-  (`#64808 <https://github.com/llvm/llvm-project/issues/64808>`_).
+  expression references to an entity declared outside of the lambda. Fixes
+  (#GH64808)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp
index 936ab6b1dc6135..649ba4e83f7723 100644
--- a/clang/lib/Sema/SemaConcept.cpp
+++ b/clang/lib/Sema/SemaConcept.cpp
@@ -692,7 +692,7 @@ bool Sema::CheckFunctionConstraints(const FunctionDecl *FD,
   // and constraints checking relies on whether we are in a lambda call operator
   // (and may refer to its parameters), so check the call operator instead.
   // Note that the declarations outside of the lambda should also be
-  // incorporated. Turning on the 'ForOverloadResolution' flag results in the
+  // considered. Turning on the 'ForOverloadResolution' flag results in the
   // LocalInstantiationScope not looking into its parents, but we can still
   // access Decls from the parents while building a lambda RAII scope later.
   if (const auto *MD = dyn_cast<CXXConversionDecl>(FD);



More information about the cfe-commits mailing list