@@ -194,6 +194,28 @@ Type MapLocalArchetypesOutOfContext::operator()(SubstitutableType *type) const {
194194 llvm_unreachable (" Fell off the end" );
195195}
196196
197+ static Type mapIntoLocalContext (GenericTypeParamType *param, unsigned baseDepth,
198+ ArrayRef<GenericEnvironment *> capturedEnvs) {
199+ assert (!param->isParameterPack ());
200+ unsigned envIndex = param->getDepth () - baseDepth;
201+ assert (envIndex < capturedEnvs.size ());
202+ auto *capturedEnv = capturedEnvs[envIndex];
203+ auto localInterfaceType = capturedEnv->getGenericSignature ()
204+ .getInnermostGenericParams ()[param->getIndex ()];
205+ assert (localInterfaceType->getIndex () == param->getIndex ());
206+ return capturedEnvs[envIndex]->mapTypeIntoContext (localInterfaceType);
207+ }
208+
209+ Type MapIntoLocalArchetypeContext::operator ()(SubstitutableType *type) const {
210+ unsigned baseDepth = baseGenericEnv->getGenericSignature ().getNextDepth ();
211+
212+ auto param = cast<GenericTypeParamType>(type);
213+ if (param->getDepth () >= baseDepth)
214+ return mapIntoLocalContext (param, baseDepth, capturedEnvs);
215+
216+ return baseGenericEnv->mapTypeIntoContext (param);
217+ }
218+
197219// / Given a substitution map for a call to a local function or closure, extend
198220// / it to include all captured element archetypes; they become primary archetypes
199221// / inside the body of the function.
@@ -215,15 +237,8 @@ swift::buildSubstitutionMapWithCapturedEnvironments(
215237 genericSigWithCaptures,
216238 [&](SubstitutableType *type) -> Type {
217239 auto param = cast<GenericTypeParamType>(type);
218- if (param->getDepth () >= baseDepth) {
219- assert (!param->isParameterPack ());
220- unsigned envIndex = param->getDepth () - baseDepth;
221- assert (envIndex < capturedEnvs.size ());
222- auto *capturedEnv = capturedEnvs[envIndex];
223- auto localInterfaceType = capturedEnv->getGenericSignature ()
224- .getInnermostGenericParams ()[param->getIndex ()];
225- return capturedEnvs[envIndex]->mapTypeIntoContext (localInterfaceType);
226- }
240+ if (param->getDepth () >= baseDepth)
241+ return mapIntoLocalContext (param, baseDepth, capturedEnvs);
227242 return Type (type).subst (baseSubMap);
228243 },
229244 [&](CanType origType, Type substType,
0 commit comments