Default Actor Isolation and foundational protocols

I've been testing my open source libraries with Swift 6.2 and the new Default Actor Isolation concurrency build setting set to MainActor (with Complete strict concurrency turned on). My library Destinations uses protocols extensively, often applying conformance to foundational Swift protocols like Hashable and Identifiable. Many of these basic protocols are not flagged as running on the @MainActor in Beta 1, leading to situations like this:

Given this example code:

public protocol Contentable: Identifiable {
    var id: UUID { get }
}
 
final class ContentModel: Contentable {
    let id: UUID = UUID()
}

I get the warning:

Multiline Conformance of 'ContentModel' to protocol 'Contentable' crosses into main actor-isolated code and can cause data races; this is an error in the Swift 6 language mode.
Conformance depends on main actor-isolated conformance of 'ContentModel' to protocol 'Identifiable'

The fix Xcode suggests is to put a @MainActor before the Contentable protocol declaration in ContentModel, which seems to be a new attribute configuration in Swift 6.2. This solves the warning, but would create a lot of extra noise across the codebase.

Was it an oversight or a temporary omission that protocols like Hashable and Identifiable do not run on @MainActor by default, or is there some other reason they are excluded? Considering how often protocols in our code may conform to foundational protocols like this, it seems at odds to the MainActor mode of the Default Actor Isolation setting given that it was created to make concurrency easier and less boilerplate to implement.

2 Likes

Yes, seeing similar. Something like this, a struct that conforms to Codable, throwing the same error.

So I guess the same question - are we supposed to sprinkle @MainActor on those conformances like the fixit says or is this something that will be changed in a later seed?

struct FeedbinFeed: Identifiable, Codable, Sendable {
    let id: Int
    let title: String
    let siteURL: String?
    
    enum CodingKeys: String, CodingKey { // <<< this is an error same as OP's
        case id
        case title
        case siteURL = "site_url"
    }
}

This is a compiler bug! It's tracked on GitHub by Default main actor isolation fails to deeply apply to some protocol hierarchies · Issue #82222 · swiftlang/swift · GitHub

5 Likes

Good deal, thanks Holly for the heads up!

Thanks, Holly! I figured it might be.