[clang] [Clang][Sema] Fix a crash in lambda instantiation (PR #85565)

Qizhi Hu via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 17 18:35:46 PDT 2024


https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/85565

>From 535617d786799c7657155e6e2cfa34fd3070f840 Mon Sep 17 00:00:00 2001
From: huqizhi <huqizhi at feysh.com>
Date: Sun, 17 Mar 2024 17:48:05 +0800
Subject: [PATCH] [Clang][Sema] Fix a crash in lambda instantiation

---
 clang/docs/ReleaseNotes.rst    |  1 +
 clang/lib/Sema/TreeTransform.h |  6 ++++++
 clang/test/Sema/PR85343.cpp    | 22 ++++++++++++++++++++++
 3 files changed, 29 insertions(+)
 create mode 100644 clang/test/Sema/PR85343.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 125d51c42d507f..2587c9d002c2c9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -394,6 +394,7 @@ Bug Fixes to C++ Support
   expression references to an entity declared outside of the lambda. (#GH64808)
 - Clang's __builtin_bit_cast will now produce a constant value for records with empty bases. See:
   (#GH82383)
+- Fix a crash in lambda instantiation that missing set ``ThisType`` when checking capture. Fixes (#GH85343).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 2d22692f3ab750..9d638d27d5f51c 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -13714,6 +13714,12 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
 
     // Capturing 'this' is trivial.
     if (C->capturesThis()) {
+      // We need ThisType here.
+      Sema::CXXThisScopeRAII ThisScope(
+          getSema(),
+          dyn_cast_or_null<CXXRecordDecl>(
+              getSema().getFunctionLevelDeclContext()),
+          Qualifiers());
       getSema().CheckCXXThisCapture(C->getLocation(), C->isExplicit(),
                                     /*BuildAndDiagnose*/ true, nullptr,
                                     C->getCaptureKind() == LCK_StarThis);
diff --git a/clang/test/Sema/PR85343.cpp b/clang/test/Sema/PR85343.cpp
new file mode 100644
index 00000000000000..d90ef19d423455
--- /dev/null
+++ b/clang/test/Sema/PR85343.cpp
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -std=c++14 -verify %s
+// expected-no-diagnostics
+
+template <typename c> auto ab() -> c ;
+
+template <typename> struct e {};
+
+template <typename f> struct ac {
+  template <typename h> static e<decltype(ab<h>()(ab<int>))> i;
+  decltype(i<f>) j;
+};
+
+struct d {
+  template <typename f>
+  d(f) { 
+    ac<f> a;
+  }
+};
+struct a {
+  d b = [=](auto) { (void)[this] {}; };
+};
+void b() { new a; }



More information about the cfe-commits mailing list