How do SwiftUI nonisolated types use MainActor isolated ones in a synchronous nonisolated context?

I have a custom SwiftUI.Transition.

struct Foo: Transition { ... } // inherits MainActor isolation from Transition.

And there is type-erased nonisolated AnyTransition that has all built-in transitions as static members (AnyTransition | Apple Developer Documentation). If I want to follow the pattern I hit a compiler error.

extension AnyTransition {
    // Yields "Call to main actor-isolated initializer 'init(_:)'
    // in a synchronous nonisolated context"
    static var foo: AnyTransition { AnyTransition(Foo()) }
}

Neither AnyTransition extension nor the static members seem to be annotated with @MainActor while the actual built-in types seem to have main actor isolated initializers. So how are they able to return synchronously?

The only solution I can think of is that they are assuming an actor's execution context.

extension AnyTransition {

    static var foo: AnyTransition {
        .init(MainActor.assumeIsolated({ Foo() }))
    }
}

Is that it? Is it safe? I have encountered some crashes (unrelated to this) under com.apple.SwiftUI.AsyncRenderer domain recently so I'm not sure about such assumption.

Weird, I can't reproduce this issue. The following code compiles fine for me:

import SwiftUI

struct Foo: Transition {  
    func body(content: Content, phase: TransitionPhase) -> some View {
        content
    }
}

extension AnyTransition {
    static var foo: AnyTransition {
        AnyTransition(Foo())
    }
}

On Xcode 16.1.

No. This will crash when called from outside the main actor.

Huh. I've just updated to 16.1 and now it compiles :tada:.

The error is still present when calling static member in generic context though.

extension Transition where Self == Foo {

    static var foo: Foo { .init() }
}

extension AnyTransition {

    // Yields "Main actor-isolated static property 'foo' can not be referenced
    // from a nonisolated context".
    static var foo: AnyTransition { AnyTransition(.foo) }
}

So I guess this will be resolved in upcoming Swift releases, or, at least I don't need to be intrigued about SwiftUI's magic.

In that case, the issue is static var foo: Foo inherits main actor isolation because it's defined in an extension of Transition (not Foo), which is Main Actor isolated.

However, since you don't access any property of Transition, you can simply opt-out of this Main Actor isolation inheritance and declare the static property as nonisolated:

import SwiftUI

struct Foo: Transition {
    func body(content: Content, phase: TransitionPhase) -> some View {
        content
    }
}

extension Transition where Self == Foo {
    nonisolated static var foo: Foo { .init() }
}

extension AnyTransition {
    static var foo: AnyTransition {
        return AnyTransition(.foo)
    }
}

Yeah I know. Either this or isolate my AnyTransaction static member to the main actor. But SwiftUI doesn't do that. All Transition types and members are main actor isolated while AnyTransition and its static members are nonisolated. So I was just curious how they are putting it together. Just to learn more about the new concurrency model.