@@ -543,10 +543,6 @@ bool TypeChecker::checkContextualRequirements(GenericTypeDecl *decl,
543543 noteLoc = loc;
544544 }
545545
546- if (contextSig) {
547- parentTy = contextSig.getGenericEnvironment ()->mapTypeIntoContext (parentTy);
548- }
549-
550546 const auto subMap = parentTy->getContextSubstitutions (decl->getDeclContext ());
551547 const auto genericSig = decl->getGenericSignature ();
552548
@@ -556,7 +552,16 @@ bool TypeChecker::checkContextualRequirements(GenericTypeDecl *decl,
556552 decl->getDeclaredInterfaceType (),
557553 genericSig.getGenericParams (),
558554 genericSig.getRequirements (),
559- QueryTypeSubstitutionMap{subMap});
555+ [&](SubstitutableType *type) -> Type {
556+ auto result = QueryTypeSubstitutionMap{subMap}(type);
557+ if (result->hasTypeParameter ()) {
558+ if (contextSig) {
559+ auto *genericEnv = contextSig.getGenericEnvironment ();
560+ return genericEnv->mapTypeIntoContext (result);
561+ }
562+ }
563+ return result;
564+ });
560565
561566 switch (result) {
562567 case RequirementCheckResult::Failure:
@@ -845,15 +850,6 @@ Type TypeResolution::applyUnboundGenericArguments(
845850 // Check the generic arguments against the requirements of the declaration's
846851 // generic signature.
847852
848- // First, map substitutions into context.
849- TypeSubstitutionMap contextualSubs = subs;
850- if (const auto contextSig = getGenericSignature ()) {
851- auto *genericEnv = contextSig.getGenericEnvironment ();
852- for (auto &pair : contextualSubs) {
853- pair.second = genericEnv->mapTypeIntoContext (pair.second );
854- }
855- }
856-
857853 SourceLoc noteLoc = decl->getLoc ();
858854 if (noteLoc.isInvalid ())
859855 noteLoc = loc;
@@ -862,7 +858,16 @@ Type TypeResolution::applyUnboundGenericArguments(
862858 module , loc, noteLoc,
863859 UnboundGenericType::get (decl, parentTy, getASTContext ()),
864860 genericSig.getGenericParams (), genericSig.getRequirements (),
865- QueryTypeSubstitutionMap{contextualSubs});
861+ [&](SubstitutableType *type) -> Type {
862+ auto result = QueryTypeSubstitutionMap{subs}(type);
863+ if (result->hasTypeParameter ()) {
864+ if (const auto contextSig = getGenericSignature ()) {
865+ auto *genericEnv = contextSig.getGenericEnvironment ();
866+ return genericEnv->mapTypeIntoContext (result);
867+ }
868+ }
869+ return result;
870+ });
866871
867872 switch (result) {
868873 case RequirementCheckResult::Failure:
0 commit comments