Hello,
I'm facing an issue since Swift 5.9 which seems to consider short handed syntax to reference a method inside an isolated object as been isolated as well whereas is wasn't the case prior to Swift 5.9. It's a very problematic concerned regarding injection pattern where we would like to be able to register MainActor isolated objects outside the main thread.
I'd like to know if it's a bug from swift 5.9 compiler. If it isn't, I wonder is there a recommended way to perform injection pattern with isolated objects such as View or UIViewController.
Description
Accessing the init method of a @mainactor isolated object to store as a closure raises an isolation error since Swift 5.9.
Steps to reproduce
@MainActor
struct Component {
init() {}
}
struct Builder<T> {
private let builder: @MainActor () -> T
init(
builder: @MainActor @escaping () -> T
) {
self.builder = builder
}
@MainActor
func build() -> T {
builder()
}
}
let _ = Builder { Component() } // OK
let _ = Builder(builder: Component.init) // KO: Call to main actor-isolated initializer 'init()' in a synchronous nonisolated context
Expected behavior
I would expect the last line to compile successfully as it used to prior to Swift 5.9
Environment
Swift compiler version info
swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.106 clang-1500.0.40.1)
Target: arm64-apple-macosx13.0
Xcode version info
Xcode 15.0
Build version 15A5229h
Deployment target:
iOS 15.0
I wrote an issue on github last week but were not lucky yet [Swift 5.9] Using shorthand syntax to reference @MainActor isolated method from non isolated context raises error · Issue #68117 · apple/swift · GitHub