Skip to content

[CSDiagnostics] Improving the fix-it for defining computed properties #23500

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 6 commits into from
Mar 25, 2019
Merged

[CSDiagnostics] Improving the fix-it for defining computed properties #23500

merged 6 commits into from
Mar 25, 2019

Conversation

theblixguy
Copy link
Collaborator

@theblixguy theblixguy commented Mar 22, 2019

This PR offers an improvement when the user is trying to define a computed property that is initialised with a closure. For example:

class Foo {
  var something: Int = { return 0 }
}

We currently emit an error diagnostic for the above, with a fix-it to add ():

Function produces expected type 'Int'; did you mean to call it with '()'?

In case of extensions, we emit:

Extensions must not contain stored properties

With this change, we will offer another fix-it to convert it into a computed property, by removing the = and if the property is immutable, then also changing the let to a var.

Resolves SR-9267.

@theblixguy
Copy link
Collaborator Author

cc @xedin

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

LGTM! I only wonder though if problems like this are actually diagnosed via fixes or are they still failing through to CSDiag?

@theblixguy
Copy link
Collaborator Author

theblixguy commented Mar 23, 2019

@xedin For this specific case, it does go through the fix, but there are other cases where it doesn't go through the fix. I was running the tests and got a couple of failures, so I need to figure out a way to create a fix for all the cases, or leave the code in CSDiag so it can catch those other cases.

@xedin
Copy link
Contributor

xedin commented Mar 23, 2019

@theblixguy I think it would be great to factor this logic into a separate diagnostic which could be used from both places if possible...

@theblixguy
Copy link
Collaborator Author

@xedin I have extracted the fix-it code into a separate method in ContextualFailure. Is that what you meant?

@xedin
Copy link
Contributor

xedin commented Mar 25, 2019

@swift-ci please smoke test

@theblixguy
Copy link
Collaborator Author

Build failure on OSX looks unrelated to me.

20:48:07 Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "kind", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: "kind", intValue: nil) ("kind").", underlyingError: nil)): file /Users/buildnode/jenkins/workspace/swift-PR-osx-smoke-test/branch-master/swiftpm/Sources/SPMLLBuild/llbuild.swift, line 186

@xedin
Copy link
Contributor

xedin commented Mar 25, 2019

@swift-ci please smoke test macOS platform

@theblixguy
Copy link
Collaborator Author

theblixguy commented Mar 25, 2019

Same error in SwiftPM. @aciidb0mb3r could you take a look? SPMLLBuild is crashing due to a force-unwrap here: https://github.com/apple/swift-package-manager/blob/master/Sources/SPMLLBuild/llbuild.swift#L186

20:48:07 Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "kind", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: "kind", intValue: nil) ("kind").", underlyingError: nil)): file /Users/buildnode/jenkins/workspace/swift-PR-osx-smoke-test/branch-master/swiftpm/Sources/SPMLLBuild/llbuild.swift, line 186

@aciidgh
Copy link
Contributor

aciidgh commented Mar 25, 2019

Should be fixed now, try rerunning the CI

@theblixguy
Copy link
Collaborator Author

theblixguy commented Mar 25, 2019

I don't have the permission to run the CI, could you run the smoke test on macOS? It was the one that failed.

@aciidgh
Copy link
Contributor

aciidgh commented Mar 25, 2019

@swift-ci please smoke test macOS platform

1 similar comment
@aciidgh
Copy link
Contributor

aciidgh commented Mar 25, 2019

@swift-ci please smoke test macOS platform

@theblixguy
Copy link
Collaborator Author

Thanks, all tests are passing now 🎉

@xedin xedin merged commit 1ee66cd into swiftlang:master Mar 25, 2019
@theblixguy
Copy link
Collaborator Author

@xedin Can I cherry pick this to the 5.1 branch?

@xedin
Copy link
Contributor

xedin commented Mar 27, 2019

@theblixguy please do!

@theblixguy theblixguy changed the title [CSDiag] Improving the fix-it for defining computed variables [CSDiag] Improving the fix-it for defining computed properties Mar 27, 2019
@theblixguy theblixguy changed the title [CSDiag] Improving the fix-it for defining computed properties [CSDiagnostics] Improving the fix-it for defining computed properties Mar 27, 2019
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