Skip to content

Commit 9ee583e

Browse files
committed
Look at whether the result is returned indirectly
Remove assertion that is no longer true.
1 parent 0a6c87c commit 9ee583e

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

lib/SILGen/SILGenBridging.cpp

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,8 +1331,8 @@ static SILFunctionType *
13311331
emitObjCThunkArguments(SILGenFunction &SGF, SILLocation loc, SILDeclRef thunk,
13321332
SmallVectorImpl<SILValue> &args,
13331333
SILValue &foreignErrorSlot, SILValue &foreignAsyncSlot,
1334-
std::optional<ForeignErrorConvention> &foreignError,
1335-
std::optional<ForeignAsyncConvention> &foreignAsync,
1334+
std::optional<ForeignErrorConvention> foreignError,
1335+
std::optional<ForeignAsyncConvention> foreignAsync,
13361336
CanType &nativeFormalResultTy,
13371337
CanType &bridgedFormalResultTy) {
13381338
SILDeclRef native = thunk.asForeign(false);
@@ -1355,20 +1355,6 @@ emitObjCThunkArguments(SILGenFunction &SGF, SILLocation loc, SILDeclRef thunk,
13551355
SmallVector<ManagedValue, 8> bridgedArgs;
13561356
bridgedArgs.reserve(objcFnTy->getParameters().size());
13571357

1358-
// Find the foreign error and async conventions if we have one.
1359-
if (thunk.hasDecl()) {
1360-
if (auto func = dyn_cast<AbstractFunctionDecl>(thunk.getDecl())) {
1361-
foreignError = func->getForeignErrorConvention();
1362-
foreignAsync = func->getForeignAsyncConvention();
1363-
}
1364-
}
1365-
1366-
// We don't know what to do with indirect results from the Objective-C side.
1367-
assert((SGF.F.getRepresentation() ==
1368-
SILFunctionType::Representation::CFunctionPointer ||
1369-
objcFnTy->getNumIndirectFormalResults() == 0) &&
1370-
"Objective-C methods cannot have indirect results");
1371-
13721358
auto bridgedFormalTypes = getParameterTypes(objcFormalFnTy.getParams());
13731359
bridgedFormalResultTy = objcFormalFnTy.getResult();
13741360

@@ -1618,27 +1604,40 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
16181604
}
16191605
}
16201606

1607+
std::optional<ForeignErrorConvention> foreignError;
1608+
std::optional<ForeignAsyncConvention> foreignAsync;
1609+
1610+
// Find the foreign error and async conventions if we have one.
1611+
if (thunk.hasDecl()) {
1612+
if (auto func = dyn_cast<AbstractFunctionDecl>(thunk.getDecl())) {
1613+
foreignError = func->getForeignErrorConvention();
1614+
foreignAsync = func->getForeignAsyncConvention();
1615+
}
1616+
}
1617+
1618+
// If we are bridging a Swift method with Any return value(s), create a
1619+
// stack allocation to hold the result(s), since Any is address-only.
16211620
SmallVector<SILValue, 4> args;
1621+
SILFunctionConventions funcConv = F.getConventions();
1622+
bool needsBridging = true;
16221623
if (substConv.hasIndirectSILResults()) {
1623-
if (F.getRepresentation() ==
1624-
SILFunctionType::Representation::CFunctionPointer) {
1625-
// Pass the result address of the thunk to the native function.
1626-
auto resultTy =
1627-
F.getConventions().getSingleSILResultType(getTypeExpansionContext());
1628-
assert(resultTy ==
1629-
substConv.getSingleSILResultType(getTypeExpansionContext()) &&
1630-
"result type mismatch");
1631-
args.push_back(F.begin()->createFunctionArgument(resultTy));
1632-
} else {
1633-
// If we are bridging a Swift method with Any return value(s), create a
1634-
// stack allocation to hold the result(s), since Any is address-only.
1635-
for (auto result : substConv.getResults()) {
1636-
if (!substConv.isSILIndirect(result)) {
1637-
continue;
1638-
}
1639-
args.push_back(emitTemporaryAllocation(
1640-
loc, substConv.getSILType(result, getTypeExpansionContext())));
1624+
for (auto result : substConv.getResults()) {
1625+
if (!substConv.isSILIndirect(result)) {
1626+
continue;
1627+
}
1628+
1629+
if (!foreignAsync && funcConv.hasIndirectSILResults()) {
1630+
auto resultTy =
1631+
funcConv.getSingleSILResultType(getTypeExpansionContext());
1632+
assert(substConv.getSingleSILResultType(getTypeExpansionContext()) ==
1633+
resultTy);
1634+
args.push_back(F.begin()->createFunctionArgument(resultTy));
1635+
needsBridging = false;
1636+
break;
16411637
}
1638+
1639+
args.push_back(emitTemporaryAllocation(
1640+
loc, substConv.getSILType(result, getTypeExpansionContext())));
16421641
}
16431642
}
16441643

@@ -1648,8 +1647,6 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
16481647
Scope argScope(Cleanups, CleanupLocation(loc));
16491648

16501649
// Bridge the arguments.
1651-
std::optional<ForeignErrorConvention> foreignError;
1652-
std::optional<ForeignAsyncConvention> foreignAsync;
16531650
SILValue foreignErrorSlot;
16541651
SILValue foreignAsyncSlot;
16551652
CanType nativeFormalResultType, bridgedFormalResultType;
@@ -1876,7 +1873,7 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
18761873
if (foreignAsync) {
18771874
result = passResultToCompletionHandler(result);
18781875
} else {
1879-
if (F.getRepresentation() != SILFunctionType::Representation::CFunctionPointer) {
1876+
if (needsBridging) {
18801877
if (substConv.hasIndirectSILResults()) {
18811878
assert(substTy->getNumResults() == 1);
18821879
result = args[0];

0 commit comments

Comments
 (0)