]> granicus.if.org Git - clang/commitdiff
Detect attempts to provide a specialization of a function within a
authorDouglas Gregor <dgregor@apple.com>
Tue, 8 Mar 2011 02:04:14 +0000 (02:04 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 8 Mar 2011 02:04:14 +0000 (02:04 +0000)
dependent scope and produce an error (rather than crashing). Fixes PR8979.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@127206 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDecl.cpp
test/CXX/temp/temp.spec/temp.expl.spec/p2.cpp

index ba439513a49bff47ec1dbb39e9390320868ad8c7..7cd6d9ed7f52e6000ccf3188e1bc80dd80d50840 100644 (file)
@@ -1721,6 +1721,8 @@ def err_template_spec_default_arg : Error<
 def err_not_class_template_specialization : Error<
   "cannot specialize a %select{dependent template|template template "
   "parameter}0">;
+def err_function_specialization_in_class : Error<
+  "cannot specialize a function %0 within class scope">;
 
 // C++ class template specializations and out-of-line definitions
 def err_template_spec_needs_header : Error<
index b6191e82cd25f3ea18603be8c1fe4a8585b6c969..f957fb7c1647164185495c67486cade5f904fbe6 100644 (file)
@@ -4067,9 +4067,14 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
                                                        Previous))
         NewFD->setInvalidDecl();
     } else if (isFunctionTemplateSpecialization) {
-      if (CheckFunctionTemplateSpecialization(NewFD,
-                                              (HasExplicitTemplateArgs ? &TemplateArgs : 0),
-                                              Previous))
+      if (CurContext->isDependentContext() && CurContext->isRecord()) {
+        Diag(NewFD->getLocation(), diag::err_function_specialization_in_class)
+          << NewFD->getDeclName();
+        NewFD->setInvalidDecl();
+        return 0;
+      } else if (CheckFunctionTemplateSpecialization(NewFD,
+                                  (HasExplicitTemplateArgs ? &TemplateArgs : 0),
+                                                     Previous))
         NewFD->setInvalidDecl();
     } else if (isExplicitSpecialization && isa<CXXMethodDecl>(NewFD)) {
       if (CheckMemberSpecialization(NewFD, Previous))
index 1032a87def133248e6f1b5bf5e2ac2a4be603dfa..229523557008ed40f793c6ee525cce89b2c843d5 100644 (file)
@@ -237,3 +237,15 @@ void test_func_template(N0::X0<void *> xvp, void *vp, const void *cvp,
   xvp.ft1(vp, i);
   xvp.ft1(vp, u);
 }
+
+namespace PR8979 {
+  template<typename Z>
+  struct X0 {
+    template <class T, class U> class Inner;
+    struct OtherInner;
+    template<typename T, typename U> void f(Inner<T, U>&);
+
+    typedef Inner<OtherInner, OtherInner> MyInner;
+    template<> void f(MyInner&); // expected-error{{cannot specialize a function 'f' within class scope}}
+  };
+}