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

Use Swift typed throws #289

Open
chinedufn opened this issue Aug 25, 2024 · 3 comments
Open

Use Swift typed throws #289

chinedufn opened this issue Aug 25, 2024 · 3 comments

Comments

@chinedufn
Copy link
Owner

chinedufn commented Aug 25, 2024

Swift 6 will introduce a "typed throws" feature https://github.com/swiftlang/swift-evolution/blob/c0576bc0e479efabc97904020def8a8601939687/proposals/0413-typed-throws.md

// Copied from
// https://github.com/swiftlang/swift-evolution/blob/c0576bc0e479efabc97904020def8a8601939687/proposals/0413-typed-throws.md?plain=1#L224-L235
func callCat() throws(CatError) -> Cat {
  if Int.random(in: 0..<24) < 20 {
    throw .sleeps
  }
  // ...
}

Give the following bridge module:

#[swift_bridge::bridge]
mod ffi {
    enum MyError {
        Failed,
    }
    extern "Rust" {
        fn grow() -> Result<(), MyError>;
    }
}

We should emit a typed throw for Rust functions that return a Result.

// Currently generated Swift (before completing this issue)
func grow() throws {
    // ... calls Rust
}

// Typed throw (after completing this issue)
func grow() throws(MyError) {
    // ... calls Rust
}

We can add a #[swift_bridge(typed_throw)] attribute that allows people to use this in the 0.1.x branch, then remove the attribute once Swift 6 is the lowest Swift version supported by our "Minimum Supported Swift Version` policy #288

#[swift_bridge::bridge]
mod ffi {
    // We would stop requiring this attribute whenever we dropped support for Swift 5.x
    extern "Rust" {
        #[swift_bridge(typed_throw)]
        fn grow() -> Result<(), ()>;
    }
}
@NiwakaDev
Copy link
Collaborator

@chinedufn
I'd like to use the feature. Can I implement this feature?

@chinedufn
Copy link
Owner Author

Yeah!

@chinedufn
Copy link
Owner Author

then remove the attribute once Swift 6 is the lowest Swift version supported by our "Minimum Supported Swift Version` policy

Swift doesn't recommend using typed throws by default: https://github.com/swiftlang/swift-evolution/blob/d780651990fc5dec3e72c4c4203df1cff23bdf98/proposals/0413-typed-throws.md?plain=1#L448-L464

So, we may not want to default to using typed throws and instead keep the #[swift_bridge(typed_throw)] attribute even after Swift 6 is released.
Not sure. Needs more research.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants