-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Description
Previous ID | SR-15131 |
Radar | rdar://problem/82535088 |
Original Reporter | @mattneub |
Type | Bug |
Status | Resolved |
Resolution | Done |
Environment
Version 13.0 beta 5 (13A5212g)
Additional Detail from JIRA
Votes | 2 |
Component/s | Compiler |
Labels | Bug, Concurrency |
Assignee | None |
Priority | Medium |
md5: 05c78bc787ea11fe3d88159e042026a7
Issue Description:
The proposal https://github.com/apple/swift-evolution/blob/main/proposals/0316-global-actors.md clearly states:
Global actor inference Subclasses infer actor isolation from their superclass:
class RemoteIconViewController: IconViewController { // implicitly @MainActor func connect() { ... } // implicitly @MainActor }
But this is not true. The following compiles and crashes:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Task.detached {
await self.viewControllerMethod()
}
}
func viewControllerMethod() {
UIImageView().image = UIImage()
}
}
The method `viewControllerMethod` is called on a background thread, not the main thread. The compiler fails to realize that this will happen, and we crash at runtime for calling a main thread method on a background thread. The method `viewControllerMethod` should be isolated to the main actor according to the proposal, because ViewController is a subclass of a main actor bound class, UIViewController.
I can fix the problem by marking ViewController `@MainActor` explicitly but what I'm saying is that according to the proposal (and common sense) I shouldn't have to.