Merge remote-tracking branch 'origin/swift-3.1-branch' into stable

* origin/swift-3.1-branch:
  Fix spelling mistake.
  [Sema] Avoid instantiating templates only when UncompilableErrorOccurred and FatalErrorOccurred are both set.
  Revert "[index] Handle properly C++14's template variables."
diff --git a/lib/Index/IndexSymbol.cpp b/lib/Index/IndexSymbol.cpp
index 4126bb6..d98d7c0 100644
--- a/lib/Index/IndexSymbol.cpp
+++ b/lib/Index/IndexSymbol.cpp
@@ -91,25 +91,6 @@
       Info.SubKinds |= (unsigned)SymbolSubKind::TemplateSpecialization;
     }
 
-  } else if (auto *VD = dyn_cast<VarDecl>(D)) {
-    Info.Kind = SymbolKind::Variable;
-    if (isa<CXXRecordDecl>(D->getDeclContext())) {
-      Info.Kind = SymbolKind::StaticProperty;
-      Info.Lang = SymbolLanguage::CXX;
-    }
-    if (isa<VarTemplatePartialSpecializationDecl>(D)) {
-      Info.Lang = SymbolLanguage::CXX;
-      Info.SubKinds |= (unsigned)SymbolSubKind::Generic;
-      Info.SubKinds |= (unsigned)SymbolSubKind::TemplatePartialSpecialization;
-    } else if (isa<VarTemplateSpecializationDecl>(D)) {
-      Info.Lang = SymbolLanguage::CXX;
-      Info.SubKinds |= (unsigned)SymbolSubKind::Generic;
-      Info.SubKinds |= (unsigned)SymbolSubKind::TemplateSpecialization;
-    } else if (VD->getDescribedVarTemplate()) {
-      Info.Lang = SymbolLanguage::CXX;
-      Info.SubKinds |= (unsigned)SymbolSubKind::Generic;
-    }
-
   } else {
     switch (D->getKind()) {
     case Decl::Import:
@@ -120,6 +101,16 @@
     case Decl::Function:
       Info.Kind = SymbolKind::Function;
       break;
+    case Decl::ParmVar:
+      Info.Kind = SymbolKind::Variable;
+      break;
+    case Decl::Var:
+      Info.Kind = SymbolKind::Variable;
+      if (isa<CXXRecordDecl>(D->getDeclContext())) {
+        Info.Kind = SymbolKind::StaticProperty;
+        Info.Lang = SymbolLanguage::CXX;
+      }
+      break;
     case Decl::Field:
       Info.Kind = SymbolKind::Field;
       if (const CXXRecordDecl *
diff --git a/lib/Index/IndexingContext.cpp b/lib/Index/IndexingContext.cpp
index e623a49..21869d8 100644
--- a/lib/Index/IndexingContext.cpp
+++ b/lib/Index/IndexingContext.cpp
@@ -130,10 +130,9 @@
   if (const ClassTemplateSpecializationDecl *
       SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
     TKind = SD->getSpecializationKind();
-  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+  }
+  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
     TKind = FD->getTemplateSpecializationKind();
-  } else if (auto *VD = dyn_cast<VarDecl>(D)) {
-    TKind = VD->getTemplateSpecializationKind();
   }
   switch (TKind) {
     case TSK_Undeclared:
@@ -165,10 +164,9 @@
   if (const ClassTemplateSpecializationDecl *
       SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
     return SD->getTemplateInstantiationPattern();
-  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+  }
+  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
     return FD->getTemplateInstantiationPattern();
-  } else if (auto *VD = dyn_cast<VarDecl>(D)) {
-    return VD->getTemplateInstantiationPattern();
   }
   return nullptr;
 }
diff --git a/lib/Index/USRGeneration.cpp b/lib/Index/USRGeneration.cpp
index 58f61c3..bec777d 100644
--- a/lib/Index/USRGeneration.cpp
+++ b/lib/Index/USRGeneration.cpp
@@ -286,15 +286,6 @@
 
   VisitDeclContext(D->getDeclContext());
 
-  if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) {
-    Out << "@VT";
-    VisitTemplateParameterList(VarTmpl->getTemplateParameters());
-  } else if (const VarTemplatePartialSpecializationDecl *PartialSpec
-             = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
-    Out << "@VP";
-    VisitTemplateParameterList(PartialSpec->getTemplateParameters());
-  }
-
   // Variables always have simple names.
   StringRef s = D->getName();
 
@@ -306,17 +297,6 @@
     IgnoreResults = true;
   else
     Out << '@' << s;
