[clang] [clang] reject to capture variable in `RequiresExprBodyDecl` (PR #78598)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 18 07:16:47 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Congcong Cai (HerrCai0907)
<details>
<summary>Changes</summary>
Expression in `RequiresExprBodyDecl` is resolved as constants and should not be captured.
Fixes: #<!-- -->69307, #<!-- -->76593.
---
Full diff: https://github.com/llvm/llvm-project/pull/78598.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaExpr.cpp (+3-1)
- (added) clang/test/SemaCXX/requires-expression-with-capture-variable.cpp (+25)
``````````diff
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)();
``````````
</details>
https://github.com/llvm/llvm-project/pull/78598
More information about the cfe-commits
mailing list