-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[MiscDiagnostics] Emit a deprecation warning for some writes through … #17350
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
[MiscDiagnostics] Emit a deprecation warning for some writes through … #17350
Conversation
@swift-ci Please smoke test |
@@ -570,6 +571,7 @@ struct VisibilityTesting { | |||
struct VisibilityTesting2 { | |||
func inFilePrivateContext() { | |||
var xRef = \VisibilityTesting.x | |||
// expected-warning@-1 {{forming a writable keypath to property 'x' that is read-only in this context is deprecated and will be removed in a future release}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How are you supposed to fix this one to be read-only, though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can make it read-only with an explicit type, but in retrospect this warning should really be tracking the keypaths that would be formed as read-only in -swift-version 5
that are used in key path applications that write.
I'll take a look at updating the PR to do that instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Swift 5 mode, this infers as a read-only KeyPath
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that’s correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not diagnose both? You'll probably need to build a cache of incorrectly-inferred declrefs to catch stuff like this
class Foo {
private(set) var foo: String = ""
init() {}
}
let f = Foo();
let kp = \Foo.foo // forming a writable keypath to property...
f[keyPath: kp] = "asdf" // write to writeable key path that is read-only in this context is deprecated...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unhappy with giving people a warning on code that doesn't do anything wrong.
let f = Foo()
let kp = \Foo.foo // happens to be writable...
print(f[keyPath: kp]) // ...but I only use it to read
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CodaFi Detecting the write in your example is what I meant when I said "...this warning should really be tracking the keypaths that would be formed as read-only in -swift-version 5 that are used in key path applications that write.".
As Jordan points out, we don't want to warn about things that we happen to infer today as writable but are never used that way.
Anything we do in Sema is going to be necessarily approximate because doing this properly requires data flow analysis.
I'm not sure how much value we'll actually get from doing something approximate, either. I don't want to end up with any false positives, and the only way to do that is to clear out the cache of values we believe hold writable keypaths that would have been inferred as readable any time we exit a control structure.
This may ultimately not be worth doing.
…literal keypaths. We incorrectly allowed some keypaths to be inferred as writable keypaths in Swift 3/4 modes. This no longer happens when -swift-version 5 is specified. This warning is a limited attempt at providing some advanced notice of code that will break, only in the cases where the keypath is a direct argument to a keypath subscript write. Fixes: rdar://problem/40068274
I ended up changing this around so that we will now emit a warning on key path subscript writes with literal key paths that would be inferred to be read-only under -swift-version 5. |
@swift-ci Please smoke test |
@swift-ci Please test source compatibility |
I grabbed the logs from the source compatibility run to ensure that this warning is not firing spuriously anywhere. Everything looks good on that front. |
…literal keypaths.
We incorrectly allowed some keypaths to be inferred as writable
keypaths in Swift 3/4 modes. This no longer happens when
-swift-version 5 is specified.
This warning is a limited attempt at providing some advanced notice of
code that will break, only in the cases where the keypath is a direct
argument to a keypath subscript write.
Fixes: rdar://problem/40068274