uniffi-rs icon indicating copy to clipboard operation
uniffi-rs copied to clipboard

[Swift 6] Compilation issues when migrating to Swift 6

Open trevoranderson opened this issue 1 year ago • 6 comments

Similar to https://github.com/mozilla/uniffi-rs/issues/2274 but adding a few more from my project. This is all tested by setting SWIFT_STRICT_CONCURRENCY = complete in the project's build settings in XCode. Most of the issues are on the class that Swift passes into Rust to get notified about certain events. I believe it's called Foreign traits in the docs.

Foreign Trait Swift 6 concurrency issues

private enum UniffiCallbackInterfaceRustEventHandler {
    // Create the VTable using a series of closures.
    // Swift automatically converts these into C callback functions.
    static var vtable: UniffiVTableCallbackInterfaceRustEventHandler = .init(

gives the error

Static property 'vtable' is not concurrency-safe because it is nonisolated global shared mutable state; this is an error in the Swift 6 language mode

another location

public struct FfiConverterTypeRustEventHandler: FfiConverter {
    fileprivate static var handleMap = UniffiHandleMap<RustEventHandler>()

gives the error

Static property 'handleMap' is not concurrency-safe because it is nonisolated global shared mutable state; this is an error in the Swift 6 language mode

Initialization Result Swift 6 concurrency issues

This last one is perhaps more important because it seems like it will affect all Rust+Swift users. The comment seems to disagree with the compiler. (This one might be fixable by switching var to let)

// Use a global variable to perform the versioning checks. Swift ensures that
// the code inside is only computed once.
private var initializationResult: InitializationResult = {

produces error

Var 'initializationResult' is not concurrency-safe because it is nonisolated global shared mutable state; this is an error in the Swift 6 language mode

Fixing

In all cases, the trivial fix is to mark the relevant locations as nonisolated(unsafe) var, which should immediately solve the warnings and allow users to migrate to Swift6 while a more "Swifty" solution is designed. The InitializationResult one looks like just a one-liner bug

trevoranderson avatar Oct 17 '24 19:10 trevoranderson