-
-  // For a template specialization, mangle the template arguments.
-  if (const VarTemplateSpecializationDecl *Spec
-                              = dyn_cast<VarTemplateSpecializationDecl>(D)) {
-    const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
-    Out << '>';
-    for (unsigned I = 0, N = Args.size(); I != N; ++I) {
-      Out << '#';
-      VisitTemplateArgument(Args.get(I));
-    }
-  }
 }
 
 void USRGenerator::VisitNonTypeTemplateParmDecl(
diff --git a/lib/Sema/SemaTemplateInstantiate.cpp b/lib/Sema/SemaTemplateInstantiate.cpp
index 4749962..e7b221d 100644
--- a/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/lib/Sema/SemaTemplateInstantiate.cpp
@@ -209,9 +209,11 @@
     sema::TemplateDeductionInfo *DeductionInfo)
     : SemaRef(SemaRef), SavedInNonInstantiationSFINAEContext(
                             SemaRef.InNonInstantiationSFINAEContext) {
-  // Don't allow further instantiation if a fatal error has occcured.  Any
-  // diagnostics we might have raised will not be visible.
-  if (SemaRef.Diags.hasFatalErrorOccurred()) {
+  // Don't allow further instantiation if a fatal error and an uncompilable
+  // error have occurred. Any diagnostics we might have raised will not be
+  // visible, and we do not need to construct a correct AST.
+  if (SemaRef.Diags.hasFatalErrorOccurred() &&
+      SemaRef.Diags.hasUncompilableErrorOccurred()) {
     Invalid = true;
     return;
   }
diff --git a/test/Index/Core/index-source.cpp b/test/Index/Core/index-source.cpp
index 7db5d53..61b9675 100644
--- a/test/Index/Core/index-source.cpp
+++ b/test/Index/Core/index-source.cpp
@@ -1,4 +1,4 @@
-// RUN: c-index-test core -print-source-symbols -- %s -std=c++14 -target x86_64-apple-macosx10.7 | FileCheck %s
+// RUN: c-index-test core -print-source-symbols -- %s -target x86_64-apple-macosx10.7 | FileCheck %s
 
 template <typename TemplArg>
 class TemplCls {
@@ -29,14 +29,3 @@
 // CHECK: [[@LINE+2]]:7 | function/C | operator new | c:@F@operator new#l# | __Znwm |
 // CHECK: [[@LINE+1]]:20 | type-alias/C | size_t | {{.*}} | Ref |
 void* operator new(size_t sz);
-
-// CHECK: [[@LINE+1]]:37 | variable(Gen)/C++ | tmplVar | c:index-source.cpp@VT>1#T@tmplVar | __ZL7tmplVar | Def | rel: 0
-template<typename T> static const T tmplVar = T(0);
-// CHECK: [[@LINE+1]]:29 | variable(Gen,TS)/C++ | tmplVar | c:index-source.cpp@tmplVar>#I | __ZL7tmplVarIiE | Def | rel: 0
-template<> static const int tmplVar<int> = 0;
-// CHECK: [[@LINE+2]]:5 | variable/C | gvi | c:@gvi | _gvi | Def | rel: 0
-// CHECK: [[@LINE+1]]:11 | variable(Gen,TS)/C++ | tmplVar | c:index-source.cpp@tmplVar>#I | __ZL7tmplVarIiE | Ref,Read | rel: 0
-int gvi = tmplVar<int>;
-// CHECK: [[@LINE+2]]:5 | variable/C | gvf | c:@gvf | _gvf | Def | rel: 0
-// CHECK: [[@LINE+1]]:11 | variable(Gen)/C++ | tmplVar | c:index-source.cpp@VT>1#T@tmplVar | __ZL7tmplVar | Ref,Read | rel: 0
-int gvf = tmplVar<float>;
diff --git a/test/SemaCXX/instantiate-template-fatal-error.cpp b/test/SemaCXX/instantiate-template-fatal-error.cpp
new file mode 100644
index 0000000..9c8916e
--- /dev/null
+++ b/test/SemaCXX/instantiate-template-fatal-error.cpp
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++14 %s
+
+#pragma clang diagnostic fatal "-Wall"
+#pragma clang diagnostic fatal "-Wold-style-cast"
+
+template <class T> bool foo0(const long long *a, T* b) {
+  return a == (const long long*)b; // expected-error {{use of old-style cast}}
+}
+
+template<class T>
+struct S1 {
+};
+
+template<class T>
+struct S2 : S1<T> {
+  bool m1(const long long *a, T *b) const { return foo0(a, b); }
+};
+
+bool foo1(const long long *a, int *b) {
+  S2<int> s2;
+  return s2.m1(a, b);
+}