Skip to content

[clang] Crash when referencing capture in static lambda #74661

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

Merged
Merged
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
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,10 @@ Miscellaneous Clang Crashes Fixed
`Issue 41302 <https://github.com/llvm/llvm-project/issues/41302>`_
- Fixed a crash when ``-ast-dump=json`` was used for code using class
template deduction guides.
- Fixed a crash when a lambda marked as ``static`` referenced a captured
variable in an expression.
`Issue 74608 <https://github.com/llvm/llvm-project/issues/74608>`_


OpenACC Specific Changes
------------------------
Expand Down
14 changes: 12 additions & 2 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8492,14 +8492,24 @@ bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
return false;

if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) {
const auto *MD = cast<CXXMethodDecl>(Info.CurrentCall->Callee);

// Static lambda function call operators can't have captures. We already
// diagnosed this, so bail out here.
if (MD->isStatic()) {
assert(Info.CurrentCall->This == nullptr &&
"This should not be set for a static call operator");
return false;
}

// Start with 'Result' referring to the complete closure object...
if (auto *MD = cast<CXXMethodDecl>(Info.CurrentCall->Callee);
MD->isExplicitObjectMemberFunction()) {
if (MD->isExplicitObjectMemberFunction()) {
APValue *RefValue =
Info.getParamSlot(Info.CurrentCall->Arguments, MD->getParamDecl(0));
Result.setFrom(Info.Ctx, *RefValue);
} else
Result = *Info.CurrentCall->This;

// ... then update it to refer to the field of the closure object
// that represents the capture.
if (!HandleLValueMember(Info, E, Result, FD))
Expand Down
12 changes: 12 additions & 0 deletions clang/test/Parser/cxx2b-lambdas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,15 @@ void static_captures() {
}
};
}

constexpr auto static_capture_constexpr() {
char n = 'n';
return [n] static { return n; }(); // expected-error {{a static lambda cannot have any captures}}
}
static_assert(static_capture_constexpr()); // expected-error {{static assertion expression is not an integral constant expression}}

constexpr auto capture_constexpr() {
char n = 'n';
return [n] { return n; }();
}
static_assert(capture_constexpr());