[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 07:35:18 PDT 2024


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

>From edaea6b244e9e35998421e551fb757e6ba099668 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 +
 .../lib/Sema/SemaTemplateInstantiateDecl.cpp  |  4 ++++
 clang/lib/Sema/TreeTransform.h                |  4 ++++
 clang/test/Sema/PR85343.cpp                   | 22 +++++++++++++++++++
 4 files changed, 31 insertions(+)
 create mode 100644 clang/test/Sema/PR85343.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ba9de1ac98de08..26a87e9ab7b066 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -392,6 +392,7 @@ Bug Fixes to C++ Support
   Fixes (#GH84368).
 - Fixed a crash while checking constraints of a trailing requires-expression of a lambda, that the
   expression references to an entity declared outside of the lambda. (#GH64808)
+- 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/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index dc972018e7b281..7786557396cd13 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -5182,6 +5182,10 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
     // Enter the scope of this instantiation. We don't use
     // PushDeclContext because we don't have a scope.
     Sema::ContextRAII savedContext(*this, Function);
+    // We need ThisType in lambda instantiation.
+    Sema::CXXThisScopeRAII ThisScope(
+        *this, dyn_cast<CXXRecordDecl>(Function->getDeclContext()),
+        Qualifiers());
 
     FPFeaturesStateRAII SavedFPFeatures(*this);
     CurFPFeatures = FPOptions(getLangOpts());
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 2d22692f3ab750..69d8e38181897d 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -13698,6 +13698,10 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
                              E->hasExplicitParameters(), E->isMutable());
 
   // Introduce the context of the call operator.
+  // We need ThisType in lambda instantiation.
+  std::optional<Sema::CXXThisScopeRAII> ThisScope;
+  if (auto *RD = dyn_cast<CXXRecordDecl>(SemaRef.getFunctionLevelDeclContext()))
+    ThisScope.emplace(SemaRef, RD, Qualifiers());
   Sema::ContextRAII SavedContext(getSema(), NewCallOperator,
                                  /*NewThisContext*/false);
 
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