[clang] [Clang] Fix the instantiation of return type requirements in lambda bodies (PR #76967)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 4 07:40:28 PST 2024


https://github.com/LYP951018 updated https://github.com/llvm/llvm-project/pull/76967

>From 00b1b696e3c26f43db2b3b3b2e4e0224d8c61615 Mon Sep 17 00:00:00 2001
From: letrec <liuyupei951018 at hotmail.com>
Date: Thu, 4 Jan 2024 23:10:35 +0800
Subject: [PATCH 1/4] add support for return type requirement in lambdas

---
 clang/docs/ReleaseNotes.rst                 |  3 ++
 clang/lib/Sema/SemaTemplateInstantiate.cpp  |  8 +++++
 clang/test/SemaTemplate/concepts-lambda.cpp | 33 +++++++++++++++++++++
 3 files changed, 44 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 30cfe66703f5a9..1815e8d69332e2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -843,6 +843,9 @@ Bug Fixes to C++ Support
 - Fix crash when parsing nested requirement. Fixes:
   (`#73112 <https://github.com/llvm/llvm-project/issues/73112>`_)
 
+- Fixed a crash caused by using return type requirement in a lambda. Fixes:
+  (`#64607 <https://github.com/llvm/llvm-project/issues/64607>`_)
+
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
 - Fixed an import failure of recursive friend class template.
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 37e5b9cad08bc9..bbaead33a6efe1 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1479,6 +1479,14 @@ namespace {
       return Result;
     }
 
+    StmtResult TransformLambdaBody(LambdaExpr *E, Stmt *Body) {
+      bool Prev = EvaluateConstraints;
+      EvaluateConstraints = true;
+      StmtResult Stmt = inherited::TransformLambdaBody(E, Body);
+      EvaluateConstraints = Prev;
+      return Stmt;
+    }
+
     ExprResult TransformRequiresExpr(RequiresExpr *E) {
       LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
       ExprResult TransReq = inherited::TransformRequiresExpr(E);
diff --git a/clang/test/SemaTemplate/concepts-lambda.cpp b/clang/test/SemaTemplate/concepts-lambda.cpp
index 8a184cbf4e9bc2..0a58137055a381 100644
--- a/clang/test/SemaTemplate/concepts-lambda.cpp
+++ b/clang/test/SemaTemplate/concepts-lambda.cpp
@@ -116,3 +116,36 @@ static_assert(E<int>);
 // expected-note at -11{{because 'Q.template operator()<float>()' would be invalid: no matching member function for call to 'operator()'}}
 }
 }
+
+namespace ReturnTypeConstraintInLambda {
+template <typename T>
+concept C1 = true;
+
+template <class T>
+concept test = [] {
+  return requires(T t) {
+    { t } -> C1;
+  };
+}();
+
+static_assert(test<int>);
+
+template <typename T>
+concept C2 = true;
+struct S1 {
+  int f1() { return 1; }
+};
+
+void foo() {
+  auto make_caller = []<auto member> {
+    return [](S1 *ps) {
+      if constexpr (requires {
+                      { (ps->*member)() } -> C2;
+                    })
+        ;
+    };
+  };
+
+  auto caller = make_caller.operator()<&S1::f1>();
+}
+} // namespace ReturnTypeConstraintInLambda

>From 6412398aae3c610abecabea7dfe127670f320478 Mon Sep 17 00:00:00 2001
From: letrec <liuyupei951018 at hotmail.com>
Date: Thu, 4 Jan 2024 23:31:44 +0800
Subject: [PATCH 2/4] Rename namespace to ReturnTypeRequirementInLambda

---
 clang/test/SemaTemplate/concepts-lambda.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/SemaTemplate/concepts-lambda.cpp b/clang/test/SemaTemplate/concepts-lambda.cpp
index 0a58137055a381..7e431529427dff 100644
--- a/clang/test/SemaTemplate/concepts-lambda.cpp
+++ b/clang/test/SemaTemplate/concepts-lambda.cpp
@@ -117,7 +117,7 @@ static_assert(E<int>);
 }
 }
 
-namespace ReturnTypeConstraintInLambda {
+namespace ReturnTypeRequirementInLambda {
 template <typename T>
 concept C1 = true;
 
@@ -148,4 +148,4 @@ void foo() {
 
   auto caller = make_caller.operator()<&S1::f1>();
 }
-} // namespace ReturnTypeConstraintInLambda
+} // namespace ReturnTypeRequirementInLambda

>From b9a25c7cb9ea38094dfbbbe2e1231aae254347f4 Mon Sep 17 00:00:00 2001
From: letrec <liuyupei951018 at hotmail.com>
Date: Thu, 4 Jan 2024 23:38:14 +0800
Subject: [PATCH 3/4] add comments

---
 clang/lib/Sema/SemaTemplateInstantiate.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index bbaead33a6efe1..e1cbdcd72eac1d 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1480,6 +1480,15 @@ namespace {
     }
 
     StmtResult TransformLambdaBody(LambdaExpr *E, Stmt *Body) {
+      // Currently, we instantiate the body when instantiating the lambda
+      // expression. However, `EvaluateConstraints` is disabled during the
+      // instantiation of the lambda expression, causing the instantiation
+      // failure of the return type requirement in the body. If p0588r1 is fully
+      // implemented, the body will be lazily instantiated, and this problem
+      // will not occur. Here, `EvaluateConstraints` is temporarily set to
+      // `true` to temporarily fix this issue.
+      // FIXME: This temporary fix can be removed after fully implementing
+      // p0588r1.
       bool Prev = EvaluateConstraints;
       EvaluateConstraints = true;
       StmtResult Stmt = inherited::TransformLambdaBody(E, Body);

>From 144d8832bc159dc0c7d804ab788a04fc8b5cc0b2 Mon Sep 17 00:00:00 2001
From: letrec <liuyupei951018 at hotmail.com>
Date: Thu, 4 Jan 2024 23:40:12 +0800
Subject: [PATCH 4/4] add all issues to list

---
 clang/docs/ReleaseNotes.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1815e8d69332e2..e0e32ee1e80e1e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -844,7 +844,9 @@ Bug Fixes to C++ Support
   (`#73112 <https://github.com/llvm/llvm-project/issues/73112>`_)
 
 - Fixed a crash caused by using return type requirement in a lambda. Fixes:
+  (`#63808 <https://github.com/llvm/llvm-project/issues/63808>`_)
   (`#64607 <https://github.com/llvm/llvm-project/issues/64607>`_)
+  (`#64086 <https://github.com/llvm/llvm-project/issues/64086>`_)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^



More information about the cfe-commits mailing list