@@ -980,6 +980,7 @@ class AssociatedTypeInference {
980
980
981
981
// / Infer associated type witnesses for the given associated type.
982
982
InferredAssociatedTypesByWitnesses inferTypeWitnessesViaAssociatedType (
983
+ const llvm::SetVector<AssociatedTypeDecl *> &allUnresolved,
983
984
AssociatedTypeDecl *assocType);
984
985
985
986
// / Infer associated type witnesses for all relevant value requirements.
@@ -1678,7 +1679,8 @@ AssociatedTypeInference::inferTypeWitnessesViaValueWitnesses(
1678
1679
if (assocTypes.count (assocType) == 0 )
1679
1680
continue ;
1680
1681
1681
- auto reqInferred = inferTypeWitnessesViaAssociatedType (assocType);
1682
+ auto reqInferred = inferTypeWitnessesViaAssociatedType (assocTypes,
1683
+ assocType);
1682
1684
if (!reqInferred.empty ())
1683
1685
result.push_back ({req, std::move (reqInferred)});
1684
1686
@@ -1826,12 +1828,10 @@ static Type removeSelfParam(ValueDecl *value, Type type) {
1826
1828
1827
1829
InferredAssociatedTypesByWitnesses
1828
1830
AssociatedTypeInference::inferTypeWitnessesViaAssociatedType (
1831
+ const llvm::SetVector<AssociatedTypeDecl *> &allUnresolved,
1829
1832
AssociatedTypeDecl *assocType) {
1830
1833
InferredAssociatedTypesByWitnesses result;
1831
1834
1832
- LLVM_DEBUG (llvm::dbgs () << " Considering associated type:\n " ;
1833
- assocType->dump (llvm::dbgs ()));
1834
-
1835
1835
// Check if this associated type is actually fixed to a fully concrete type by
1836
1836
// a same-type requirement in a protocol that our conforming type conforms to.
1837
1837
//
@@ -1856,34 +1856,6 @@ AssociatedTypeInference::inferTypeWitnessesViaAssociatedType(
1856
1856
}
1857
1857
}
1858
1858
1859
- // Look for a generic parameter with the same name as this associated type.
1860
- if (ctx.LangOpts .EnableExperimentalAssociatedTypeInference ) {
1861
- // Ignore the generic parameters for AsyncIteratorProtocol.Failure and
1862
- // AsyncSequence.Failure.
1863
- if (!isAsyncIteratorProtocolFailure (assocType)) {
1864
- if (auto genericSig = dc->getGenericSignatureOfContext ()) {
1865
- for (auto *gp : genericSig.getInnermostGenericParams ()) {
1866
- // Packs cannot witness associated type requirements.
1867
- if (gp->isParameterPack ())
1868
- continue ;
1869
-
1870
- if (gp->getName () == assocType->getName ()) {
1871
- auto witnessType = dc->mapTypeIntoContext (gp);
1872
-
1873
- // Add this result.
1874
- InferredAssociatedTypesByWitness inferred;
1875
- inferred.Witness = gp->getDecl ();
1876
- inferred.Inferred .push_back ({assocType, witnessType});
1877
- result.push_back (std::move (inferred));
1878
-
1879
- LLVM_DEBUG (llvm::dbgs () << " Candidate generic parameter:\n " ;
1880
- gp->dump (llvm::dbgs ()));
1881
- }
1882
- }
1883
- }
1884
- }
1885
- }
1886
-
1887
1859
// Form the default name _Default_Foo.
1888
1860
DeclNameRef defaultName;
1889
1861
{
@@ -2468,6 +2440,29 @@ AssociatedTypeInference::computeAbstractTypeWitness(
2468
2440
void AssociatedTypeInference::collectAbstractTypeWitnesses (
2469
2441
TypeWitnessSystem &system,
2470
2442
ArrayRef<AssociatedTypeDecl *> unresolvedAssocTypes) const {
2443
+ // Look for suitably-named generic parameters first, before we go digging
2444
+ // through same-type requirements of protocols.
2445
+ if (auto genericSig = dc->getGenericSignatureOfContext ()) {
2446
+ for (auto *const assocType : unresolvedAssocTypes) {
2447
+ // Ignore the generic parameters for AsyncIteratorProtocol.Failure and
2448
+ // AsyncSequence.Failure.
2449
+ if (isAsyncIteratorProtocolFailure (assocType))
2450
+ continue ;
2451
+
2452
+ for (auto *gp : genericSig.getInnermostGenericParams ()) {
2453
+ // Packs cannot witness associated type requirements.
2454
+ if (gp->isParameterPack ())
2455
+ continue ;
2456
+
2457
+ if (gp->getName () == assocType->getName ()) {
2458
+ system.addTypeWitness (assocType->getName (),
2459
+ dc->mapTypeIntoContext (gp),
2460
+ /* preferred=*/ true );
2461
+ }
2462
+ }
2463
+ }
2464
+ }
2465
+
2471
2466
auto considerProtocolRequirements = [&](ProtocolDecl *conformedProto) {
2472
2467
// FIXME: The RequirementMachine will assert on re-entrant construction.
2473
2468
// We should find a more principled way of breaking this cycle.
0 commit comments