https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/78598

Expression in `RequiresExprBodyDecl` is resolved as constants and should not be 
captured.
Fixes: #69307, #76593.

>From 33db497c31fd9da3a3acfc0d419dcdc396752863 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0...@163.com>
Date: Thu, 18 Jan 2024 23:12:23 +0800
Subject: [PATCH] [clang] reject to capture variable in `RequiresExprBodyDecl`

Expression in `RequiresExprBodyDecl` is resolved as constants and should not be 
captured.
Fixes: #69307, #76593.
---
 clang/lib/Sema/SemaExpr.cpp                   |  4 ++-
 ...uires-expression-with-capture-variable.cpp | 25 +++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 
clang/test/SemaCXX/requires-expression-with-capture-variable.cpp

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 6413a48f809ac9..767fc3787cb22f 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -19719,7 +19719,9 @@ bool Sema::tryCaptureVariable(
   // we can bailout early.
   if (CapturingFunctionScopes == 0 && (!BuildAndDiagnose || VarDC == DC))
     return true;
-
+  // Expression in `RequiresExprBodyDecl` should not be captured.
+  if (isa<RequiresExprBodyDecl>(CurContext))
+    return true;
   const auto *VD = dyn_cast<VarDecl>(Var);
   if (VD) {
     if (VD->isInitCapture())
diff --git a/clang/test/SemaCXX/requires-expression-with-capture-variable.cpp 
b/clang/test/SemaCXX/requires-expression-with-capture-variable.cpp
new file mode 100644
index 00000000000000..d01a54133f6c39
--- /dev/null
+++ b/clang/test/SemaCXX/requires-expression-with-capture-variable.cpp
@@ -0,0 +1,25 @@
+// RUN: %clang -fsyntax-only -std=c++20 -Xclang -verify %s
+
+// expected-no-diagnostics
+
+auto GH69307_Func_1() {
+  constexpr auto b = 1;
+  return [&](auto c) -> int
+           requires requires { b + c; }
+  { return 1; };
+};
+auto GH69307_Func_Ret = GH69307_Func_1()(1);
+
+auto GH69307_Lambda_1 = []() {
+  return [&](auto c) -> int
+           requires requires { c; }
+  { return 1; };
+};
+auto GH69307_Lambda_1_Ret = GH69307_Lambda_1()(1);
+
+auto GH69307_Lambda_2 = [](auto c) {
+  return [&]() -> int
+           requires requires { c; }
+  { return 1; };
+};
+auto GH69307_Lambda_2_Ret = GH69307_Lambda_2(1)();

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to