@@ -1856,6 +1856,7 @@ static void emitRawApply(SILGenFunction &SGF,
18561856 CanSILFunctionType substFnType,
18571857 ApplyOptions options,
18581858 ArrayRef<SILValue> indirectResultAddrs,
1859+ SILValue indirectErrorAddr,
18591860 SmallVectorImpl<SILValue> &rawResults,
18601861 ExecutorBreadcrumb prevExecutor) {
18611862 SILFunctionConventions substFnConv (substFnType, SGF.SGM .M );
@@ -1880,6 +1881,10 @@ static void emitRawApply(SILGenFunction &SGF,
18801881#endif
18811882 argValues.append (indirectResultAddrs.begin (), indirectResultAddrs.end ());
18821883
1884+ assert (!!indirectErrorAddr == substFnConv.hasIndirectSILErrorResults ());
1885+ if (indirectErrorAddr)
1886+ argValues.push_back (indirectErrorAddr);
1887+
18831888 auto inputParams = substFnType->getParameters ();
18841889 assert (inputParams.size () == args.size ());
18851890
@@ -1978,6 +1983,7 @@ static void emitRawApply(SILGenFunction &SGF,
19781983 SILBasicBlock *errorBB =
19791984 SGF.getTryApplyErrorDest (loc, substFnType, prevExecutor,
19801985 substFnType->getErrorResult (),
1986+ indirectErrorAddr,
19811987 options.contains (ApplyFlags::DoesNotThrow));
19821988
19831989 options -= ApplyFlags::DoesNotThrow;
@@ -4876,7 +4882,8 @@ SILGenFunction::emitBeginApply(SILLocation loc, ManagedValue fn,
48764882 // Emit the call.
48774883 SmallVector<SILValue, 4 > rawResults;
48784884 emitRawApply (*this , loc, fn, subs, args, substFnType, options,
4879- /* indirect results*/ {}, rawResults, ExecutorBreadcrumb ());
4885+ /* indirect results*/ {}, /* indirect errors*/ {},
4886+ rawResults, ExecutorBreadcrumb ());
48804887
48814888 auto token = rawResults.pop_back_val ();
48824889 auto yieldValues = llvm::makeArrayRef (rawResults);
@@ -5411,6 +5418,16 @@ RValue SILGenFunction::emitApply(
54115418 SmallVector<SILValue, 4 > indirectResultAddrs;
54125419 resultPlan->gatherIndirectResultAddrs (*this , loc, indirectResultAddrs);
54135420
5421+ SILValue indirectErrorAddr;
5422+ if (substFnType->hasErrorResult ()) {
5423+ auto errorResult = substFnType->getErrorResult ();
5424+ if (errorResult.getConvention () == ResultConvention::Indirect) {
5425+ auto loweredErrorResultType = getSILType (errorResult, substFnType);
5426+ indirectErrorAddr = B.createAllocStack (loc, loweredErrorResultType);
5427+ enterDeallocStackCleanup (indirectErrorAddr);
5428+ }
5429+ }
5430+
54145431 // If the function returns an inner pointer, we'll need to lifetime-extend
54155432 // the 'self' parameter.
54165433 SILValue lifetimeExtendedSelf;
@@ -5540,7 +5557,8 @@ RValue SILGenFunction::emitApply(
55405557 {
55415558 SmallVector<SILValue, 1 > rawDirectResults;
55425559 emitRawApply (*this , loc, fn, subs, args, substFnType, options,
5543- indirectResultAddrs, rawDirectResults, breadcrumb);
5560+ indirectResultAddrs, indirectErrorAddr,
5561+ rawDirectResults, breadcrumb);
55445562 assert (rawDirectResults.size () == 1 );
55455563 rawDirectResult = rawDirectResults[0 ];
55465564 }
@@ -5709,12 +5727,22 @@ SILValue SILGenFunction::emitApplyWithRethrow(SILLocation loc, SILValue fn,
57095727 // Emit the rethrow logic.
57105728 {
57115729 B.emitBlock (errorBB);
5712- SILValue error = errorBB->createPhiArgument (
5713- fnConv.getSILErrorType (getTypeExpansionContext ()),
5714- OwnershipKind::Owned);
5730+
5731+ SILValue error;
5732+ bool indirectError = fnConv.hasIndirectSILErrorResults ();
5733+
5734+ if (!indirectError) {
5735+ error = errorBB->createPhiArgument (
5736+ fnConv.getSILErrorType (getTypeExpansionContext ()),
5737+ OwnershipKind::Owned);
5738+ }
57155739
57165740 Cleanups.emitCleanupsForReturn (CleanupLocation (loc), IsForUnwind);
5717- B.createThrow (loc, error);
5741+
5742+ if (!indirectError)
5743+ B.createThrow (loc, error);
5744+ else
5745+ B.createThrowAddr (loc);
57185746 }
57195747
57205748 // Enter the normal path.
0 commit comments