@@ -4562,19 +4562,29 @@ CheckTypeWitnessResult
45624562swift::checkTypeWitness (Type type, AssociatedTypeDecl *assocType,
45634563 const NormalProtocolConformance *Conf,
45644564 SubstOptions options) {
4565+ auto &ctx = assocType->getASTContext ();
4566+
45654567 if (type->hasError ())
4566- return ErrorType::get (assocType-> getASTContext () );
4568+ return ErrorType::get (ctx );
45674569
45684570 const auto proto = Conf->getProtocol ();
45694571 const auto dc = Conf->getDeclContext ();
4570- const auto genericSig = proto->getGenericSignature ();
4572+ const auto sig = proto->getGenericSignature ();
4573+
4574+ // FIXME: The RequirementMachine will assert on re-entrant construction.
4575+ // We should find a more principled way of breaking this cycle.
4576+ if (ctx.isRecursivelyConstructingRequirementMachine (sig.getCanonicalSignature ()) ||
4577+ ctx.isRecursivelyConstructingRequirementMachine (proto) ||
4578+ proto->isComputingRequirementSignature ())
4579+ return ErrorType::get (ctx);
4580+
45714581 const auto depTy = DependentMemberType::get (proto->getSelfInterfaceType (),
45724582 assocType);
45734583
45744584 Type contextType = type->hasTypeParameter () ? dc->mapTypeIntoContext (type)
45754585 : type;
45764586
4577- if (auto superclass = genericSig ->getSuperclassBound (depTy)) {
4587+ if (auto superclass = sig ->getSuperclassBound (depTy)) {
45784588 if (superclass->hasTypeParameter ()) {
45794589 // Replace type parameters with other known or tentative type witnesses.
45804590 superclass = superclass.subst (
@@ -4596,7 +4606,7 @@ swift::checkTypeWitness(Type type, AssociatedTypeDecl *assocType,
45964606 auto *module = dc->getParentModule ();
45974607
45984608 // Check protocol conformances.
4599- for (const auto reqProto : genericSig ->getRequiredProtocols (depTy)) {
4609+ for (const auto reqProto : sig ->getRequiredProtocols (depTy)) {
46004610 if (module ->lookupConformance (contextType, reqProto)
46014611 .isInvalid ())
46024612 return CheckTypeWitnessResult (reqProto->getDeclaredInterfaceType ());
@@ -4617,7 +4627,7 @@ swift::checkTypeWitness(Type type, AssociatedTypeDecl *assocType,
46174627 }
46184628 }
46194629
4620- if (genericSig ->requiresClass (depTy) &&
4630+ if (sig ->requiresClass (depTy) &&
46214631 !contextType->satisfiesClassConstraint ())
46224632 return CheckTypeWitnessResult (module ->getASTContext ().getAnyObjectType ());
46234633
0 commit comments