Nonisolated synchronous actor initializers

I recently ran into this warning when adding the keyword nonisolated to a synchronous actor initializer:

'nonisolated' on an actor's synchronous initializer is invalid; this is an error in Swift 6

I tried looking into it but did not find explicit discussion about this restriction. I did read some of SE-0327, where the discussion about implicitly suspending when self is fully initialized seemed to me to be possibly relevant to the restriction on having a nonisolated initializer.

The reason I wanted to put nonisolated on the initializer is because I wanted the ability to create one of these actors synchronously in any context (global constants, default values for struct properties, etc.) and I believed that logically there would be no problem with creating an actor and receiving a reference to it synchronously, as long as I do not use self in the initializer after it is fully formed.

Just to make it concrete, here's an example of what I'm imagining:

My actor has one property, which is an Int. I create one of these actors, passing in the initial value for the Int. The protected state and the actor are created "on the spot" (synchronously), but nothing is accessed - only the reference to the actor is returned. The reference to the actor may now be used to access and modify the state, subject to all of the usual actor rules.

Is what I'm imagining logically compatible with complete data race safety? Can someone help me understand why I can't mark the initializer nonisolated?

4 Likes