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;


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;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to