Skip to content

Commit 33db497

Browse files
committed
[clang] reject to capture variable in RequiresExprBodyDecl
Expression in `RequiresExprBodyDecl` is resolved as constants and should not be captured. Fixes: #69307, #76593.
1 parent bd2430b commit 33db497

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19719,7 +19719,9 @@ bool Sema::tryCaptureVariable(
1971919719
// we can bailout early.
1972019720
if (CapturingFunctionScopes == 0 && (!BuildAndDiagnose || VarDC == DC))
1972119721
return true;
19722-
19722+
// Expression in `RequiresExprBodyDecl` should not be captured.
19723+
if (isa<RequiresExprBodyDecl>(CurContext))
19724+
return true;
1972319725
const auto *VD = dyn_cast<VarDecl>(Var);
1972419726
if (VD) {
1972519727
if (VD->isInitCapture())
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang -fsyntax-only -std=c++20 -Xclang -verify %s
2+
3+
// expected-no-diagnostics
4+
5+
auto GH69307_Func_1() {
6+
constexpr auto b = 1;
7+
return [&](auto c) -> int
8+
requires requires { b + c; }
9+
{ return 1; };
10+
};
11+
auto GH69307_Func_Ret = GH69307_Func_1()(1);
12+
13+
auto GH69307_Lambda_1 = []() {
14+
return [&](auto c) -> int
15+
requires requires { c; }
16+
{ return 1; };
17+
};
18+
auto GH69307_Lambda_1_Ret = GH69307_Lambda_1()(1);
19+
20+
auto GH69307_Lambda_2 = [](auto c) {
21+
return [&]() -> int
22+
requires requires { c; }
23+
{ return 1; };
24+
};
25+
auto GH69307_Lambda_2_Ret = GH69307_Lambda_2(1)();

0 commit comments

Comments
 (0)