Property wrapper seems to have a problem if its wrappedValue is marked with global actor.
When a property wrapper's wrappedValue is @MainActor , a struct whole itself which has a property using the property wrapper becomes @MainActor. This behavior looks incorrect.
Minimal reproduction code below.
Steps to reproduce
@propertyWrapper struct Wrap<T> {
init(wrappedValue: T) {
self.wrappedValue = wrappedValue
}
@MainActor var wrappedValue: T
}
struct Foo {
@Wrap var a: Int = 1
}
Foo() // Error
The error is:
error: call to main actor-isolated initializer 'init()' in a synchronous nonisolated context
note: calls to initializer 'init()' from outside of its actor context are implicitly asynchronous
Other cases
var b: Wrap<Int> = .init(wrappedValue: 1) instead of @Wrap var a: Int = 1
Compilation succeeds
remove @MainActor from wrappedValue
Compilation succeeds
How I met this problem
I met this problem when using SwiftUI. Reproduction code:
class ViewModel: ObservableObject {
var value: Int = 1
}
struct Foo: View, Equatable {
@ObservedObject var vm: ViewModel
nonisolated static func == (lhs: Foo, rhs: Foo) -> Bool {
lhs.vm.value == rhs.vm.value // Main actor-isolated property 'vm' can not be referenced from a non-isolated context
}
var body: some View {
EmptyView()
}
}
Expected behavior
Actor of wrappedValue does not affect to actor of type which owns the property wrapper as a property.
Description
Property wrapper seems to have a problem if its
wrappedValue
is marked with global actor.When a property wrapper's
wrappedValue
is@MainActor
, a struct whole itself which has a property using the property wrapper becomes@MainActor
. This behavior looks incorrect.Minimal reproduction code below.
Steps to reproduce
The error is:
Other cases
var b: Wrap<Int> = .init(wrappedValue: 1)
instead of@Wrap var a: Int = 1
@MainActor
fromwrappedValue
How I met this problem
I met this problem when using SwiftUI. Reproduction code:
Expected behavior
Actor of
wrappedValue
does not affect to actor of type which owns the property wrapper as a property.Environment
Thanks @omochi for supporting investigation.