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

Congcong Cai via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 27 08:01:27 PDT 2023


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

>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 1/2] 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'}}
+};

>From 461790881cc95675a88812b2c2d6c691c4ff4aa1 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Fri, 27 Oct 2023 23:01:01 +0800
Subject: [PATCH 2/2] move test

---
 clang/test/SemaCXX/gh67687.cpp            | 7 -------
 clang/test/SemaCXX/lambda-expressions.cpp | 8 ++++++++
 2 files changed, 8 insertions(+), 7 deletions(-)
 delete mode 100644 clang/test/SemaCXX/gh67687.cpp

diff --git a/clang/test/SemaCXX/gh67687.cpp b/clang/test/SemaCXX/gh67687.cpp
deleted file mode 100644
index 36b9ac67ca14e13..000000000000000
--- a/clang/test/SemaCXX/gh67687.cpp
+++ /dev/null
@@ -1,7 +0,0 @@
-// 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'}}
-};
diff --git a/clang/test/SemaCXX/lambda-expressions.cpp b/clang/test/SemaCXX/lambda-expressions.cpp
index b4209d50ad03d71..1797eef320b8659 100644
--- a/clang/test/SemaCXX/lambda-expressions.cpp
+++ b/clang/test/SemaCXX/lambda-expressions.cpp
@@ -382,6 +382,14 @@ namespace PR18128 {
   };
 }
 
+namespace gh67687 {
+struct S {
+  int n;
+  int a = (4, []() { return n; }());  // expected-error {{'this' cannot be implicitly captured in this context}} \
+                                      // expected-note {{explicitly capture 'this'}}
+};
+}
+
 namespace PR18473 {
   template<typename T> void f() {
     T t(0);



More information about the llvm-commits mailing list