@@ -2939,13 +2939,13 @@ static ManagedValue createThunk(SILGenFunction &SGF,
2939
2939
2940
2940
static CanSILFunctionType buildWithoutActuallyEscapingThunkType (
2941
2941
SILGenFunction &SGF, CanSILFunctionType &noEscapingType,
2942
- CanSILFunctionType &escapingType, GenericEnvironment *&genericEnv) {
2942
+ CanSILFunctionType &escapingType, GenericEnvironment *&genericEnv,
2943
+ SubstitutionMap &interfaceSubs) {
2943
2944
2944
2945
assert (escapingType->getExtInfo () ==
2945
2946
noEscapingType->getExtInfo ().withNoEscape (false ));
2946
2947
2947
2948
CanType inputSubstType, outputSubstType;
2948
- SubstitutionMap interfaceSubs;
2949
2949
return SGF.buildThunkType (noEscapingType, escapingType,
2950
2950
inputSubstType, outputSubstType,
2951
2951
genericEnv, interfaceSubs,
@@ -3000,12 +3000,13 @@ SILGenFunction::createWithoutActuallyEscapingClosure(
3000
3000
->getExtInfo ()
3001
3001
.withNoEscape (false ));
3002
3002
3003
+ SubstitutionMap interfaceSubs;
3003
3004
GenericEnvironment *genericEnv = nullptr ;
3004
3005
auto noEscapingFnTy =
3005
3006
noEscapingFunctionValue.getType ().castTo <SILFunctionType>();
3006
3007
3007
3008
auto thunkType = buildWithoutActuallyEscapingThunkType (
3008
- *this , noEscapingFnTy, escapingFnTy, genericEnv);
3009
+ *this , noEscapingFnTy, escapingFnTy, genericEnv, interfaceSubs );
3009
3010
3010
3011
auto *thunk = SGM.getOrCreateReabstractionThunk (
3011
3012
thunkType, noEscapingFnTy, escapingFnTy, F.isSerialized ());
@@ -3016,17 +3017,24 @@ SILGenFunction::createWithoutActuallyEscapingClosure(
3016
3017
buildWithoutActuallyEscapingThunkBody (thunkSGF);
3017
3018
}
3018
3019
3019
- CanSILFunctionType substFnType = thunkType->substGenericArgs (
3020
- F.getModule (), thunk->getForwardingSubstitutions ());
3020
+ CanSILFunctionType substFnTy = thunkType;
3021
+ // Use the subsitution map in the context of the current function.
3022
+ // thunk->getForwardingSubstitutionMap() / thunk might have been created in a
3023
+ // different function's generic enviroment.
3024
+ SmallVector<Substitution, 4 > subs;
3025
+ if (auto genericSig = thunkType->getGenericSignature ()) {
3026
+ genericSig->getSubstitutions (interfaceSubs, subs);
3027
+ substFnTy = thunkType->substGenericArgs (F.getModule (), interfaceSubs);
3028
+ }
3021
3029
3022
3030
// Create it in our current function.
3023
3031
auto thunkValue = B.createFunctionRef (loc, thunk);
3024
3032
SILValue noEscapeValue =
3025
3033
noEscapingFunctionValue.ensurePlusOne (*this , loc).forward (*this );
3026
3034
SingleValueInstruction *thunkedFn = B.createPartialApply (
3027
3035
loc, thunkValue,
3028
- SILType::getPrimitiveObjectType (substFnType ),
3029
- thunk-> getForwardingSubstitutions () ,
3036
+ SILType::getPrimitiveObjectType (substFnTy ),
3037
+ subs ,
3030
3038
noEscapeValue,
3031
3039
SILType::getPrimitiveObjectType (escapingFnTy));
3032
3040
// We need to ensure the 'lifetime' of the trivial values context captures. As
0 commit comments