-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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 sendability warning should be suppressed #68943
Comments
|
Or scratch that. Updating to Swift 6 language mode fixes. I guess building for <6 still preserves the old warnings? |
|
Yeah, |
|
@xedin Thanks for confirming! I'll close this now unless you think there's anything outstanding. |
|
Nothing outstanding, thanks! |
|
@xedin Just came across this key path sendability error in Swift 6: @MainActor
class Foo: NSObject {
var bar = 0
var observation: NSKeyValueObservation?
func f<Value>(_ keyPath: ReferenceWritableKeyPath<Foo, Value>) {
observation = observe(keyPath) { foo, _ in
print(foo[keyPath: keyPath]) // 🛑
}
}
func g() {
f(\.bar)
}
}
Is this an incorrect diagnosis? I can add @MainActor
class Foo: NSObject {
var bar = 0
var observation: NSKeyValueObservation?
func f<Value>(_ keyPath: ReferenceWritableKeyPath<Foo, Value> & Sendable) {
observation = observe(keyPath) { foo, _ in
print(foo[keyPath: keyPath]) // ✅
}
}
func g() {
f(\.bar) // 🛑
}
}
In this case it seems like it should infer the Is there another way to write the above? |
|
This is correct because isolation makes key paths non-Sendable. |
|
@xedin Ah, so this specifically because of the |
|
Indeed, key path is non-Sendable because all of the isolation information is erased effectively which means that it’s not safe to use it anywhere but where it was created. |
Description
The sendability story for key paths is not complete right now, and so users can encounter sendability warnings for them when turning up concurrency warnings in their project. Ideally the warnings a user encounters should highlight problems that can be addressed in their code bases, and not unsolved problems in the language. And yet, key paths emit these warnings nonetheless, and these warnings will persist and cannot be suppressed short of turning down concurrency warnings in the entire project.
Steps to reproduce
We can see the problem by turning up concurrency warnings and compiling the following code:
(Thanks @sidepelican for this example.)
Expected behavior
I expect no warning, or for there to be a way to avoid the warning while still having concurrency warnings turned up.
Environment
The text was updated successfully, but these errors were encountered: