Unexpected error with NonisolatedNonsendingByDefault

This code produces an error, as expected:

open class NotSendable {
    var value = ""

    func runAsync() async {
        for i in 0... {
            value = "\(i)"
        }
    }
}

@MainActor
class C {
    let x = NotSendable()

    func f() async {
        await x.runAsync()
        //      |- error: sending 'self.x' risks causing data races
        //      `- note: sending main actor-isolated 'self.x' to nonisolated
        //               instance method 'runAsync()' risks causing data races
        //               between nonisolated and main actor-isolated uses
    }
}

await C().f()

I thought that with SE-0461's -enable-upcoming-feature NonisolatedNonsendingByDefault, this error should go away, because runAsync will be run on the main actor, without sending x. However, I still get the error. Is that expected? If it is, maybe the error should be reworded, since the function is nonisolated(nonsending), it's strange to get an error about sending when calling it. Or is this just a bug in the current implementation, the code safe, and the error spurious? (@hborla)

seems like it maybe works on the release branch (this commit) – how are you building it?

That definitely looks like a bug to me. You're right that with NonisolatedNonsendingByDefault, the call to runAsync will run on the main actor and not actually send self.x anywhere.

Aaah sorry folks, this was PEBKAC — I had the wrong Xcode selected. It does work with Xcode 26 beta 1.

The older swiftc silently accepts the -enable-upcoming-feature NonisolatedNonsendingByDefault argument, so I didn't get any "negative feedback" that I'd done the wrong thing.

3 Likes