Skip to content

Commit b1b9cd4

Browse files
authored
[clang]set invalid for lambda which missing capture this (#70432)
It can suppression further crash. Fixes: #67687
1 parent 58bdd16 commit b1b9cd4

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,8 @@ Bug Fixes in This Version
529529
``thread_local`` instead of ``_Thread_local``.
530530
Fixes (`#70068 <https://github.com/llvm/llvm-project/issues/70068>`_) and
531531
(`#69167 <https://github.com/llvm/llvm-project/issues/69167>`_)
532+
- Fix crash in evaluating invalid lambda expression which forget capture this.
533+
Fixes (`#67687 <https://github.com/llvm/llvm-project/issues/67687>`_)
532534
- Fix crash from constexpr evaluator evaluating uninitialized arrays as rvalue.
533535
Fixes (`#67317 <https://github.com/llvm/llvm-project/issues/67317>`_)
534536

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,7 @@ bool Sema::CheckCXXThisCapture(SourceLocation Loc, const bool Explicit,
13331333
if (LSI && isGenericLambdaCallOperatorSpecialization(LSI->CallOperator)) {
13341334
// This context can't implicitly capture 'this'; fail out.
13351335
if (BuildAndDiagnose) {
1336+
LSI->CallOperator->setInvalidDecl();
13361337
Diag(Loc, diag::err_this_capture)
13371338
<< (Explicit && idx == MaxFunctionScopesIndex);
13381339
if (!Explicit)
@@ -1354,10 +1355,11 @@ bool Sema::CheckCXXThisCapture(SourceLocation Loc, const bool Explicit,
13541355
continue;
13551356
}
13561357
// This context can't implicitly capture 'this'; fail out.
1357-
if (BuildAndDiagnose)
1358+
if (BuildAndDiagnose) {
1359+
LSI->CallOperator->setInvalidDecl();
13581360
Diag(Loc, diag::err_this_capture)
13591361
<< (Explicit && idx == MaxFunctionScopesIndex);
1360-
1362+
}
13611363
if (!Explicit)
13621364
buildLambdaThisCaptureFixit(*this, LSI);
13631365
return true;

clang/test/SemaCXX/lambda-expressions.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,14 @@ namespace PR18128 {
382382
};
383383
}
384384

385+
namespace gh67687 {
386+
struct S {
387+
int n;
388+
int a = (4, []() { return n; }()); // expected-error {{'this' cannot be implicitly captured in this context}} \
389+
// expected-note {{explicitly capture 'this'}}
390+
};
391+
}
392+
385393
namespace PR18473 {
386394
template<typename T> void f() {
387395
T t(0);

0 commit comments

Comments
 (0)