Skip to content

Add correct diagnostic for missing parentheses #2012

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

Conversation

kimdv
Copy link
Contributor

@kimdv kimdv commented Aug 6, 2023

Resolves #1925

@kimdv kimdv requested a review from ahoppen as a code owner August 6, 2023 14:52
@kimdv kimdv force-pushed the kimdv/1925-missing-parentheses-in-closure-signature-produces-bad-diagnostic branch 3 times, most recently from bd5f7d6 to 9de0862 Compare August 7, 2023 07:12
@kimdv kimdv force-pushed the kimdv/1925-missing-parentheses-in-closure-signature-produces-bad-diagnostic branch 2 times, most recently from 0fe61be to 8356e1c Compare August 8, 2023 10:46
@kimdv kimdv force-pushed the kimdv/1925-missing-parentheses-in-closure-signature-produces-bad-diagnostic branch from 8356e1c to c54b023 Compare August 10, 2023 06:24
@kimdv
Copy link
Contributor Author

kimdv commented Aug 10, 2023

@swift-ci please test

@kimdv kimdv force-pushed the kimdv/1925-missing-parentheses-in-closure-signature-produces-bad-diagnostic branch from c54b023 to b9a23a9 Compare August 10, 2023 19:53
if self.at(.leftParen) {
// If the next token is ':', then it looks like the code contained a non-shorthand closure parameter with a type annotation.
// These need to be wrapped in parentheses.
if self.at(.leftParen) || self.peek(isAt: .colon) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added this test and it fails because we are peeking here.
When at this place current token is comparisonPredicate.

    assertParse(
      """
      withInvalidOrderings { (comparisonPredicate: @escaping (Int, Int) -> Bool) in
      }
      """
    )

So the peek seems not the correct solutions here.
Maybe we should do something like self.peek(isAt: .colon) && previousToken != .leftParen

What do you think @ahoppen

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ahoppen 😬

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, it’s been a busy week and I missed the comment. Thanks for the ping.

The issue happens because in canPareClosureSignature, we now stop at the arrow. Since we start doing skipSingle at comparisonPredicate. If you make the following change, then we’re calling skipSingle on the opening (, which consumes all the contents until the matching ) before the in, including the ->, which fixes the issue.

- if lookahead.consume(if: .leftParen) != nil {  // Consume the ')'.
+ if lookahead.at(.leftParen) {  // Consume the '('.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh okay nice!
Thanks a lot.

And no worries! Take the time to reply when you can 😁

@kimdv kimdv force-pushed the kimdv/1925-missing-parentheses-in-closure-signature-produces-bad-diagnostic branch from b9a23a9 to a911e90 Compare August 16, 2023 06:18
@kimdv
Copy link
Contributor Author

kimdv commented Aug 16, 2023

@swift-ci please test

@kimdv kimdv enabled auto-merge August 16, 2023 06:19
@kimdv
Copy link
Contributor Author

kimdv commented Aug 16, 2023

@swift-ci please test macOS

@kimdv
Copy link
Contributor Author

kimdv commented Aug 16, 2023

@ahoppen the job fails here: https://ci.swift.org/job/swift-syntax-PR-macOS/3177/console

Not sure why.
When I add the tests in the test suite they succeeds correctly.

Edit:
committed and added them now

@kimdv kimdv force-pushed the kimdv/1925-missing-parentheses-in-closure-signature-produces-bad-diagnostic branch from a911e90 to 56548c7 Compare August 16, 2023 07:55
@kimdv
Copy link
Contributor Author

kimdv commented Aug 16, 2023

@swift-ci please test

@ahoppen
Copy link
Member

ahoppen commented Aug 16, 2023

It’s failing at the following test case. The in in the for statement is critical to reproduce the failure. Could you look into why it’s failing? If you can’t find it, I can try debugging as well.

state.withCriticalRegion { (1 + 2) }
for action in tracking {
  action()
}

And just to explain how I got to this: I clones the compiler repo and ran

swift-parser-cli print-diags stdlib/public/Observation/Sources/Observation/ObservationRegistrar.swift

Then I kept removing code from that file while still reproducing the failure until I ended up with the reduced example.

@kimdv kimdv force-pushed the kimdv/1925-missing-parentheses-in-closure-signature-produces-bad-diagnostic branch from 56548c7 to 7fb3e7f Compare August 17, 2023 16:06
@kimdv
Copy link
Contributor Author

kimdv commented Aug 17, 2023

@ahoppen found it.
We had inside canParseClosureSignature this !lookahead.at(.endOfFile, .rightParen, .keyword(.in)) and it didn't stop before hitting the in keyword.

Changed rightParen -> rightBrace

@kimdv
Copy link
Contributor Author

kimdv commented Aug 17, 2023

@swift-ci please test

@ahoppen
Copy link
Member

ahoppen commented Aug 17, 2023

@swift-ci Please test Windows

Comment on lines -2437 to +2438
while !lookahead.at(.endOfFile, .rightParen) && lookahead.hasProgressed(&skipProgress) {
while !lookahead.at(.endOfFile, .rightBrace, .keyword(.in)) && !lookahead.at(.arrow) && lookahead.hasProgressed(&skipProgress) {
Copy link
Member

Choose a reason for hiding this comment

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

Oh, yes, that does make sense. skipSingle is looking for the rightParen now that we previously consumed manually in lookahead.consume(if: .leftParen).

@kimdv kimdv merged commit b05dfb6 into swiftlang:main Aug 17, 2023
@kimdv kimdv deleted the kimdv/1925-missing-parentheses-in-closure-signature-produces-bad-diagnostic branch August 17, 2023 18:37
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.

Missing parentheses in closure signature produces bad diagnostic
2 participants