-
Notifications
You must be signed in to change notification settings - Fork 439
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
Merged
kimdv
merged 1 commit into
swiftlang:main
from
kimdv:kimdv/1925-missing-parentheses-in-closure-signature-produces-bad-diagnostic
Aug 17, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1746,7 +1746,9 @@ extension Parser { | |
var effectSpecifiers: RawTypeEffectSpecifiersSyntax? | ||
var returnClause: RawReturnClauseSyntax? = nil | ||
if !self.at(.keyword(.in)) { | ||
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) { | ||
// Parse the closure arguments. | ||
let params = self.parseParameterClause(RawClosureParameterClauseSyntax.self) { parser in | ||
parser.parseClosureParameter() | ||
|
@@ -2430,52 +2432,49 @@ extension Parser.Lookahead { | |
} | ||
|
||
// Parse pattern-tuple func-signature-result? 'in'. | ||
if lookahead.consume(if: .leftParen) != nil { // Consume the ')'. | ||
|
||
if lookahead.at(.leftParen) { // Consume the '('. | ||
// While we don't have '->' or ')', eat balanced tokens. | ||
var skipProgress = LoopProgressCondition() | ||
while !lookahead.at(.endOfFile, .rightParen) && lookahead.hasProgressed(&skipProgress) { | ||
while !lookahead.at(.endOfFile, .rightBrace, .keyword(.in)) && !lookahead.at(.arrow) && lookahead.hasProgressed(&skipProgress) { | ||
Comment on lines
-2437
to
+2438
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, yes, that does make sense. |
||
lookahead.skipSingle() | ||
} | ||
|
||
// Consume the ')', if it's there. | ||
if lookahead.consume(if: .rightParen) != nil { | ||
lookahead.consumeEffectsSpecifiers() | ||
|
||
// Parse the func-signature-result, if present. | ||
if lookahead.consume(if: .arrow) != nil { | ||
guard lookahead.canParseType() else { | ||
return false | ||
} | ||
|
||
lookahead.consumeEffectsSpecifiers() | ||
} | ||
} | ||
// Okay, we have a closure signature. | ||
} else if lookahead.at(.identifier) || lookahead.at(.wildcard) { | ||
// Parse identifier (',' identifier)* | ||
lookahead.consumeAnyToken() | ||
|
||
/// If the next token is a colon, interpret is as a type annotation and consume a type after it. | ||
/// While type annotations aren’t allowed in shorthand closure parameters, we consume them to improve recovery. | ||
func consumeOptionalTypeAnnotation() -> Bool { | ||
if lookahead.consume(if: .colon) != nil { | ||
return lookahead.canParseType() | ||
} else { | ||
return true | ||
} | ||
} | ||
|
||
var parametersProgress = LoopProgressCondition() | ||
while lookahead.consume(if: .comma) != nil && lookahead.hasProgressed(¶metersProgress) { | ||
while consumeOptionalTypeAnnotation() && lookahead.consume(if: .comma) != nil && lookahead.hasProgressed(¶metersProgress) { | ||
if lookahead.at(.identifier) || lookahead.at(.wildcard) { | ||
lookahead.consumeAnyToken() | ||
continue | ||
} | ||
|
||
return false | ||
} | ||
} | ||
|
||
lookahead.consumeEffectsSpecifiers() | ||
// Consume the ')', if it's there. | ||
lookahead.consume(if: .rightParen) | ||
|
||
// Parse the func-signature-result, if present. | ||
if lookahead.consume(if: .arrow) != nil { | ||
guard lookahead.canParseType() else { | ||
return false | ||
} | ||
lookahead.consumeEffectsSpecifiers() | ||
|
||
lookahead.consumeEffectsSpecifiers() | ||
// Parse the func-signature-result, if present. | ||
if lookahead.consume(if: .arrow) != nil { | ||
guard lookahead.canParseType() else { | ||
return false | ||
} | ||
|
||
lookahead.consumeEffectsSpecifiers() | ||
} | ||
|
||
// Parse the 'in' at the end. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 added this test and it fails because we are peeking here.
When at this place current token is
comparisonPredicate
.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
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.
@ahoppen 😬
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.
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 doingskipSingle
atcomparisonPredicate
. If you make the following change, then we’re callingskipSingle
on the opening(
, which consumes all the contents until the matching)
before thein
, including the->
, which fixes the issue.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.
Oh okay nice!
Thanks a lot.
And no worries! Take the time to reply when you can 😁