r348313 - Fix crash if an in-class explicit function specialization has explicit

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 4 14:26:33 PST 2018


Author: rsmith
Date: Tue Dec  4 14:26:32 2018
New Revision: 348313

URL: http://llvm.org/viewvc/llvm-project?rev=348313&view=rev
Log:
Fix crash if an in-class explicit function specialization has explicit
template arguments referring to template paramaeters.

Added:
    cfe/trunk/test/SemaTemplate/member-specialization.cpp
Modified:
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp

Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=348313&r1=348312&r2=348313&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Tue Dec  4 14:26:32 2018
@@ -2702,26 +2702,28 @@ Decl *TemplateDeclInstantiator::VisitUsi
 }
 
 Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
-                                     ClassScopeFunctionSpecializationDecl *Decl) {
+    ClassScopeFunctionSpecializationDecl *Decl) {
   CXXMethodDecl *OldFD = Decl->getSpecialization();
   CXXMethodDecl *NewFD =
     cast_or_null<CXXMethodDecl>(VisitCXXMethodDecl(OldFD, nullptr, true));
   if (!NewFD)
     return nullptr;
 
-  LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
-                        Sema::ForExternalRedeclaration);
-
-  TemplateArgumentListInfo TemplateArgs;
-  TemplateArgumentListInfo *TemplateArgsPtr = nullptr;
+  TemplateArgumentListInfo ExplicitTemplateArgs;
+  TemplateArgumentListInfo *ExplicitTemplateArgsPtr = nullptr;
   if (Decl->hasExplicitTemplateArgs()) {
-    TemplateArgs = Decl->templateArgs();
-    TemplateArgsPtr = &TemplateArgs;
+    if (SemaRef.Subst(Decl->templateArgs().getArgumentArray(),
+                      Decl->templateArgs().size(), ExplicitTemplateArgs,
+                      TemplateArgs))
+      return nullptr;
+    ExplicitTemplateArgsPtr = &ExplicitTemplateArgs;
   }
 
+  LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
+                        Sema::ForExternalRedeclaration);
   SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
-  if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
-                                                  Previous)) {
+  if (SemaRef.CheckFunctionTemplateSpecialization(
+          NewFD, ExplicitTemplateArgsPtr, Previous)) {
     NewFD->setInvalidDecl();
     return NewFD;
   }

Added: cfe/trunk/test/SemaTemplate/member-specialization.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/member-specialization.cpp?rev=348313&view=auto
==============================================================================
--- cfe/trunk/test/SemaTemplate/member-specialization.cpp (added)
+++ cfe/trunk/test/SemaTemplate/member-specialization.cpp Tue Dec  4 14:26:32 2018
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++17 -verify %s
+// expected-no-diagnostics
+
+template<typename T, typename U> struct X {
+  template<typename V> const V &as() { return V::error; }
+  template<> const U &as<U>() { return u; }
+  U u;
+};
+int f(X<int, int> x) {
+  return x.as<int>();
+}




More information about the cfe-commits mailing list