Skip to content
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

[cxx-interop] Errors/Exceptions crash the Swift runtime instead of being forwarded to C++ #75290

Open
mrousavy opened this issue Jul 17, 2024 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++

Comments

@mrousavy
Copy link

Description

I'm bridging Swift classes to C++ to use them from C++.

One of my classes has a function that throws an error, but I haven't found a way to implement error throwing in Swift yet (other than through Optional results).

I tried multiple approaches:

Swift errors

enum SomeError: Error {
  case failed
}

public class MySwiftClass {
  public func throwError() throws {
    throw SomeError.failed
  }
}

^ the problem with this approach is that throwError() is no longer accessible from C++ once it has throws in it's method signature.

Obj-C errors

public class MySwiftClass {
  public func throwError() throws {
    let error = NSException(name: NSExceptionName("Failed"), reason: "This failed!")
    error.raise()
  }
}

^ the problem with this approach is that the exception cannot be propagated to C++, it will always be "unhandled"

image

C++ errors

namespace helpers {
  void throwCppError(std::string message) { throw std::runtime_error(message); }
}
public class MySwiftClass {
  public func throwError() throws {
    helpers.throwCppError("This failed!")
  }
}

^ the problem with this approach is the same as with Obj-C errors - they are not propagated to C++ and will always be "unhandled". I cannot wrap them in a try/catch in C++.

Reproduction

enum SomeError: Error {
  case failed
}

public class MySwiftClass {
  public func throwError() throws {
    throw SomeError.failed
  }
}

Expected behavior

I expect errors/exceptions to be propagated upwards to C++ so I can handle them from there.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Additional information

No response

@mrousavy mrousavy added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jul 17, 2024
@hyp
Copy link
Contributor

hyp commented Jul 17, 2024

Calling Swift functions that throw is not yet supported from C++.
Swift will not support throwing C++/ObjC exceptions correctly, so they will not be propagated to C++.
C++/ObjC exceptions that are thrown in C++ and not caught right now trap in Swift. Ideally Swift would in the future support catching such exceptions when a call to C++ is made, but that's not yet supported.

@mrousavy
Copy link
Author

Thanks for your reply!

Swift will not support throwing C++/ObjC exceptions correctly, so they will not be propagated to C++.
C++/ObjC exceptions that are thrown in C++ and not caught right now trap in Swift. Ideally Swift would in the future support catching such exceptions when a call to C++ is made, but that's not yet supported.

Is that something that's on some kind of roadmap? Throwing errors in Swift, and catching them in C++?
Should I open a feature request / enhancement for this to track this, or can we use this issue here?

@mrousavy mrousavy changed the title C++ Interop: Errors crash the Swift runtime [cxx-interop] Errors/Exceptions crash the Swift runtime instead of being forwarded to C++ Jul 18, 2024
@hborla hborla added c++ interop Feature: Interoperability with C++ and removed triage needed This issue needs more specific labels labels Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++
Projects
None yet
Development

No branches or pull requests

3 participants