@@ -1550,6 +1550,17 @@ ASTBuilder::findDeclContext(NodePointer node) {
15501550 }
15511551 }
15521552
1553+ // FIXME: We shouldn't be attempting to find an exact extension match,
1554+ // clients only need the nominal for qualified lookup. Additionally, the
1555+ // module in which the extension resides is currently used to filter the
1556+ // lookup results. This means when we have multiple matches, the particular
1557+ // extension we choose matters.
1558+ //
1559+ // We ought to refactor things such that we return a module ABI name +
1560+ // nominal decl which downstream logic can use to lookup and limit results
1561+ // to only those that appear in the ABI module. Then we can delete all this
1562+ // logic.
1563+ SmallVector<ExtensionDecl *, 4 > genericExts;
15531564 for (auto *ext : nominalDecl->getExtensions ()) {
15541565 bool found = false ;
15551566 for (ModuleDecl *module : moduleDecls) {
@@ -1588,6 +1599,39 @@ ASTBuilder::findDeclContext(NodePointer node) {
15881599 if (requirements.empty ())
15891600 return ext;
15901601 }
1602+ genericExts.push_back (ext);
1603+ }
1604+ if (!genericSig)
1605+ return nullptr ;
1606+
1607+ SmallVector<Requirement, 2 > requirements;
1608+ SmallVector<InverseRequirement, 2 > inverses;
1609+ genericSig->getRequirementsWithInverses (requirements, inverses);
1610+
1611+ // If we didn't find a result yet, try again without invertible requirements
1612+ // since `demangleGenericSignature` won't include them, e.g won't include
1613+ // Copyable for:
1614+ //
1615+ // struct S<T: ~Copyable> {}
1616+ // protocol P: ~Copyable {}
1617+ // extension S where T: P/*, T: Copyable*/ {}
1618+ //
1619+ // We do this as a separate loop to avoid disturbing existing lookup
1620+ // behavior for cases where there's an extension with matching inverses,
1621+ // since the choice of extension matters (see above FIXME).
1622+ //
1623+ // FIXME: This is a complete hack, we ought to delete all this logic and
1624+ // just return the nominal + module ABI name.
1625+ for (auto *ext : genericExts) {
1626+ auto extSig = ext->getGenericSignature ().getCanonicalSignature ();
1627+ if (extSig.getGenericParams () != genericSig.getGenericParams ())
1628+ continue ;
1629+
1630+ SmallVector<Requirement, 2 > extReqs;
1631+ SmallVector<InverseRequirement, 2 > extInvs;
1632+ extSig->getRequirementsWithInverses (extReqs, extInvs);
1633+ if (extReqs == requirements)
1634+ return ext;
15911635 }
15921636
15931637 return nullptr ;
0 commit comments