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

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

Open
stephencelis opened this issue Apr 3, 2024 · 3 comments
Open

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

stephencelis opened this issue Apr 3, 2024 · 3 comments
Assignees
Labels
availability The @available attribute and availability checking in general bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. key paths Feature: key paths (both native and Objective-C) type checker Area → compiler: Semantic analysis

Comments

@stephencelis
Copy link
Contributor

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

@stephencelis stephencelis added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Apr 3, 2024
@xedin
Copy link
Member

xedin commented Apr 3, 2024

Sounds reasonable to me, we do this for declarations and conformance in the solver already but not in key path context. Shouldn't be hard to fix either.

@xedin xedin added type checker Area → compiler: Semantic analysis key paths Feature: key paths (both native and Objective-C) availability The @available attribute and availability checking in general and removed triage needed This issue needs more specific labels labels Apr 3, 2024
@stephencelis
Copy link
Contributor Author

stephencelis commented Apr 3, 2024

@xedin Do you think this should also propagate availability (warnings/errors) when forming such key paths in contexts that require writability?

@stephencelis
Copy link
Contributor Author

/cc @tshortli

@tshortli tshortli self-assigned this Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
availability The @available attribute and availability checking in general bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. key paths Feature: key paths (both native and Objective-C) type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants