Skip to content

[clang] reject to capture variable in RequiresExprBodyDecl #78598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19713,15 +19713,18 @@ bool Sema::tryCaptureVariable(
// An init-capture is notionally from the context surrounding its
// declaration, but its parent DC is the lambda class.
DeclContext *VarDC = Var->getDeclContext();
DeclContext *DC = CurContext;

// tryCaptureVariable is called every time a DeclRef is formed,
// it can therefore have non-negigible impact on performances.
// For local variables and when there is no capturing scope,
// we can bailout early.
if (CapturingFunctionScopes == 0 && (!BuildAndDiagnose || VarDC == DC))
if (CapturingFunctionScopes == 0 &&
(!BuildAndDiagnose || VarDC == CurContext))
return true;

DeclContext *DC = isa_and_present<RequiresExprBodyDecl>(CurContext)
? CurContext->getParent()
: CurContext;

const auto *VD = dyn_cast<VarDecl>(Var);
if (VD) {
if (VD->isInitCapture())
Expand Down Expand Up @@ -19791,11 +19794,10 @@ bool Sema::tryCaptureVariable(

// Only block literals, captured statements, and lambda expressions can
// capture; other scopes don't work.
DeclContext *ParentDC =
!IsInScopeDeclarationContext
? DC->getParent()
: getParentOfCapturingContextOrNull(DC, Var, ExprLoc,
BuildAndDiagnose, *this);
DeclContext *ParentDC = IsInScopeDeclarationContext
? getParentOfCapturingContextOrNull(
DC, Var, ExprLoc, BuildAndDiagnose, *this)
: DC->getParent();
// We need to check for the parent *first* because, if we *have*
// private-captured a global variable, we need to recursively capture it in
// intermediate blocks, lambdas, etc.
Expand Down
34 changes: 34 additions & 0 deletions clang/test/SemaCXX/requires-expression-with-capture-variable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %clang -fsyntax-only -std=c++20 -Xclang -verify %s

// expected-no-diagnostics

auto GH69307_correct() {
constexpr auto b = 1;
// no need to capture
return [](auto c) -> int
requires requires { b + c; }
{ return 1; };
};
auto GH69307_correct_ret = GH69307_correct()(1);

auto GH69307_func() {
constexpr auto b = 1;
return [&](auto c) -> int
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do a similar test with NO capture here? To show that they aren't needed.

requires requires { b + c; }
{ return 1; };
};
auto GH69307_func_ret = GH69307_func()(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)();
7 changes: 7 additions & 0 deletions clang/test/SemaCXX/warn-unused-lambda-capture-cxx20.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %clang_cc1 -fsyntax-only -Wunused-lambda-capture -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++20 %s

void test() {
int i;
auto explicit_by_value_unused_requires = [i] (auto) requires requires { i; } {}; // expected-warning{{lambda capture 'i' is not required to be captured for this use}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be PARTICULARLY good if we could 'remember' that it was 'used' in the requires clause so we can add a note that says something like (please consider wording and write better:)), note: use in requires expr doesn't use captures because they are special.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be interesting. I will try to do it later in festival.

explicit_by_value_unused_requires(1);
}
4 changes: 4 additions & 0 deletions clang/test/SemaCXX/warn-unused-lambda-capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ void test() {
auto explicit_by_value_unused_sizeof = [i] { return sizeof(i); }; // expected-warning{{lambda capture 'i' is not required to be captured for this use}}
auto explicit_by_value_unused_decltype = [i] { decltype(i) j = 0; }; // expected-warning{{lambda capture 'i' is not required to be captured for this use}}
auto explicit_by_value_unused_const = [k] { return k + 1; }; // expected-warning{{lambda capture 'k' is not required to be captured for this use}}
#if __cplusplus >= 202002L
auto explicit_by_value_unused_requires = [i] (auto) requires requires { i; } {}; // expected-warning{{lambda capture 'i' is not required to be captured for this use}}
explicit_by_value_unused_requires(1);
#endif

auto explicit_by_reference_used = [&i] { i++; };
auto explicit_by_reference_unused = [&i] {}; // expected-warning{{lambda capture 'i' is not used}}
Expand Down