-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Description
per SE-327:
A non-isolated initializer of a global-actor isolated type (GAIT) is in the same situation as a non-async actor initializer, in that it must bootstrap the instance without the executor's protection. ... To solve this race, we propose to apply flow-sensitive actor isolation to the initializers of GAITs that are marked as non-isolated.
however, nonisolated GAIT inits seem to error out during typechecking and don't make it to flow isolation in some cases where synchronous actor initializers do not. specifically, you cannot seem to initialize stored properties of non-sendable type in the GAIT case.
Reproduction
open class NS {}
@MainActor
final class GAIT {
let ns: NS
nonisolated init() {
self.ns = NS() // 🛑
}
}
/*
<source>:8:14: error: main actor-isolated property 'ns' can not be mutated from a nonisolated context [actor_isolated_non_self_reference]
3 | @MainActor
4 | final class GAIT {
5 | let ns: NS
| `- note: mutation of this property is only permitted within the actor [actor_mutable_state]
6 |
7 | nonisolated init() {
8 | self.ns = NS()
| `- error: main actor-isolated property 'ns' can not be mutated from a nonisolated context [actor_isolated_non_self_reference]
9 | }
10 | }
Compiler returned: 1
*/
Expected behavior
the behavior should generally match how synchronous actor initializers work:
actor A {
let ns: NS
init() {
self.ns = NS() // ✅
}
}
Environment
Swift version 6.2-dev (LLVM ca4ccf35b901178, Swift 5754397)
Target: x86_64-unknown-linux-gnu
compiled using -swift-version 6
(but seems to affect other configurations as well)
Additional information
No response