@@ -980,7 +980,6 @@ 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,
984
983
AssociatedTypeDecl *assocType);
985
984
986
985
// / Infer associated type witnesses for all relevant value requirements.
@@ -1679,8 +1678,7 @@ AssociatedTypeInference::inferTypeWitnessesViaValueWitnesses(
1679
1678
if (assocTypes.count (assocType) == 0 )
1680
1679
continue ;
1681
1680
1682
- auto reqInferred = inferTypeWitnessesViaAssociatedType (assocTypes,
1683
- assocType);
1681
+ auto reqInferred = inferTypeWitnessesViaAssociatedType (assocType);
1684
1682
if (!reqInferred.empty ())
1685
1683
result.push_back ({req, std::move (reqInferred)});
1686
1684
@@ -1828,10 +1826,12 @@ static Type removeSelfParam(ValueDecl *value, Type type) {
1828
1826
1829
1827
InferredAssociatedTypesByWitnesses
1830
1828
AssociatedTypeInference::inferTypeWitnessesViaAssociatedType (
1831
- const llvm::SetVector<AssociatedTypeDecl *> &allUnresolved,
1832
1829
AssociatedTypeDecl *assocType) {
1833
1830
InferredAssociatedTypesByWitnesses result;
1834
1831
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,6 +1856,34 @@ 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
+
1859
1887
// Form the default name _Default_Foo.
1860
1888
DeclNameRef defaultName;
1861
1889
{
@@ -2440,29 +2468,6 @@ AssociatedTypeInference::computeAbstractTypeWitness(
2440
2468
void AssociatedTypeInference::collectAbstractTypeWitnesses (
2441
2469
TypeWitnessSystem &system,
2442
2470
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
-
2466
2471
auto considerProtocolRequirements = [&](ProtocolDecl *conformedProto) {
2467
2472
// FIXME: The RequirementMachine will assert on re-entrant construction.
2468
2473
// We should find a more principled way of breaking this cycle.
0 commit comments