In Swift 6 language mode, why does a `@MainActor` view model fail to conform to a plain protocol, and what are the fixes, including Swift 6.2's isolated conformances?
answer
- nonisolated requirement, isolated witness
- generic code may call from anywhere
- nonisolated witness or a run-time check
- isolate the conformance itself
basics
~20 sA plain protocol requirement can be called from any context, but the class's method may run only on the main actor. Fix it with nonisolated witnesses, a @preconcurrency conformance or, since Swift 6.2, a @MainActor conformance.
solid answer
~50 sA protocol with no isolation promises generic code that it may call `refresh()` from anywhere, while a `@MainActor` class's `refresh()` may run only on the main actor. Swift reports "conformance of 'GalleryViewModel' to protocol 'Refreshing' crosses into main actor-isolated code and can cause data races": a warning in Swift 5 language mode, an error in Swift 6 mode. The fixes: mark the witnesses `nonisolated`, which loses access to isolated state; write `@preconcurrency Refreshing`, which suppresses the error and, in Swift 6 mode, checks at run time that each call is on the main actor, trapping otherwise; or, from Swift 6.2, write `@MainActor Refreshing`, an **isolated conformance** that only main-actor code may use and that cannot satisfy a `Sendable` requirement. Inferring that isolation automatically needs the upcoming feature `InferIsolatedConformances`, which Swift 6 mode does not turn on.
code
swift · 22 linesprotocol Refreshing {
func refresh()
}
@MainActor
final class GalleryViewModel: @MainActor Refreshing {
var refreshCount = 0
func refresh() { refreshCount += 1 }
}
func refreshAll<T: Refreshing>(_ items: [T]) {
for item in items { item.refresh() }
}
@MainActor
func refreshOnMain(_ model: GalleryViewModel) {
refreshAll([model]) // fine: the conformance is used on the main actor
}
func refreshAnywhere(_ model: GalleryViewModel) {
refreshAll([model]) // error in Swift 6 mode: used in nonisolated context
}go deeper
Recall that a @MainActor class's methods cannot satisfy a plain protocol requirement without help, because the protocol allows calls from anywhere.
Explain why the conformance crosses isolation and how the diagnostic differs between Swift 5 and Swift 6 language modes.
Choose between nonisolated witnesses, @preconcurrency and an isolated conformance, and say what each costs at compile time and run time.
Set the migration rule for third-party protocols: which conformances may trap at run time and when to require Swift 6.2 isolated conformances.
## Why the conformance crosses isolation A protocol requirement written without isolation, `protocol Refreshing { func refresh() }`, is **nonisolated**: any code that holds a `some Refreshing` or a generic `T: Refreshing` may call `refresh()` synchronously, from whatever context it runs in. A class marked `@MainActor` isolates its methods to the main actor, so its `refresh()` touches state that only main-actor code may touch. If the conformance were accepted as is, a generic function running on a background executor could call `refresh()` and mutate main-actor state concurrently with the UI. Swift calls this a conformance that **crosses into actor-isolated code**. ## What the compiler reports, by mode | Situation | Swift 5 mode | Swift 6 mode | |---|---|---| | `@MainActor` class, isolated witness, plain conformance | warning: "conformance of 'GalleryViewModel' to protocol 'Refreshing' crosses into main actor-isolated code and can cause data races" | the same text as an error | | `actor` with an isolated witness for a synchronous requirement | error | error | | isolated conformance used from nonisolated code | warning, "this is an error in the Swift 6 language mode" | error: "main actor-isolated conformance of 'GalleryViewModel' to 'Refreshing' cannot be used in nonisolated context" | The diagnostic's notes suggest the fixes, and the compiler's documentation for the `ConformanceIsolation` diagnostic group explains each one. ## The fixes | Fix | Spelling | What it costs | Since | |---|---|---|---| | Nonisolated witnesses | `nonisolated func refresh()` | the body cannot touch isolated state | Swift 5.5 | | Nonisolated stored property | `nonisolated let id` for a read-only requirement | the value must be immutable and `Sendable` | Swift 5.5 | | Run-time checked conformance | `: @preconcurrency Refreshing` | a call off the main actor traps instead of failing to compile | Swift 6.0 | | Isolated conformance | `: @MainActor Refreshing` | usable only from main-actor code | Swift 6.2 | The `@preconcurrency` conformance from SE-0423 suppresses the static error and inserts a check that the witness is called on the main actor's executor. That check is the `DynamicActorIsolation` upcoming feature, which Swift 6 mode turns on; in Swift 5 mode without the flag the error is suppressed but no check is emitted. A common pre-6.2 alternative is a `nonisolated` witness that wraps its body in `MainActor.assumeIsolated { ... }`, which also traps when the assumption is wrong. Both fit protocols from older frameworks that document "called on the main thread" but cannot say so in their types. ## Isolated conformances in detail SE-0470 lets the conformance itself carry a global actor: `final class GalleryViewModel: @MainActor Refreshing`. The type then conforms, but only for code that runs on the main actor: - a `@MainActor` function may pass the view model to `refreshAll<T: Refreshing>(_:)`; - a nonisolated function may not; Swift 6 mode rejects it as used "in nonisolated context"; - the conformance cannot satisfy a `Sendable` requirement, so `T: Refreshing & Sendable` rejects it with "cannot satisfy conformance requirement for a 'Sendable' type parameter", in both modes, because a `Sendable` value could carry the conformance to another isolation domain; - it applies to **global actors** such as the main actor, not to an instance of an `actor` type. The proposal also describes inferring the isolation, so that a `@MainActor` type's conformances become `@MainActor` without spelling it out. That inference is the upcoming feature `InferIsolatedConformances`, a future-mode feature: Swift 6 mode does **not** turn it on. Projects that adopt default main-actor isolation are advised to enable it, a setting covered with Sendable and data-race safety. ## Choosing a fix 1. If the witness does not need main-actor state, make it `nonisolated`: the conformance then works everywhere, including `Sendable` contexts. 2. If the protocol is yours and is really main-actor-only, mark the protocol `@MainActor`. 3. If it is someone else's protocol whose callers are always main-actor code and you build with Swift 6.2 or later, use an isolated conformance. 4. If you must support an older compiler, or the callers are not visible to the type checker, use `@preconcurrency` and accept a run-time trap as the failure mode. Whatever you choose, the protocol's own semantics and default implementations stay the business of the lessons on protocols; this lesson is about the isolation of the conformance.
- Why can an isolated conformance not be passed to a generic function that requires `T: Refreshing & Sendable`?A `Sendable` value may be handed to another isolation domain, and its conformance would travel with it, letting code off the main actor call `refresh()`. Swift 6.4 therefore rejects it: the main actor-isolated conformance "cannot satisfy conformance requirement for a 'Sendable' type parameter", in both language modes. Either make the conformance nonisolated or drop the `Sendable` requirement.
- What does a `@preconcurrency` conformance do at run time in Swift 6 language mode?It suppresses the static conformance-isolation error and makes each isolated witness check, on entry, that it is running on the main actor's executor. If generic code calls `refresh()` from a background task, the program stops instead of racing. In Swift 5 mode without the `DynamicActorIsolation` feature, only the diagnostic is suppressed and no check runs.
- An isolated conformance can be used anywhere once the type is Sendable.
- In Swift 6 mode a @preconcurrency conformance silences the error with no run-time check.
- Swift 6 language mode infers @MainActor conformances for @MainActor types automatically.
- Marking the witnesses nonisolated still lets them mutate the view model's properties.
- An actor instance can isolate a conformance to itself the way the main actor can.
Assumes Swift 6.4: the crossing conformance is a warning in Swift 5 mode and an error in Swift 6 mode; isolated conformances arrived in Swift 6.2 (SE-0470), @preconcurrency conformances with run-time checks in Swift 6.0 (SE-0423), and InferIsolatedConformances is a future-mode feature.