@@ -2117,7 +2117,25 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
2117
2117
indirectResult = F.begin ()->createFunctionArgument (
2118
2118
nativeConv.getSingleSILResultType (F.getTypeExpansionContext ()));
2119
2119
}
2120
-
2120
+
2121
+ // Before we do anything, see if our function type is async and has an
2122
+ // implicit isolated parameter. In such a case, we need to implicitly insert
2123
+ // it here before we insert other parameters.
2124
+ //
2125
+ // NOTE: We do not jump to it or do anything further since as mentioned above,
2126
+ // we might switch to the callee's actor as part of making the call... but we
2127
+ // don't need to do anything further than that because we're going to
2128
+ // immediately return.
2129
+ bool hasImplicitIsolatedParameter = false ;
2130
+ if (auto isolatedParameter = nativeFnTy->maybeGetIsolatedParameter ();
2131
+ isolatedParameter && nativeFnTy->isAsync () &&
2132
+ isolatedParameter->hasOption (SILParameterInfo::ImplicitLeading)) {
2133
+ auto loweredTy = getLoweredTypeForFunctionArgument (
2134
+ isolatedParameter->getArgumentType (&F));
2135
+ F.begin ()->createFunctionArgument (loweredTy);
2136
+ hasImplicitIsolatedParameter = true ;
2137
+ }
2138
+
2121
2139
// Forward the arguments.
2122
2140
SmallVector<SILValue, 8 > params;
2123
2141
@@ -2182,9 +2200,12 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
2182
2200
getParameterTypes (nativeCI.LoweredType .getParams (), hasSelfParam);
2183
2201
2184
2202
for (unsigned nativeParamIndex : indices (params)) {
2203
+ // Adjust the parameter if we inserted an implicit isolated parameter.
2204
+ unsigned nativeFnTyParamIndex = nativeParamIndex + hasImplicitIsolatedParameter;
2205
+
2185
2206
// Bring the parameter to +1.
2186
2207
auto paramValue = params[nativeParamIndex];
2187
- auto thunkParam = nativeFnTy->getParameters ()[nativeParamIndex ];
2208
+ auto thunkParam = nativeFnTy->getParameters ()[nativeFnTyParamIndex ];
2188
2209
// TODO: Could avoid a retain if the bridged parameter is also +0 and
2189
2210
// doesn't require a bridging conversion.
2190
2211
ManagedValue param;
0 commit comments