@@ -1662,11 +1662,19 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
1662
1662
getConstantInfo (getTypeExpansionContext (), foreignDeclRef);
1663
1663
auto foreignFnTy = foreignCI.SILFnType ;
1664
1664
1665
- // Find the foreign error convention and 'self' parameter index.
1665
+ // Find the foreign error/async convention and 'self' parameter index.
1666
+ bool hasError = false ;
1667
+ Optional<ForeignAsyncConvention> foreignAsync;
1668
+ if (nativeFnTy->isAsync ()) {
1669
+ foreignAsync = fd->getForeignAsyncConvention ();
1670
+ assert (foreignAsync && " couldn't find foreign async convention?!" );
1671
+ }
1666
1672
Optional<ForeignErrorConvention> foreignError;
1667
1673
if (nativeFnTy->hasErrorResult ()) {
1674
+ hasError = true ;
1668
1675
foreignError = fd->getForeignErrorConvention ();
1669
- assert (foreignError && " couldn't find foreign error convention!" );
1676
+ assert ((foreignError || foreignAsync)
1677
+ && " couldn't find foreign error or async convention for foreign error!" );
1670
1678
}
1671
1679
ImportAsMemberStatus memberStatus = fd->getImportAsMemberStatus ();
1672
1680
@@ -1707,7 +1715,7 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
1707
1715
1708
1716
// Set up the throw destination if necessary.
1709
1717
CleanupLocation cleanupLoc (fd);
1710
- if (foreignError ) {
1718
+ if (hasError ) {
1711
1719
prepareRethrowEpilog (cleanupLoc);
1712
1720
}
1713
1721
@@ -1719,14 +1727,20 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
1719
1727
SmallVector<ManagedValue, 8 > args;
1720
1728
unsigned foreignArgIndex = 0 ;
1721
1729
1722
- // A helper function to add a function error argument in the
1730
+ // A helper function to add a placeholder for a foreign argument in the
1723
1731
// appropriate position.
1724
- auto maybeAddForeignErrorArg = [&] {
1725
- if (foreignError &&
1726
- foreignArgIndex == foreignError->getErrorParameterIndex ()) {
1732
+ auto maybeAddForeignArg = [&]() -> bool {
1733
+ if ((foreignError
1734
+ && foreignArgIndex == foreignError->getErrorParameterIndex ())
1735
+ || (foreignAsync
1736
+ && foreignArgIndex == foreignAsync->completionHandlerParamIndex ()))
1737
+ {
1727
1738
args.push_back (ManagedValue ());
1728
1739
++foreignArgIndex;
1740
+ return true ;
1729
1741
}
1742
+
1743
+ return false ;
1730
1744
};
1731
1745
1732
1746
{
@@ -1768,7 +1782,7 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
1768
1782
llvm_unreachable (" unsupported convention" );
1769
1783
}
1770
1784
1771
- maybeAddForeignErrorArg ( );
1785
+ while ( maybeAddForeignArg () );
1772
1786
1773
1787
bool isSelf = (hasSelfParam && nativeParamIndex == params.size () - 1 );
1774
1788
@@ -1825,7 +1839,7 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
1825
1839
}
1826
1840
}
1827
1841
1828
- maybeAddForeignErrorArg ( );
1842
+ while ( maybeAddForeignArg () );
1829
1843
1830
1844
// Call the original.
1831
1845
auto subs = getForwardingSubstitutionMap ();
@@ -1846,7 +1860,7 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
1846
1860
bridgedFormalResultType),
1847
1861
nativeFormalResultType,
1848
1862
foreignError,
1849
- {}, // TODO foreignAsync
1863
+ foreignAsync,
1850
1864
ImportAsMemberStatus ());
1851
1865
1852
1866
auto init = indirectResult
0 commit comments