Skip to content

[SR-15131] MainActor class isolation not inherited as advertised #57457

@mattneub

Description

@mattneub
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconcurrencyFeature: umbrella label for concurrency language features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions