Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@propertyWrapper value in actor-isolated property 'home' can not be mutated from a non-isolated context #60926

Open
m1entus opened this issue Sep 2, 2022 · 6 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@m1entus
Copy link

m1entus commented Sep 2, 2022

Describe the bug
Compiler shows warnings about can not be mutated from a non-isolated context in Xcode 14.0 when assigning value when using custom @propertyWrapper .

Steps To Reproduce

public struct Home {
}

@propertyWrapper
public final class MyPropertyWrapper<Value: Sendable>: @unchecked Sendable {
    public var wrappedValue: Value

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

public actor MyActor {

    @MyPropertyWrapper
    private var home: Home

    public init(home: Home) {
        self.home = home // Actor-isolated property 'home' can not be mutated from a non-isolated context; this is an error in Swift 6
    }
}

as a workaround it is possible to use MyPropertyWrapper<Home> and then warning dissapears.

public actor MyActor {

    private var home: MyPropertyWrapper<Home>

    public init(home: Home) {
        self.home = MyPropertyWrapper<Home>(wrappedValue: home)
    }
}

Expected behavior
There should not be warning about Actor-isolated property 'home' can not be mutated from a non-isolated context when using @MyPropertyWrapper
Warning not appear in Xcode 13.4.0.

Screenshots
Zrzut ekranu 2022-09-2 o 15 04 14

Environment (please fill out the following information)

  • Xcode Version/Tag/Branch: Xcode 14.0.0 beta 6

Additional context
Add any other context about the problem here.

@m1entus m1entus added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Sep 2, 2022
@theblixguy
Copy link
Collaborator

Reduced:

public struct Home {}

public actor MyActor {

  private var home: Home {
    get { Home() }
    set { }
  }

  public init(home: Home) {
    self.home = home
  }
}

@m1entus
Copy link
Author

m1entus commented Sep 2, 2022

Reduced:

public struct Home {}

public actor MyActor {

  private var home: Home {
    get { Home() }
    set { }
  }

  public init(home: Home) {
    self.home = home
  }
}

???

@theblixguy
Copy link
Collaborator

theblixguy commented Sep 2, 2022

It’s a smaller version of the code that triggers the same warning. It’s nothing to do with property wrappers specifically, but with using a computed property.

@m1entus
Copy link
Author

m1entus commented Sep 2, 2022

Aaa okay make sense now - thanks for explanation.

@m1entus
Copy link
Author

m1entus commented Sep 8, 2022

Found out that this workaround works for me (instead of using computed property i use reference to propertyWrapper):

public actor MyActor {

    @MyPropertyWrapper
    private var home: Home

    public init(home: Home) {
        _home = MyPropertyWrapper<Home>(wrappedValue: home)
    }
}

@MilezHigh
Copy link

You need to make sure home is a published property

@Published var home: Home? = nil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

3 participants