Skip to content

Compilation Error: "Setter for 'property' is unavailable" when trying to modify property of a class type wrapped in a property wrapper #74283

@CrazyFanFan

Description

@CrazyFanFan

Description

I'm encountering a compilation error when trying to modify the property of a class that's been wrapped using a custom property wrapper. The error states Setter for 'property' is unavailable, which is unexpected because the property foo is a mutable property of a class (FooValue). This seems to suggest an issue with how the setter is being synthesized (or not synthesized) for the wrapped property.

Reproduction

import Foundation

class FooValue {
    var foo: String = ""
}

@propertyWrapper
public struct Wrapper<Value> {
    private let value: Value

    public init(wrappedValue: Value) {
        value = wrappedValue
    }

    @available(*, unavailable, message: "@Published is only available on properties of classes")
    public var wrappedValue: Value {
        get { fatalError() }
        set { fatalError() }
    }

    public static subscript<EnclosingSelf>(
    _enclosingInstance object: EnclosingSelf,
    wrapped wrappedKeyPath: Swift.ReferenceWritableKeyPath<EnclosingSelf, Value>,
    storage storageKeyPath: Swift.ReferenceWritableKeyPath<EnclosingSelf, Wrapper<Value>>
    ) -> Value where EnclosingSelf : AnyObject {
        get { object[keyPath: storageKeyPath].value }

        @available(*, unavailable)
        set { }
    }
}

class MyClass {
    @Wrapper
    var property: FooValue = FooValue()
}

let outer = MyClass()
outer.property.foo = ""

Expected behavior

The property foo of the FooValue class should be mutable, and modifications to it via an instance of MyClass should be allowed, considering FooValue is a class with a mutable foo property.

Environment

swift-driver version: 1.109.2 Apple Swift version 6.0 (swiftlang-6.0.0.3.300 clang-1600.0.20.10)
Target: arm64-apple-macosx14.0

Additional information

No response

rdar://132309786

Metadata

Metadata

Assignees

No one assigned

    Labels

    availabilityThe @available attribute and availability checking in generalbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.property wrappersFeature: property wrappers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions