From: John McCall Date: Wed, 27 Apr 2011 06:46:31 +0000 (+0000) Subject: Diagnose attempts to implicitly instantiate a template before it is X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d46a1125d43bcfd47fbd1206ebd1226863549390;p=clang Diagnose attempts to implicitly instantiate a template before it is fully defined. Somehow this escaped notice for a very long time. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@130298 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index f35d5c4b9b..9a29ba2be0 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1863,6 +1863,9 @@ def err_template_recursion_depth_exceeded : Error< def note_template_recursion_depth : Note< "use -ftemplate-depth-N to increase recursive template instantiation depth">; +def err_template_instantiate_within_definition : Error< + "%select{implicit|explicit}0 instantiation of template %1 within its" + " own definition">; def err_template_instantiate_undefined : Error< "%select{implicit|explicit}0 instantiation of undefined template %1">; def err_implicit_instantiate_member_undefined : Error< diff --git a/lib/Sema/SemaTemplateInstantiate.cpp b/lib/Sema/SemaTemplateInstantiate.cpp index c951b2573a..749c4a18a4 100644 --- a/lib/Sema/SemaTemplateInstantiate.cpp +++ b/lib/Sema/SemaTemplateInstantiate.cpp @@ -1650,9 +1650,18 @@ Sema::InstantiateClass(SourceLocation PointOfInstantiation, CXXRecordDecl *PatternDef = cast_or_null(Pattern->getDefinition()); - if (!PatternDef) { - if (!Complain) { + if (!PatternDef || PatternDef->isBeingDefined()) { + if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) { // Say nothing + } else if (PatternDef) { + assert(PatternDef->isBeingDefined()); + Diag(PointOfInstantiation, + diag::err_template_instantiate_within_definition) + << (TSK != TSK_ImplicitInstantiation) + << Context.getTypeDeclType(Instantiation); + // Not much point in noting the template declaration here, since + // we're lexically inside it. + Instantiation->setInvalidDecl(); } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { Diag(PointOfInstantiation, diag::err_implicit_instantiate_member_undefined) diff --git a/test/CXX/class/class.mem/p2.cpp b/test/CXX/class/class.mem/p2.cpp new file mode 100644 index 0000000000..09040d859c --- /dev/null +++ b/test/CXX/class/class.mem/p2.cpp @@ -0,0 +1,31 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// C++11 [class.mem]p2: +// A class is considered a completely-defined object type (or +// complete type) at the closing } of the class-specifier. Within +// the class member-specification, the class is regarded as complete +// within function bodies, default arguments, +// exception-specifications, and brace-or-equal-initializers for +// non-static data members (including such things in nested classes). +// Otherwise it is regarded as incomplete within its own class +// member-specification. + +namespace test0 { + struct A { // expected-note {{definition of 'test0::A' is not complete until the closing '}'}} + A x; // expected-error {{field has incomplete type 'test0::A'}} + }; +} + +namespace test1 { + template struct A { + A x; // expected-error {{implicit instantiation of template 'test1::A' within its own definition}} + }; +} + +namespace test2 { + template struct A; + template <> struct A {}; + template struct A { + A x; + }; +} diff --git a/test/SemaCXX/PR9460.cpp b/test/SemaCXX/PR9460.cpp index be8dc6eb30..2cc435e495 100644 --- a/test/SemaCXX/PR9460.cpp +++ b/test/SemaCXX/PR9460.cpp @@ -8,11 +8,11 @@ struct basic_string{ basic_string(aT*); }; -struct runtime_error{ -runtime_error( +struct runtime_error{ // expected-note {{candidate constructor}} + runtime_error( // expected-note {{candidate constructor}} basic_string struct{ // expected-error {{cannot combine with previous 'type-name' declaration specifier}} a(){ // expected-error {{requires a type specifier}} -runtime_error(0); + runtime_error(0); // expected-error {{no matching conversion}} } } ); diff --git a/test/SemaCXX/PR9461.cpp b/test/SemaCXX/PR9461.cpp index e24bd4d35a..ce17931324 100644 --- a/test/SemaCXX/PR9461.cpp +++ b/test/SemaCXX/PR9461.cpp @@ -26,7 +26,7 @@ basic_string<_CharT,_Traits,_Alloc>::basic_string(const _CharT*,const _Alloc&) :us(_S_construct) {string a;} -struct runtime_error{runtime_error(string);}; +struct runtime_error{runtime_error(string);}; // expected-note 2 {{candidate constructor}} struct system_error:runtime_error{ // expected-note {{to match}} expected-note {{specified here}} -system_error():time_error("" // expected-error 4 {{expected}} expected-error {{initializer}} expected-note {{to match}} +system_error():time_error("" // expected-error 4 {{expected}} expected-error {{initializer}} expected-note {{to match}} expected-error {{no matching constructor}}