@@ -2117,7 +2117,25 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
21172117 indirectResult = F.begin ()->createFunctionArgument (
21182118 nativeConv.getSingleSILResultType (F.getTypeExpansionContext ()));
21192119 }
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+
21212139 // Forward the arguments.
21222140 SmallVector<SILValue, 8 > params;
21232141
@@ -2182,9 +2200,12 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
21822200 getParameterTypes (nativeCI.LoweredType .getParams (), hasSelfParam);
21832201
21842202 for (unsigned nativeParamIndex : indices (params)) {
2203+ // Adjust the parameter if we inserted an implicit isolated parameter.
2204+ unsigned nativeFnTyParamIndex = nativeParamIndex + hasImplicitIsolatedParameter;
2205+
21852206 // Bring the parameter to +1.
21862207 auto paramValue = params[nativeParamIndex];
2187- auto thunkParam = nativeFnTy->getParameters ()[nativeParamIndex ];
2208+ auto thunkParam = nativeFnTy->getParameters ()[nativeFnTyParamIndex ];
21882209 // TODO: Could avoid a retain if the bridged parameter is also +0 and
21892210 // doesn't require a bridging conversion.
21902211 ManagedValue param;
0 commit comments