Skip to content

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

Merged
merged 1 commit into from
Sep 1, 2023

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Aug 24, 2023

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

@ahoppen
Copy link
Member Author

ahoppen commented Aug 25, 2023

@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
@ahoppen ahoppen force-pushed the ahoppen/closure-parsing branch from 8ba671d to 3a5647b Compare August 30, 2023 23:42
@kimdv
Copy link
Contributor

kimdv commented Aug 31, 2023

@swift-ci Please test

@ahoppen ahoppen requested a review from bnbarham August 31, 2023 14:36
@@ -607,3 +627,17 @@ fileprivate extension TokenSyntax {
}
}
}

fileprivate extension SyntaxProtocol {
/// Returns this node or the first ancestor that satisfies `condition`.
Copy link
Contributor

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?

Copy link
Member Author

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 {}",
Copy link
Contributor

Choose a reason for hiding this comment

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

We differ for guard?

Copy link
Member Author

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.

@ahoppen ahoppen merged commit fe6ce4e into swiftlang:main Sep 1, 2023
@ahoppen ahoppen deleted the ahoppen/closure-parsing branch September 1, 2023 16:13
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.

SwiftParser allows closures in condition lists where the compiler doesn't
3 participants