Skip to content

[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

Merged
merged 2 commits into from
Jun 26, 2018
Merged

[MiscDiagnostics] Emit a deprecation warning for some writes through … #17350

merged 2 commits into from
Jun 26, 2018

Conversation

rudkx
Copy link
Contributor

@rudkx rudkx commented Jun 20, 2018

…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

@rudkx
Copy link
Contributor Author

rudkx commented Jun 20, 2018

@swift-ci Please smoke test

@rudkx rudkx requested a review from CodaFi June 20, 2018 02:41
@@ -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}}
Copy link
Contributor

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?

Copy link
Contributor Author

@rudkx rudkx Jun 20, 2018

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.

Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that’s correct.

Copy link
Contributor

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...

Copy link
Contributor

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

Copy link
Contributor Author

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.

@rudkx rudkx requested a review from jckarter June 20, 2018 03:20
rudkx added 2 commits June 25, 2018 23:03
…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
@rudkx
Copy link
Contributor Author

rudkx commented Jun 26, 2018

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.

@rudkx
Copy link
Contributor Author

rudkx commented Jun 26, 2018

@swift-ci Please smoke test

@rudkx
Copy link
Contributor Author

rudkx commented Jun 26, 2018

@swift-ci Please test source compatibility

@rudkx rudkx changed the title Add a warning for forming writable key paths when disallowed. [MiscDiagnostics] Emit a deprecation warning for some writes through … Jun 26, 2018
@rudkx
Copy link
Contributor Author

rudkx commented Jun 26, 2018

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.

@rudkx rudkx merged commit 059d5d2 into swiftlang:master Jun 26, 2018
@rudkx rudkx deleted the diagnose-deprecated-writable-keypath branch June 26, 2018 23:25
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.

4 participants