[Interop][SwiftToCxx] Adding support to return Swift::Expected when exceptions are not available.

This commit is contained in:
Roberto Rosmaninho
2022-12-08 19:35:55 -03:00
parent a2633d942b
commit ce666ac592
4 changed files with 135 additions and 13 deletions

View File

@@ -9,7 +9,11 @@
// CHECK-LABEL: namespace _impl {
// CHECK: SWIFT_EXTERN void $s9Functions18emptyThrowFunctionyyKF(SWIFT_CONTEXT void * _Nonnull _ctx, SWIFT_ERROR_RESULT void * _Nullable * _Nullable _error) SWIFT_CALL; // emptyThrowFunction()
// CHECK: SWIFT_EXTERN void $s9Functions18testDestroyedErroryyKF(SWIFT_CONTEXT void * _Nonnull _ctx, SWIFT_ERROR_RESULT void * _Nullable * _Nullable _error) SWIFT_CALL; // testDestroyedError()
// CHECK: SWIFT_EXTERN void $s9Functions13throwFunctionyyKF(SWIFT_CONTEXT void * _Nonnull _ctx, SWIFT_ERROR_RESULT void * _Nullable * _Nullable _error) SWIFT_CALL; // throwFunction()
// CHECK: SWIFT_EXTERN ptrdiff_t $s9Functions31throwFunctionWithPossibleReturnyS2iKF(ptrdiff_t a, SWIFT_CONTEXT void * _Nonnull _ctx, SWIFT_ERROR_RESULT void * _Nullable * _Nullable _error) SWIFT_CALL; // throwFunctionWithPossibleReturn(_:)
// CHECK: SWIFT_EXTERN ptrdiff_t $s9Functions23throwFunctionWithReturnSiyKF(SWIFT_CONTEXT void * _Nonnull _ctx, SWIFT_ERROR_RESULT void * _Nullable * _Nullable _error) SWIFT_CALL; // throwFunctionWithReturn()
// CHECK: }
@@ -26,12 +30,16 @@ public enum NaiveErrors : Error {
@_expose(Cxx)
public func emptyThrowFunction() throws { print("passEmptyThrowFunction") }
// CHECK: inline void emptyThrowFunction() {
// CHECK: inline Swift::ThrowingResult<void> emptyThrowFunction() {
// CHECK: void* opaqueError = nullptr;
// CHECK: void* _ctx = nullptr;
// CHECK: _impl::$s9Functions18emptyThrowFunctionyyKF(_ctx, &opaqueError);
// CHECK: if (opaqueError != nullptr)
// CHECK: throw (Swift::Error(opaqueError))
// CHECK: #ifdef __cpp_exceptions
// CHECK: throw (Swift::Error(opaqueError));
// CHECK: #else
// CHECK: return SWIFT_RETURN_THUNK(void, Swift::Error(opaqueError));
// CHECK: #endif
// CHECK: }
class TestDestroyed {
@@ -48,12 +56,16 @@ public struct DestroyedError : Error {
@_expose(Cxx)
public func testDestroyedError() throws { throw DestroyedError() }
// CHECK: inline void testDestroyedError() {
// CHECK: inline Swift::ThrowingResult<void> testDestroyedError() {
// CHECK: void* opaqueError = nullptr;
// CHECK: void* _ctx = nullptr;
// CHECK: _impl::$s9Functions18testDestroyedErroryyKF(_ctx, &opaqueError);
// CHECK: if (opaqueError != nullptr)
// CHECK: throw (Swift::Error(opaqueError))
// CHECK: #ifdef __cpp_exceptions
// CHECK: throw (Swift::Error(opaqueError));
// CHECK: #else
// CHECK: return SWIFT_RETURN_THUNK(void, Swift::Error(opaqueError));
// CHECK: #endif
// CHECK: }
@_expose(Cxx)
@@ -62,12 +74,38 @@ public func throwFunction() throws {
throw NaiveErrors.throwError
}
// CHECK: inline void throwFunction() {
// CHECK: inline Swift::ThrowingResult<void> throwFunction() {
// CHECK: void* opaqueError = nullptr;
// CHECK: void* _ctx = nullptr;
// CHECK: _impl::$s9Functions13throwFunctionyyKF(_ctx, &opaqueError);
// CHECK: if (opaqueError != nullptr)
// CHECK: throw (Swift::Error(opaqueError))
// CHECK: #ifdef __cpp_exceptions
// CHECK: throw (Swift::Error(opaqueError));
// CHECK: #else
// CHECK: return SWIFT_RETURN_THUNK(void, Swift::Error(opaqueError));
// CHECK: #endif
// CHECK: }
@_expose(Cxx)
public func throwFunctionWithPossibleReturn(_ a: Int) throws -> Int {
print("passThrowFunctionWithPossibleReturn")
if (a == 0) {
throw NaiveErrors.returnError
}
return 0
}
// CHECK: inline Swift::ThrowingResult<swift::Int> throwFunctionWithPossibleReturn(swift::Int a) SWIFT_WARN_UNUSED_RESULT {
// CHECK: void* opaqueError = nullptr;
// CHECK: void* _ctx = nullptr;
// CHECK: auto returnValue = _impl::$s9Functions31throwFunctionWithPossibleReturnyS2iKF(a, _ctx, &opaqueError);
// CHECK: if (opaqueError != nullptr)
// CHECK: #ifdef __cpp_exceptions
// CHECK: throw (Swift::Error(opaqueError));
// CHECK: #else
// CHECK: return SWIFT_RETURN_THUNK(swift::Int, Swift::Error(opaqueError));
// CHECK: #endif
// CHECK: return SWIFT_RETURN_THUNK(swift::Int, returnValue);
// CHECK: }
@_expose(Cxx)
@@ -77,11 +115,14 @@ public func throwFunctionWithReturn() throws -> Int {
return 0
}
// CHECK: inline swift::Int throwFunctionWithReturn() SWIFT_WARN_UNUSED_RESULT {
// CHECK: inline Swift::ThrowingResult<swift::Int> throwFunctionWithReturn() SWIFT_WARN_UNUSED_RESULT {
// CHECK: void* opaqueError = nullptr;
// CHECK: void* _ctx = nullptr;
// CHECK: auto returnValue = _impl::$s9Functions23throwFunctionWithReturnSiyKF(_ctx, &opaqueError);
// CHECK: if (opaqueError != nullptr)
// CHECK: throw (Swift::Error(opaqueError))
// CHECK: return returnValue;
// CHECK: #ifdef __cpp_exceptions
// CHECK: throw (Swift::Error(opaqueError));
// CHECK: #else
// CHECK: return SWIFT_RETURN_THUNK(swift::Int, Swift::Error(opaqueError));
// CHECK: #endif
// CHECK: return SWIFT_RETURN_THUNK(swift::Int, returnValue);
// CHECK: }