@@ -868,7 +868,7 @@ Type AssociatedTypeInference::computeFixedTypeWitness(
868
868
869
869
Optional<AbstractTypeWitness>
870
870
AssociatedTypeInference::computeDefaultTypeWitness (
871
- AssociatedTypeDecl *assocType) {
871
+ AssociatedTypeDecl *assocType) const {
872
872
// Go find a default definition.
873
873
auto *const defaultedAssocType = findDefaultedAssociatedType (assocType);
874
874
if (!defaultedAssocType)
@@ -937,6 +937,57 @@ AssociatedTypeInference::computeAbstractTypeWitness(
937
937
return None;
938
938
}
939
939
940
+ void AssociatedTypeInference::collectAbstractTypeWitnesses (
941
+ TypeWitnessSystem &system,
942
+ ArrayRef<AssociatedTypeDecl *> unresolvedAssocTypes) const {
943
+ // First, look at all the protocols the adoptee conforms to and feed the
944
+ // same-type constraints in their requirement signatures to the system.
945
+ for (auto *const conformedProto :
946
+ adoptee->getAnyNominal ()->getAllProtocols (/* sorted=*/ true )) {
947
+ // FIXME: The RequirementMachine will assert on re-entrant construction.
948
+ // We should find a more principled way of breaking this cycle.
949
+ if (ctx.isRecursivelyConstructingRequirementMachine (
950
+ conformedProto->getGenericSignature ().getCanonicalSignature ()) ||
951
+ ctx.isRecursivelyConstructingRequirementMachine (conformedProto) ||
952
+ conformedProto->isComputingRequirementSignature ())
953
+ continue ;
954
+
955
+ for (const auto &req :
956
+ conformedProto->getRequirementSignature ().getRequirements ()) {
957
+ if (req.getKind () != RequirementKind::SameType) {
958
+ continue ;
959
+ }
960
+
961
+ system.addSameTypeRequirement (req);
962
+ }
963
+ }
964
+
965
+ // If the same-type constraints weren't enough to resolve an associated type,
966
+ // look for more options.
967
+ for (auto *const assocType : unresolvedAssocTypes) {
968
+ if (system.hasResolvedTypeWitness (assocType->getName ())) {
969
+ continue ;
970
+ }
971
+
972
+ // If we find a default type definition, feed it to the system.
973
+ if (const auto &typeWitness = computeDefaultTypeWitness (assocType)) {
974
+ system.addDefaultTypeWitness (typeWitness->getType (),
975
+ typeWitness->getDefaultedAssocType ());
976
+ } else {
977
+ // As a last resort, look for a generic parameter that matches the name
978
+ // of the associated type.
979
+ if (auto genericSig = dc->getGenericSignatureOfContext ()) {
980
+ for (auto *gp : genericSig.getInnermostGenericParams ()) {
981
+ if (gp->getName () == assocType->getName ()) {
982
+ system.addTypeWitness (assocType->getName (),
983
+ dc->mapTypeIntoContext (gp));
984
+ }
985
+ }
986
+ }
987
+ }
988
+ }
989
+ }
990
+
940
991
Type AssociatedTypeInference::substCurrentTypeWitnesses (Type type) {
941
992
// Local function that folds dependent member types with non-dependent
942
993
// bases into actual member references.
0 commit comments