@@ -1017,6 +1017,10 @@ class AssociatedTypeInference {
10171017 std::pair<Type, TypeDecl *>
10181018 computeDerivedTypeWitness (AssociatedTypeDecl *assocType);
10191019
1020+ // / See if we have a generic parameter named the same as this associated
1021+ // / type.
1022+ Type computeGenericParamWitness (AssociatedTypeDecl *assocType) const ;
1023+
10201024 // / Compute a type witness without using a specific potential witness.
10211025 llvm::Optional<AbstractTypeWitness>
10221026 computeAbstractTypeWitness (AssociatedTypeDecl *assocType);
@@ -2389,6 +2393,7 @@ Type AssociatedTypeInference::computeFixedTypeWitness(
23892393 // any fix this associated type to a concrete type.
23902394 for (auto conformance : getPeerConformances (conformance)) {
23912395 auto *conformedProto = conformance->getProtocol ();
2396+
23922397 auto sig = conformedProto->getGenericSignature ();
23932398
23942399 // FIXME: The RequirementMachine will assert on re-entrant construction.
@@ -2656,6 +2661,28 @@ AssociatedTypeInference::computeAbstractTypeWitness(
26562661 return llvm::None;
26572662}
26582663
2664+ // / Look for a generic parameter that matches the name of the
2665+ // / associated type.
2666+ Type AssociatedTypeInference::computeGenericParamWitness (
2667+ AssociatedTypeDecl *assocType) const {
2668+ if (auto genericSig = dc->getGenericSignatureOfContext ()) {
2669+ // Ignore the generic parameters for AsyncIteratorProtocol.Failure and
2670+ // AsyncSequence.Failure.
2671+ if (!isAsyncIteratorProtocolFailure (assocType)) {
2672+ for (auto *gp : genericSig.getInnermostGenericParams ()) {
2673+ // Packs cannot witness associated type requirements.
2674+ if (gp->isParameterPack ())
2675+ continue ;
2676+
2677+ if (gp->getName () == assocType->getName ())
2678+ return dc->mapTypeIntoContext (gp);
2679+ }
2680+ }
2681+ }
2682+
2683+ return Type ();
2684+ }
2685+
26592686void AssociatedTypeInference::collectAbstractTypeWitnesses (
26602687 TypeWitnessSystem &system,
26612688 ArrayRef<AssociatedTypeDecl *> unresolvedAssocTypes) const {
@@ -2704,35 +2731,15 @@ void AssociatedTypeInference::collectAbstractTypeWitnesses(
27042731 if (system.hasResolvedTypeWitness (assocType->getName ()))
27052732 continue ;
27062733
2707- // If we find a default type definition, feed it to the system.
2708- if (const auto &typeWitness = computeDefaultTypeWitness (assocType)) {
2734+ if (auto gpType = computeGenericParamWitness (assocType)) {
2735+ system.addTypeWitness (assocType->getName (), gpType, /* preferred=*/ true );
2736+ } else if (const auto &typeWitness = computeDefaultTypeWitness (assocType)) {
27092737 bool preferred = (typeWitness->getDefaultedAssocType ()->getDeclContext ()
27102738 == conformance->getProtocol ());
27112739 system.addDefaultTypeWitness (typeWitness->getType (),
27122740 typeWitness->getDefaultedAssocType (),
27132741 preferred);
2714- } else {
2715- // As a last resort, look for a generic parameter that matches the name
2716- // of the associated type.
2717- if (auto genericSig = dc->getGenericSignatureOfContext ()) {
2718- // Ignore the generic parameters for AsyncIteratorProtocol.Failure and
2719- // AsyncSequence.Failure.
2720- if (!isAsyncIteratorProtocolFailure (assocType)) {
2721- for (auto *gp : genericSig.getInnermostGenericParams ()) {
2722- // Packs cannot witness associated type requirements.
2723- if (gp->isParameterPack ())
2724- continue ;
2725-
2726- if (gp->getName () == assocType->getName ()) {
2727- system.addTypeWitness (assocType->getName (),
2728- dc->mapTypeIntoContext (gp),
2729- /* preferred=*/ true );
2730- }
2731- }
2732- }
2733- }
27342742 }
2735-
27362743 }
27372744}
27382745
@@ -3150,8 +3157,15 @@ AssociatedTypeDecl *AssociatedTypeInference::inferAbstractTypeWitnesses(
31503157
31513158 // If simplification failed, give up.
31523159 if (type->hasTypeParameter ()) {
3153- LLVM_DEBUG (llvm::dbgs () << " -- Simplification failed: " << type << " \n " );
3154- return assocType;
3160+ if (auto gpType = computeGenericParamWitness (assocType)) {
3161+ LLVM_DEBUG (llvm::dbgs () << " -- Found generic parameter as last resort: "
3162+ << gpType << " \n " );
3163+ type = gpType;
3164+ typeWitnesses.insert (assocType, {type, reqDepth});
3165+ } else {
3166+ LLVM_DEBUG (llvm::dbgs () << " -- Simplification failed: " << type << " \n " );
3167+ return assocType;
3168+ }
31553169 }
31563170
31573171 if (const auto failed =
0 commit comments