Skip to content

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Sep 24, 2025

…ation

If key path literal is converted to a read-only type, let's not use maximum mutability to avoid unnecessary conversions. This is also important because accessor references are availability checked and we need to avoid referencing something that is not actually going to be used.

There are some edge-cases in this approach which would still produce a conversions, i.e.:

struct S {
    var a: Int
    let b: Int
}

func test<T>(_: T, _: T) {}

test(\S.a, \S.b)

Here \S.a is going to be converted to KeyPath<S, Int>. Availability checker would still have to recognize situations like that and skip checking setters if they are not used.

…ation

If key path literal is converted to a read-only type, let's not
use maximum mutability to avoid unnecessary conversions. This is
also important because accessor references are availability checked
and we need to avoid referencing something that is not actually
going to be used.

There are some edge-cases in this approach which would still
produce a conversions, i.e.:

```
struct S {
    var a: Int
    let b: Int
}

func test<T>(_: T, _: T) {}

test(\S.a, \S.b)
```

Here `\S.a` is going to be converted to `KeyPath<S, Int>`. Availability
checker would still have to recognize situations like that and skip
checking setters if they are not used.
@xedin
Copy link
Contributor Author

xedin commented Sep 24, 2025

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented Sep 24, 2025

@swift-ci please test Windows platform

tshortli added a commit to tshortli/swift that referenced this pull request Sep 25, 2025
…king.

Accessor availability diagnostics for key path expressions were first
introduced by swiftlang#83931. Those changes were
insufficient because sometimes key path expressions are generated in the AST
with more mutability than is needed by the context and so setter availability
could be diagnosed inappropriately. Key path expression binding was refined in
swiftlang#84491 to make this less likely to
occur. However, there are still some circumstances in which a mutable key path
is generated in the AST and then immediately coerced into an immutable key path
to satisfy the contextual type. This change infers the immutability of these key
path expressions by looking through surrounding conversion expressions.
@tshortli
Copy link
Contributor

@swift-ci please test Windows platform

tshortli added a commit to tshortli/swift that referenced this pull request Sep 25, 2025
…king.

Accessor availability diagnostics for key path expressions were first
introduced by swiftlang#83931. Those changes were
insufficient because sometimes key path expressions are generated in the AST
with more mutability than is needed by the context and so setter availability
could be diagnosed inappropriately. Key path expression binding was refined in
swiftlang#84491 to make this less likely to
occur. However, there are still some circumstances in which a mutable key path
is generated in the AST and then immediately coerced into an immutable key path
to satisfy the contextual type. This change infers the immutability of these key
path expressions by looking through surrounding conversion expressions.
@xedin xedin merged commit a721d10 into swiftlang:main Sep 25, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants