Skip to content

Refactor C++ to return Swift::Expected instead of SWIFT_RETURN_THUNK #62701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Robertorosmaninho
Copy link
Contributor

@Robertorosmaninho Robertorosmaninho commented Dec 19, 2022

This is a simple PR to replace the of SWIFT_RETURN_THUNK(T, v) directly by Swift::Expected<T>(v) when it's declared on the #else clause of the #ifdef __cpp_exceptions on the C++ thunk that represents a Swift function on the bridging header.

Example:

  • Previously:
inline Swift::ThrowingResult<swift::Int> throwFunctionWithReturn() SWIFT_WARN_UNUSED_RESULT {
    void* opaqueError = nullptr;
    void* _ctx = nullptr;
    auto returnValue = _impl::$s9Functions23throwFunctionWithReturnSiyKF(_ctx, &opaqueError);
#ifdef __cpp_exceptions
    throw (Swift::Error(opaqueError));
#else
    return SWIFT_RETURN_THUNK(swift::Int, Swift::Error(opaqueError));
#endif
    return SWIFT_RETURN_THUNK(swift::Int, returnValue);
}
  • Proposed:
inline Swift::ThrowingResult<swift::Int> throwFunctionWithReturn() SWIFT_WARN_UNUSED_RESULT {
    void* opaqueError = nullptr;
    void* _ctx = nullptr;
    auto returnValue = _impl::$s9Functions23throwFunctionWithReturnSiyKF(_ctx, &opaqueError);
#ifdef __cpp_exceptions
    throw (Swift::Error(opaqueError));
#else
    return Swift::Expected<swift::Int>(Swift::Error(opaqueError));
#endif
    return SWIFT_RETURN_THUNK(swift::Int, returnValue);
}

@hyp
Copy link
Contributor

hyp commented Dec 19, 2022

@swift-ci please test

@hyp hyp added the c++ interop Feature: Interoperability with C++ label Dec 20, 2022
@hyp hyp merged commit a6e056b into swiftlang:main Dec 20, 2022
@Robertorosmaninho Robertorosmaninho deleted the cxx-interop/RefactorSwiftReturnThank branch December 20, 2022 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants