[PATCH] D119063: [SemaCXX] Properly scope ArgumentPackSubstitutionIndex when expanding base types

Michael Colavita via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 5 14:50:36 PST 2022


colavitam created this revision.
colavitam added reviewers: mizvekov, rsmith.
Herald added a subscriber: arphaman.
colavitam requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Resolve the crash in issue #53609. The ArgumentPackSubstitutionIndex
from expanding base types containing parameter packs should not persist
when we check the resulting types. Checking the types may lead to
template substitution in the base type, where the index will no longer
be valid.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119063

Files:
  clang/lib/Sema/SemaTemplateInstantiate.cpp
  clang/test/SemaCXX/template-base-class-pack-expansion.cpp


Index: clang/test/SemaCXX/template-base-class-pack-expansion.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/template-base-class-pack-expansion.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+// Don't crash (#53609).
+
+template <class, int> struct a;
+
+template <class, class...> struct b;
+template <class x, class... y, y... z>
+struct b<x, a<y, z>...> {};
+
+template <class... x> struct c: b<x>...  {};
+
+c<int> d;
Index: clang/lib/Sema/SemaTemplateInstantiate.cpp
===================================================================
--- clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -2530,12 +2530,13 @@
       // If we should expand this pack expansion now, do so.
       if (ShouldExpand) {
         for (unsigned I = 0; I != *NumExpansions; ++I) {
+          {
             Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
 
-          TypeSourceInfo *BaseTypeLoc = SubstType(Base.getTypeSourceInfo(),
-                                                  TemplateArgs,
-                                              Base.getSourceRange().getBegin(),
-                                                  DeclarationName());
+            BaseTypeLoc =
+                SubstType(Base.getTypeSourceInfo(), TemplateArgs,
+                          Base.getSourceRange().getBegin(), DeclarationName());
+          }
           if (!BaseTypeLoc) {
             Invalid = true;
             continue;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119063.406212.patch
Type: text/x-patch
Size: 1551 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220205/3f72ee3a/attachment.bin>


More information about the cfe-commits mailing list