Skip to content

nonisolated GAIT initializers should behave more like synchronous actor initializers #83478

@jamieQ

Description

@jamieQ

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.concurrencyFeature: umbrella label for concurrency language features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions