From 0a4074768a6ecd81513f0db02bfb0e96ab3e56a0 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Wed, 7 Oct 2009 17:30:37 +0000 Subject: [PATCH] Diagnose explicit instantiations and specializations that occur in class scope git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@83473 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 3 +++ lib/Sema/SemaTemplate.cpp | 10 ++++++---- test/CXX/temp/temp.spec/temp.expl.spec/p2.cpp | 6 ++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index c0a8651b36..6c8b631e39 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -927,6 +927,9 @@ def note_specialized_entity : Note< def err_template_spec_decl_function_scope : Error< "explicit %select{||specialization|instantiation|" "instantiation}0 of %1 in function scope">; +def err_template_spec_decl_class_scope : Error< + "explicit %select{||specialization|instantiation|" + "instantiation}0 of %1 in class scope">; def err_template_spec_decl_out_of_scope_global : Error< "%select{class template|class template partial|function template|member " "function|static data member|member class}0 specialization of %1 must " diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 9d7dd0a056..6792b28d1c 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -2433,11 +2433,13 @@ static bool CheckTemplateSpecializationScope(Sema &S, << TSK << Specialized; return true; } - - // FIXME: For everything except class template partial specializations, - // complain if the explicit specialization/instantiation occurs at class - // scope. + if (S.CurContext->isRecord() && !IsPartialSpecialization) { + S.Diag(Loc, diag::err_template_spec_decl_class_scope) + << TSK << Specialized; + return true; + } + // C++ [temp.class.spec]p6: // A class template partial specialization may be declared or redeclared // in any namespace scope in which its definition may be defined (14.5.1 diff --git a/test/CXX/temp/temp.spec/temp.expl.spec/p2.cpp b/test/CXX/temp/temp.spec/temp.expl.spec/p2.cpp index 7b50518605..77df60a25d 100644 --- a/test/CXX/temp/temp.spec/temp.expl.spec/p2.cpp +++ b/test/CXX/temp/temp.spec/temp.expl.spec/p2.cpp @@ -38,6 +38,12 @@ namespace N1 { template<> void N0::f0(double) { } // expected-error{{originally be declared}} +struct X1 { + template void f(T); + + template<> void f(int); // expected-error{{in class scope}} +}; + // -- class template namespace N0 { -- 2.50.1