-
Notifications
You must be signed in to change notification settings - Fork 439
Add test cases for parsing closures inside if
statement conditions
#2104
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
@swift-ci Please test |
Add a bunch of test cases that verify that SwiftParser accepts closures inside `if` conditions if and only if the C++ parser does. The only difference is that the C++ parser emits a warning in cases like `if test { $0 } {}`: `Trailing closure in this context is confusable with the body of the statement; pass as a parenthesized argument to silence this warning`. We currently don’t have any infrastructure to emit warnings from SwiftParser and I’m not sure if this warning is really necessary. NFC on whether we need it. While writing the test cases, I found an issue in `BasicFormat` that adds a newline after the opening `{` of the closure inside these conditions, which causes the closure to get parsed as the statement’s body. Fixed that as well. Fixes swiftlang#795 rdar://99948919
8ba671d
to
3a5647b
Compare
@swift-ci Please test |
@@ -607,3 +627,17 @@ fileprivate extension TokenSyntax { | |||
} | |||
} | |||
} | |||
|
|||
fileprivate extension SyntaxProtocol { | |||
/// Returns this node or the first ancestor that satisfies `condition`. |
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.
We have in SwiftParserDiagnostics and here now. Worth just adding in general?
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 don’t feel happy enough with this API to make it public yet. So I prefer to just have two copies of it for now.
|
||
func testTrailingClosureInGuard() { | ||
assertParse( | ||
"guard test 1️⃣{ $0 } 2️⃣else {}", |
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.
We differ for guard
?
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, yes we do. the trailing closure is not really ambiguous in guard
statements so we … disallow it there. 🤷🏽
I’m happy to open a conversation about this behavior independently of this issue.
Add a bunch of test cases that verify that SwiftParser accepts closures inside
if
conditions if and only if the C++ parser does.The only difference is that the C++ parser emits a warning in cases like
if test { $0 } {}
:Trailing closure in this context is confusable with the body of the statement; pass as a parenthesized argument to silence this warning
. We currently don’t have any infrastructure to emit warnings from SwiftParser and I’m not sure if this warning is really necessary. NFC on whether we need it.While writing the test cases, I found an issue in
BasicFormat
that adds a newline after the opening{
of the closure inside these conditions, which causes the closure to get parsed as the statement’s body. Fixed that as well.Fixes #795
rdar://99948919