From: Richard Smith Date: Tue, 14 Feb 2017 23:27:44 +0000 (+0000) Subject: Do not implicitly instantiate the definition of a class template specialization X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=44d0f7062d219377f1c93ada5ae676baf40381c1;p=clang Do not implicitly instantiate the definition of a class template specialization that has been explicitly specialized! We assume in various places that we can tell the template specialization kind of a class type by looking at the declaration produced by TagType::getDecl. That was previously not quite true: for an explicit specialization, we could have first seen a template-id denoting the specialization (with a use that does not trigger an implicit instantiation of the defintiion) and then seen the first explicit specialization declaration. TagType::getDecl would previously return an arbitrary declaration when called on a not-yet-defined class; it now consistently returns the most recent declaration in that case. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@295118 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp index 8f22a1b197..1d064b135e 100644 --- a/lib/AST/Type.cpp +++ b/lib/AST/Type.cpp @@ -3023,8 +3023,10 @@ static TagDecl *getInterestingTagDecl(TagDecl *decl) { if (I->isCompleteDefinition() || I->isBeingDefined()) return I; } - // If there's no definition (not even in progress), return what we have. - return decl; + // If there's no definition (not even in progress), return the most recent + // declaration. This is important for template specializations, in order to + // pick the declaration with the most complete TemplateSpecializationKind. + return decl->getMostRecentDecl(); } TagDecl *TagType::getDecl() const { diff --git a/test/SemaTemplate/explicit-specialization-member.cpp b/test/SemaTemplate/explicit-specialization-member.cpp index f302836c7e..4300ceb17e 100644 --- a/test/SemaTemplate/explicit-specialization-member.cpp +++ b/test/SemaTemplate/explicit-specialization-member.cpp @@ -57,3 +57,14 @@ template struct Helper { template void Helper::func<2>() {} // expected-error {{cannot specialize a member}} \ // expected-error {{no function template matches}} } + +namespace b35070233 { + template struct Cls { + static void f() {} + }; + + void g(Cls); + + template<> struct Cls; // expected-note {{forward declaration}} + template<> void Cls::f(); // expected-error {{incomplete type}} +}