Open
Description
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
Labels
Feature → attributes: the @main attributeFeature → concurrency: `actor` declarationsFeature → concurrency: Actor isolationFeature: Declaration and type attributesA deviation from expected or documented behavior. Also: expected but undesirable behavior.The Swift compiler itselfFeature: umbrella label for concurrency language featuresFeature → protocol: protocol conformancesFeature: declarationsFeature → protocol → conformances: default implementationsFeature → declarations: `extension` declarationsFeature → type declarations: Protocol declarationsArea → compiler: Semantic analysisBug: Unexpected behavior or incorrect output