Merge pull request #10718 from xedin/rdar-33067102-4.0

[4.0] [QoI] Don't assume that contextual type is always present for trailing closure diagnostics
diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index aae4bac..9c8e52f 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -3307,20 +3307,10 @@
 
   auto module = getParentModule();
 
-  auto genericSig = getGenericSignature();
-  // The signature should look like <Self where Self : ThisProtocol>, and we
-  // reuse the two parts of it because the parameter and the requirement are
-  // exactly what we need.
-  auto validSig = genericSig->getGenericParams().size() == 1 &&
-                  genericSig->getRequirements().size() == 1;
-  if (!validSig) {
-    // This doesn't look like a protocol we can handle, so some other error must
-    // have occurred (usually a protocol nested within another declaration)
-    return;
-  }
-
-  auto selfType = genericSig->getGenericParams()[0];
-  auto requirement = genericSig->getRequirements()[0];
+  auto selfType = getSelfInterfaceType()->castTo<GenericTypeParamType>();
+  auto requirement =
+    Requirement(RequirementKind::Conformance, selfType,
+                getDeclaredInterfaceType());
 
   GenericSignatureBuilder builder(getASTContext(),
                                   LookUpConformanceInModule(module));
@@ -3332,7 +3322,7 @@
   builder.addRequirement(
          requirement,
          GenericSignatureBuilder::RequirementSource
-          ::forRequirementSignature(selfPA, this),
+           ::forRequirementSignature(selfPA, this),
          nullptr);
   
   RequirementSignature = builder.computeGenericSignature(SourceLoc());
diff --git a/lib/Sema/TypeCheckDecl.cpp b/lib/Sema/TypeCheckDecl.cpp
index f969b8d..08a0151 100644
--- a/lib/Sema/TypeCheckDecl.cpp
+++ b/lib/Sema/TypeCheckDecl.cpp
@@ -7290,9 +7290,6 @@
 
     validateAttributes(*this, D);
 
-    if (!proto->isRequirementSignatureComputed())
-      proto->computeRequirementSignature();
-
     // If the protocol is @objc, it may only refine other @objc protocols.
     // FIXME: Revisit this restriction.
     if (proto->getAttrs().hasAttribute<ObjCAttr>()) {
diff --git a/lib/Sema/TypeCheckGeneric.cpp b/lib/Sema/TypeCheckGeneric.cpp
index 990ee55..6c10163 100644
--- a/lib/Sema/TypeCheckGeneric.cpp
+++ b/lib/Sema/TypeCheckGeneric.cpp
@@ -1221,6 +1221,13 @@
 
   prepareGenericParamList(gp, typeDecl);
 
+  // For a protocol, compute the requirement signature first. It will be used
+  // by clients of the protocol.
+  if (auto proto = dyn_cast<ProtocolDecl>(typeDecl)) {
+    if (!proto->isRequirementSignatureComputed())
+      proto->computeRequirementSignature();
+  }
+
   auto *env = checkGenericEnvironment(gp, dc, dc->getGenericSignatureOfContext(),
                                       /*allowConcreteGenericParams=*/false);
   typeDecl->setGenericEnvironment(env);
diff --git a/validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift b/validation-test/compiler_crashers_fixed/28753-conforms-equivclass-conformsto-end.swift
similarity index 83%
rename from validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift
rename to validation-test/compiler_crashers_fixed/28753-conforms-equivclass-conformsto-end.swift
index 66e67fc..6827b5d 100644
--- a/validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift
+++ b/validation-test/compiler_crashers_fixed/28753-conforms-equivclass-conformsto-end.swift
@@ -5,6 +5,5 @@
 // See https://swift.org/LICENSE.txt for license information
 // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
 
-// REQUIRES: asserts
-// RUN: not --crash %target-swift-frontend %s -emit-ir
+// RUN: not %target-swift-frontend %s -emit-ir
 protocol A:A{{}class a{let ca{a{
diff --git a/validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift b/validation-test/compiler_crashers_fixed/28791-formprotocolrelativetype-swift-protocoldecl-swift-genericsignaturebuilder-potent.swift
similarity index 74%
copy from validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift
copy to validation-test/compiler_crashers_fixed/28791-formprotocolrelativetype-swift-protocoldecl-swift-genericsignaturebuilder-potent.swift
index 66e67fc..1c5768e 100644
--- a/validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift
+++ b/validation-test/compiler_crashers_fixed/28791-formprotocolrelativetype-swift-protocoldecl-swift-genericsignaturebuilder-potent.swift
@@ -5,6 +5,5 @@
 // See https://swift.org/LICENSE.txt for license information
 // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
 
-// REQUIRES: asserts
-// RUN: not --crash %target-swift-frontend %s -emit-ir
-protocol A:A{{}class a{let ca{a{
+// RUN: not %target-swift-frontend %s -emit-ir
+extension CountableRange{class a{protocol P{protocol P{}typealias e:P
diff --git a/validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift b/validation-test/compiler_crashers_fixed/28792-conforms-equivclass-conformsto-end.swift
similarity index 79%
copy from validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift
copy to validation-test/compiler_crashers_fixed/28792-conforms-equivclass-conformsto-end.swift
index 66e67fc..7fd018a 100644
--- a/validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift
+++ b/validation-test/compiler_crashers_fixed/28792-conforms-equivclass-conformsto-end.swift
@@ -6,5 +6,5 @@
 // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
 
 // REQUIRES: asserts
-// RUN: not --crash %target-swift-frontend %s -emit-ir
-protocol A:A{{}class a{let ca{a{
+// RUN: not %target-swift-frontend %s -emit-ir
+protocol A:A{struct B{{}var a{class a{class ss:a
diff --git a/validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift b/validation-test/compiler_crashers_fixed/28794-swift-type-transformrec-llvm-function-ref-llvm-optional-swift-type-swift-typebas.swift
similarity index 75%
copy from validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift
copy to validation-test/compiler_crashers_fixed/28794-swift-type-transformrec-llvm-function-ref-llvm-optional-swift-type-swift-typebas.swift
index 66e67fc..f63fe5a 100644
--- a/validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift
+++ b/validation-test/compiler_crashers_fixed/28794-swift-type-transformrec-llvm-function-ref-llvm-optional-swift-type-swift-typebas.swift
@@ -5,6 +5,5 @@
 // See https://swift.org/LICENSE.txt for license information
 // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
 
-// REQUIRES: asserts
-// RUN: not --crash %target-swift-frontend %s -emit-ir
-protocol A:A{{}class a{let ca{a{
+// RUN: not %target-swift-frontend %s -emit-ir
+class a<a:{a {}extension{P{}protocol A:a{P{}var f
diff --git a/validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift b/validation-test/compiler_crashers_fixed/28795-inprotocol-isrequirementsignaturecomputed-missing-signature.swift
similarity index 79%
copy from validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift
copy to validation-test/compiler_crashers_fixed/28795-inprotocol-isrequirementsignaturecomputed-missing-signature.swift
index 66e67fc..1714776 100644
--- a/validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift
+++ b/validation-test/compiler_crashers_fixed/28795-inprotocol-isrequirementsignaturecomputed-missing-signature.swift
@@ -6,5 +6,6 @@
 // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
 
 // REQUIRES: asserts
-// RUN: not --crash %target-swift-frontend %s -emit-ir
-protocol A:A{{}class a{let ca{a{
+// RUN: not %target-swift-frontend %s -emit-ir
+protocol A:A.b{func 丏
+protocol b:Collection