@@ -1566,24 +1566,28 @@ class ArchetypeBuilder::InferRequirementsWalker : public TypeWalker {
15661566 if (!genericSig)
15671567 return Action::Stop;
15681568
1569- auto params = genericSig-> getInnermostGenericParams ();
1569+ // / Retrieves the type substitution.
15701570 auto args = boundGeneric->getGenericArgs ();
1571+ auto genericSigDepth =
1572+ genericSig->getInnermostGenericParams ().front ()->getDepth ();
1573+ auto getTypeSubstitution = [&](SubstitutableType *dependentType) -> Type {
1574+ if (auto gp = dyn_cast<GenericTypeParamType>(dependentType)) {
1575+ if (gp->getDepth () == genericSigDepth)
1576+ return args[gp->getIndex ()];
1577+
1578+ return gp;
1579+ }
15711580
1572- // Produce substitutions from the generic parameters to the actual
1573- // arguments.
1574- TypeSubstitutionMap substitutions;
1575- for (unsigned i = 0 , n = params.size (); i != n; ++i) {
1576- substitutions[params[i]->getCanonicalType ()->castTo <SubstitutableType>()]
1577- = args[i];
1578- }
1581+ return dependentType;
1582+ };
15791583
15801584 // Handle the requirements.
15811585 RequirementSource source (RequirementSource::Inferred, Loc);
15821586 for (const auto &req : genericSig->getRequirements ()) {
15831587 switch (req.getKind ()) {
15841588 case RequirementKind::SameType: {
15851589 auto firstType = req.getFirstType ().subst (
1586- QueryTypeSubstitutionMap{substitutions} ,
1590+ getTypeSubstitution ,
15871591 Builder.getLookupConformanceFn ());
15881592 if (!firstType)
15891593 break ;
@@ -1594,7 +1598,7 @@ class ArchetypeBuilder::InferRequirementsWalker : public TypeWalker {
15941598 return Action::Continue;
15951599
15961600 auto secondType = req.getSecondType ().subst (
1597- QueryTypeSubstitutionMap{substitutions} ,
1601+ getTypeSubstitution ,
15981602 Builder.getLookupConformanceFn ());
15991603 if (!secondType)
16001604 break ;
@@ -1618,7 +1622,7 @@ class ArchetypeBuilder::InferRequirementsWalker : public TypeWalker {
16181622 case RequirementKind::Superclass:
16191623 case RequirementKind::Conformance: {
16201624 auto subjectType = req.getFirstType ().subst (
1621- QueryTypeSubstitutionMap{substitutions} ,
1625+ getTypeSubstitution ,
16221626 Builder.getLookupConformanceFn ());
16231627 if (!subjectType)
16241628 break ;
0 commit comments