swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.49k stars 10.35k forks source link

[SR-16052] Compiler error when referencing global actor isolated value from non-isolated initializer with property wrapper #58313

Open swift-ci opened 2 years ago

swift-ci commented 2 years ago
Previous ID SR-16052
Radar None
Original Reporter StarLard (JIRA User)
Type Bug

Attachment: Download

Environment Xcode 13.3 Swift 5.6
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler, swift | |Labels | Bug, Concurrency, PropertyWrappers | |Assignee | None | |Priority | Medium | md5: f522783fd58bc1635562f6ecf2e3eca5

Issue Description:

Compiler produces an actor isolation violation error when referencing a wrapped property from a global actor isolated class' init or deinit methods. Using the long-form declaration of the property wrapper or forgoing the property wrapper does not trigger the violation. Using an actor type instead of a global actor isolated class also does not reproduce this error.

Error Reproduction Example:

@propertyWrapper struct Wrapped<Value> {
    var wrappedValue: Value

    init(wrappedValue: Value) {
        self.wrappedValue = wrappedValue
    }
}

@MainActor
class Example {
    var notWrappedValue: Bool = true

    @Wrapped
    var wrappedValue: Bool = true

    var wrappedValueLong: Wrapped<Bool> = Wrapped(wrappedValue: true)

    nonisolated init() {
        _ = self.notWrappedValue // Ok
        _ = self.wrappedValueLong // Ok
        _ = self.wrappedValue // Error: Property 'value' isolated to global actor 'MainActor' can not be referenced from a non-isolated synchronous context
    }

    deinit {
        _ = self.notWrappedValue // Ok
        _ = self.wrappedValueLong // Ok
        _ = self.wrappedValue // Error: Property 'value' isolated to global actor 'MainActor' can not be referenced from this synchronous context
    }
}
StarLard commented 1 year ago

FYI I reported this.