SE-0461 Run nonisolated async functions on the caller's actor by default

I'm running the function test() below with the swift compiler shipped with Xcode 26.0 beta (17A5241e):

    nonisolated
    func onMainActor() async throws {
        MainActor.shared.preconditionIsolated()
    }

    @MainActor
    func test() async throws {
        try await onMainActor()
    }

The code fails at the precondition.

This is from a Swift Package library, whose tools version is swift-tools-version: 6.2 and it also has the feature flag enabled, Package.swift:

    targets: [
        .target(
            name: "MyLibrary",
            swiftSettings: [
                .enableUpcomingFeature("NonisolatedNonsendingByDefault")
            ]
        ),
    ...

If I understood it correctly, according SE-0461 function onMainActor() should execute on the MainActor.

What I'm missing here?

Compiler version:

swift-driver version: 1.127.4.2 Apple Swift version 6.2 (swiftlang-6.2.0.9.909 clang-1700.3.9.907)
Target: arm64-apple-macosx15.0

Test:

Summary
import Testing

struct IsolationTests {
        
    nonisolated
    func onMainActor() async throws {
        MainActor.shared.preconditionIsolated()
    }

    @Test
    @MainActor
    func testNonisolatedAsyncFunctionRunsOnCallersIsolationByDefault2() async throws {
        MainActor.shared.preconditionIsolated()
        try await onMainActor()
        MainActor.shared.preconditionIsolated()
    }
}

are you running the tests in another target? if so, did you try adding the flag to that target as well?

2 Likes

Thank Jamie for hint!
I actually forgot do add the feature flag to the test target :grimacing:;)

So, now when adding it, it executes on the expected isolation.

1 Like