@@ -481,7 +481,7 @@ void ConstraintSystem::PotentialBindings::inferDefaultTypes(
481481 break ;
482482
483483 // If this literal protocol is not a direct requirement it
484- // would be possible to change optionality while inferring
484+ // would not be possible to change optionality while inferring
485485 // bindings for a supertype, so this hack doesn't apply.
486486 if (!isDirectRequirement)
487487 break ;
@@ -506,17 +506,22 @@ void ConstraintSystem::PotentialBindings::inferDefaultTypes(
506506 binding.BindingType = type;
507507 }
508508
509- // If this is not a literal protocol or it has been "covered" by an existing
510- // binding it can't provide a default type.
511- auto isUnviableForDefaulting = [&literalProtocols](ProtocolDecl *protocol) {
512- auto literal = literalProtocols.find (protocol);
513- return literal == literalProtocols.end () || literal->second .second ;
514- };
515-
516509 for (auto *constraint : Protocols) {
517510 auto *protocol = constraint->getProtocol ();
518511
519- if (isUnviableForDefaulting (protocol))
512+ auto literal = literalProtocols.find (protocol);
513+ if (literal == literalProtocols.end ())
514+ continue ;
515+
516+ bool isDirectRequirement = false ;
517+ bool isCovered = false ;
518+
519+ std::tie (isDirectRequirement, isCovered) = literal->second ;
520+
521+ // Can't be defaulted because it's already covered by an
522+ // existing direct or transitive binding which is always
523+ // better.
524+ if (isCovered)
520525 continue ;
521526
522527 auto defaultType = TypeChecker::getDefaultType (protocol, cs.DC );
@@ -530,9 +535,8 @@ void ConstraintSystem::PotentialBindings::inferDefaultTypes(
530535 // requirement or inferred transitive one to identify binding
531536 // kind correctly.
532537 addPotentialBinding ({defaultType,
533- isDirectRequirement (constraint)
534- ? AllowedBindingKind::Subtypes
535- : AllowedBindingKind::Supertypes,
538+ isDirectRequirement ? AllowedBindingKind::Subtypes
539+ : AllowedBindingKind::Supertypes,
536540 constraint});
537541 }
538542}
0 commit comments