[PATCH] D154716: [SemaCXX] Fix bug where unexpanded lambda captures where assumed to be expanded

Mital Ashok via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 22 07:06:00 PDT 2023


MitalAshok updated this revision to Diff 543183.
MitalAshok edited the summary of this revision.
MitalAshok added a comment.

Added entry to changelog and the test from the bug report


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154716/new/

https://reviews.llvm.org/D154716

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaTemplateInstantiate.cpp
  clang/lib/Sema/TreeTransform.h
  clang/test/SemaCXX/lambda-pack-expansion.cpp


Index: clang/test/SemaCXX/lambda-pack-expansion.cpp
===================================================================
--- clang/test/SemaCXX/lambda-pack-expansion.cpp
+++ clang/test/SemaCXX/lambda-pack-expansion.cpp
@@ -21,3 +21,30 @@
   take_by_ref(x);
 }
 }
+
+namespace GH63677 {
+
+template<typename>
+void f() {
+  []<typename... Ts>() -> void {
+    [...us = Ts{}]{
+      (Ts(us), ...);
+    };
+  }.template operator()<int, int>();
+}
+
+template void f<int>();
+
+template <class>
+inline constexpr auto fun =
+  []<class... Ts>(Ts... ts) {
+    return [... us = (Ts&&) ts]<class Fun>(Fun&& fn) mutable {
+      return static_cast<Fun&&>(fn)(static_cast<Ts&&>(us)...);
+    };
+  };
+
+void f() {
+  [[maybe_unused]] auto s = fun<int>(1, 2, 3, 4);
+}
+
+}
Index: clang/lib/Sema/TreeTransform.h
===================================================================
--- clang/lib/Sema/TreeTransform.h
+++ clang/lib/Sema/TreeTransform.h
@@ -13340,14 +13340,14 @@
               OldVD->getInit()->getSourceRange(), Unexpanded, Expand,
               RetainExpansion, NumExpansions))
         return ExprError();
+      assert(!RetainExpansion && "Should not need to retain expansion after a "
+                                 "capture since it cannot be extended");
       if (Expand) {
         for (unsigned I = 0; I != *NumExpansions; ++I) {
           Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
           SubstInitCapture(SourceLocation(), std::nullopt);
         }
-      }
-      if (!Expand || RetainExpansion) {
-        ForgetPartiallySubstitutedPackRAII Forget(getDerived());
+      } else {
         SubstInitCapture(ExpansionTL.getEllipsisLoc(), NumExpansions);
         Result.EllipsisLoc = ExpansionTL.getEllipsisLoc();
       }
Index: clang/lib/Sema/SemaTemplateInstantiate.cpp
===================================================================
--- clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1270,7 +1270,8 @@
     }
 
     void transformedLocalDecl(Decl *Old, ArrayRef<Decl *> NewDecls) {
-      if (Old->isParameterPack()) {
+      if (Old->isParameterPack() &&
+          (NewDecls.size() != 1 || !NewDecls.front()->isParameterPack())) {
         SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Old);
         for (auto *New : NewDecls)
           SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -782,6 +782,8 @@
   (`#63903 <https://github.com/llvm/llvm-project/issues/63903>`_)
 - Fix constraint checking of non-generic lambdas.
   (`#63181 <https://github.com/llvm/llvm-project/issues/63181>`_)
+- Fix init-capture packs having a size of one before being instantiated
+  (`#63677 <https://github.com/llvm/llvm-project/issues/63677>`_)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154716.543183.patch
Type: text/x-patch
Size: 2993 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230722/1287ec71/attachment.bin>


More information about the cfe-commits mailing list