@@ -1745,119 +1745,31 @@ class SubstFunctionTypePatternVisitor
1745
1745
}
1746
1746
1747
1747
auto decl = orig->getAnyNominal ();
1748
-
1749
- // Any type parameters we use as arguments to the nominal type must still
1750
- // satisfy the requirements on the nominal type. So we pull all of the
1751
- // generic requirements from the nominal type declaration into the
1752
- // substituted signature, then same-type-constrain those variables to the
1753
- // types we get by recursively visiting the bound generic arguments.
1748
+
1754
1749
auto moduleDecl = decl->getParentModule ();
1755
- auto origSubMap = orig->getContextSubstitutionMap (moduleDecl,
1756
- decl,
1757
- decl->getGenericEnvironment ());
1758
- auto substSubMap = subst->getContextSubstitutionMap (moduleDecl, decl,
1759
- decl->getGenericEnvironment ());
1760
-
1761
- auto nomGenericSig = decl->getGenericSignature ().getCanonicalSignature ();
1762
-
1763
- llvm::DenseMap<GenericTypeParamType*, GenericTypeParamType*>
1764
- substGPMapping;
1765
-
1766
- // Create parallel generic params for the nominal type's generic params.
1767
- for (unsigned i = 0 ; i < nomGenericSig.getGenericParams ().size (); ++i) {
1768
- auto nomTyParam = nomGenericSig.getGenericParams ()[i];
1769
- // If the nominal type same-type constrains away this generic parameter,
1770
- // we don't need to visit it.
1771
- if (nomGenericSig->isConcreteType (nomTyParam))
1772
- continue ;
1773
-
1774
- unsigned substGPIndex = substGenericParams.size ();
1775
- auto substGP = GenericTypeParamType::get (false , 0 ,
1776
- substGPIndex, TC.Context );
1777
- substGenericParams.push_back (substGP);
1778
- substReplacementTypes.push_back (Type ());
1779
- substGPMapping.insert ({nomGenericSig.getGenericParams ()[i], substGP});
1780
- }
1750
+ auto origSubMap = orig->getContextSubstitutionMap (moduleDecl, decl);
1751
+ auto substSubMap = subst->getContextSubstitutionMap (moduleDecl, decl);
1781
1752
1782
- // Create parallel requirements too, mapping from the generic signature's
1783
- // params to our parallel params.
1784
- auto substGPMap = SubstitutionMap::get (nomGenericSig,
1785
- [&](SubstitutableType *dt) -> Type {
1786
- auto mapping = substGPMapping.find (cast<GenericTypeParamType>(dt));
1787
- assert (mapping != substGPMapping.end ());
1788
- return mapping->second ;
1789
- }, [&](CanType dependentType,
1790
- Type conformingReplacementType,
1791
- ProtocolDecl *conformedProtocol) -> ProtocolConformanceRef {
1792
- // We should always be substituting type parameter for type parameter
1793
- return ProtocolConformanceRef (conformedProtocol);
1794
- });
1795
-
1796
- for (auto reqt : nomGenericSig.getRequirements ()) {
1797
- auto firstTy = reqt.getFirstType ().subst (substGPMap);
1798
- switch (auto kind = reqt.getKind ()) {
1799
- case RequirementKind::SameShape:
1800
- llvm_unreachable (" Same-shape requirement not supported here" );
1801
-
1802
- case RequirementKind::SameType:
1803
- // Skip same-type constraints that define away primary generic params,
1804
- // since we didn't duplicate those params.
1805
- if (reqt.getFirstType ()->getAs <GenericTypeParamType>()
1806
- && nomGenericSig->isConcreteType (reqt.getFirstType ()))
1807
- continue ;
1808
-
1809
- LLVM_FALLTHROUGH;
1810
- case RequirementKind::Conformance:
1811
- case RequirementKind::Superclass: {
1812
- auto secondTy = reqt.getSecondType ().subst (substGPMap);
1813
- substRequirements.push_back (Requirement (kind, firstTy, secondTy));
1814
- break ;
1815
- }
1816
- case RequirementKind::Layout:
1817
- substRequirements.push_back (Requirement (kind, firstTy,
1818
- reqt.getLayoutConstraint ()));
1819
- break ;
1820
- }
1821
- }
1753
+ auto nomGenericSig = decl->getGenericSignature ();
1822
1754
1823
- // Now recur into the type arguments, and same-type-constrain the
1824
- // substituted type arguments to the parallel generic parameter, giving us
1825
- // the intersection of constraints on the type binding and the nominal type's
1826
- // requirements.
1827
- llvm::DenseMap<GenericTypeParamType*, Type>
1828
- newGPMapping;
1829
-
1755
+ TypeSubstitutionMap replacementTypes;
1830
1756
for (auto gp : nomGenericSig.getGenericParams ()) {
1831
- if (nomGenericSig->isConcreteType (gp))
1832
- continue ;
1833
-
1834
1757
auto origParamTy = Type (gp).subst (origSubMap)
1835
1758
->getCanonicalType ();
1836
1759
auto substParamTy = Type (gp).subst (substSubMap)
1837
1760
->getCanonicalType ();
1838
-
1839
- auto newParamTy = visit (substParamTy,
1840
- AbstractionPattern (origSig, origParamTy));
1841
- newGPMapping.insert ({gp, newParamTy});
1842
- auto substGPTy = Type (gp).subst (substGPMap)->castTo <GenericTypeParamType>();
1843
- substRequirements.push_back (Requirement (RequirementKind::SameType,
1844
- newParamTy,
1845
- substGPTy));
1846
- assert (!substReplacementTypes[substGPTy->getIndex ()]);
1847
- substReplacementTypes[substGPTy->getIndex ()] = substParamTy;
1761
+
1762
+ replacementTypes[gp->getCanonicalType ()->castTo <SubstitutableType>()]
1763
+ = visit (substParamTy, AbstractionPattern (origSig, origParamTy));
1848
1764
}
1849
-
1765
+
1850
1766
auto newSubMap = SubstitutionMap::get (nomGenericSig,
1851
- [&](SubstitutableType *dt) -> Type {
1852
- auto mapping = newGPMapping.find (cast<GenericTypeParamType>(dt));
1853
- assert (mapping != newGPMapping.end ());
1854
- return mapping->second ;
1855
- }, [&](CanType dependentType,
1856
- Type conformingReplacementType,
1857
- ProtocolDecl *conformedProtocol) -> ProtocolConformanceRef {
1858
- // We should always be substituting type parameter for type parameter
1859
- return ProtocolConformanceRef (conformedProtocol);
1860
- });
1767
+ QueryTypeSubstitutionMap{replacementTypes},
1768
+ LookUpConformanceInModule (moduleDecl));
1769
+
1770
+ for (auto reqt : nomGenericSig.getRequirements ()) {
1771
+ substRequirements.push_back (reqt.subst (newSubMap));
1772
+ }
1861
1773
1862
1774
return decl->getDeclaredInterfaceType ().subst (newSubMap)->getCanonicalType ();
1863
1775
}
0 commit comments