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/lib/Sema/SemaTemplateInstantiate.cpp b/lib/Sema/SemaTemplateInstantiate.cpp
index c951b25..749c4a1 100644
--- a/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1650,9 +1650,18 @@
 
   CXXRecordDecl *PatternDef
     = cast_or_null<CXXRecordDecl>(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)