Skip to content

Main function provided by extension function for protocol is not MainActor  #65794

Open
@kiootic

Description

@kiootic

Description
The main function is not MainActor if it is provided by an extension function by conforming to a protocol.

Steps to reproduce

import Foundation

@main
struct Test: P {
}

protocol P {}

extension P {
    static func main() async {
        print(Thread.isMainThread) // prints false
    }
}
import Foundation

@main
struct Test: P {
    static func main() async {
        print(Thread.isMainThread) // prints true
    }
}

protocol P {}

extension P {
}

Expected behavior
If I understand SE-0323 correctly, both code should print true, since main function should be implicitly MainActor.

Environment
swift-driver version: 1.75.2 Apple Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100)
Target: arm64-apple-macosx13.0
Xcode 14.3
Build version 14E222b

Additional context

I encountered this problem while using Swift Argument Parser. The original code is something like this:

@main
struct Command: AsyncParsableCommand {
  @MainActor
  func run() async throws {
    // App is a SwiftUI App
    App.main()
  }
}

The code works in an old version of Xcode (~13.x), but failed with Fatal error: calling into SwiftUI on a non-main thread is not supported when upgrading to 14.x.

Metadata

Metadata

Assignees

No one assigned

    Labels

    @mainFeature → attributes: the @main attributeactorFeature → concurrency: `actor` declarationsactor isolationFeature → concurrency: Actor isolationattributesFeature: Declaration and type attributesbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconcurrencyFeature: umbrella label for concurrency language featuresconformancesFeature → protocol: protocol conformancesdeclarationsFeature: declarationsdefault implementationsFeature → protocol → conformances: default implementationsextensionFeature → declarations: `extension` declarationsprotocolFeature → type declarations: Protocol declarationsswift 5.9type checkerArea → compiler: Semantic analysisunexpected behaviorBug: Unexpected behavior or incorrect output

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions