Skip to content

Key path formed to setter with unavailable key path is writable #72809

Open
@stephencelis

Description

@stephencelis

Description

(First posted to the forums.)

Currently, a key path formed to a property that has an unavailable setter are WritableKeyPaths. This seems like a bug, since it allows code like dynamic member lookup to quietly compile what should crash at runtime.

While tracking the availability itself in the key path seems like a good idea and would allow library users to surface their own error messaging, I imagine that is a complex project to take on. Maybe as a baby step, key paths should at the very least be simply non-writable KeyPaths when the setter is unavailable.

Reproduction

struct S {
  var p: String {
    get { "" }
    @available(*, unavailable)
    set { fatalError() }
  }
}
let kp: WritableKeyPath = \S.p

Expected behavior

The key path returned should be KeyPath, not WritableKeyPath, so the above should not compile.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Additional information

No response

Metadata

Metadata

Assignees

Labels

availabilityThe @available attribute and availability checking in generalbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.key pathsFeature: key paths (both native and Objective-C)type checkerArea → compiler: Semantic analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions