[clang] [clang]set invalid for lambda which missing capture `this` (PR #70432)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 27 02:11:56 PDT 2023


https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/70432

It can suppression further crash.
Fixes: #67687

>From c12aeb6f2a83b9cb3c3815e72d57638cf7de43a0 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Fri, 27 Oct 2023 16:51:59 +0800
Subject: [PATCH] WIP fix lambda invalid

---
 clang/docs/ReleaseNotes.rst    | 2 ++
 clang/lib/Sema/SemaExprCXX.cpp | 6 ++++--
 clang/test/SemaCXX/gh67687.cpp | 7 +++++++
 3 files changed, 13 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/SemaCXX/gh67687.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 42f20b9a9bb0410..940dc3f22b89dc0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -507,6 +507,8 @@ Bug Fixes in This Version
   ``thread_local`` instead of ``_Thread_local``.
   Fixes (`#70068 <https://github.com/llvm/llvm-project/issues/70068>`_) and
   (`#69167 <https://github.com/llvm/llvm-project/issues/69167>`_)
+- Fix crash in evaluating invalid lambda expression which forget capture this.
+  Fixes (`#67687 <https://github.com/llvm/llvm-project/issues/67687>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 1153049496d129f..6344c9102330a00 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -1333,6 +1333,7 @@ bool Sema::CheckCXXThisCapture(SourceLocation Loc, const bool Explicit,
       if (LSI && isGenericLambdaCallOperatorSpecialization(LSI->CallOperator)) {
         // This context can't implicitly capture 'this'; fail out.
         if (BuildAndDiagnose) {
+          LSI->CallOperator->setInvalidDecl();
           Diag(Loc, diag::err_this_capture)
               << (Explicit && idx == MaxFunctionScopesIndex);
           if (!Explicit)
@@ -1354,10 +1355,11 @@ bool Sema::CheckCXXThisCapture(SourceLocation Loc, const bool Explicit,
         continue;
       }
       // This context can't implicitly capture 'this'; fail out.
-      if (BuildAndDiagnose)
+      if (BuildAndDiagnose) {
+        LSI->CallOperator->setInvalidDecl();
         Diag(Loc, diag::err_this_capture)
             << (Explicit && idx == MaxFunctionScopesIndex);
-
+      }
       if (!Explicit)
         buildLambdaThisCaptureFixit(*this, LSI);
       return true;
diff --git a/clang/test/SemaCXX/gh67687.cpp b/clang/test/SemaCXX/gh67687.cpp
new file mode 100644
index 000000000000000..36b9ac67ca14e13
--- /dev/null
+++ b/clang/test/SemaCXX/gh67687.cpp
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -verify -fsyntax-only -std=c++17 %s
+
+struct S {
+  int n;
+  int d = (4, []() { return n; }());  // expected-error {{'this' cannot be implicitly captured in this context}} \
+                                      // expected-note {{explicitly capture 'this'}}
+};



More information about the cfe-commits mailing list