Skip to content

Add diagnostic to move 'async' and 'throws' in 'ArrowExpr' in front of '->' #881

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 5 commits into from
Oct 7, 2022

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Oct 5, 2022

After parsing the -> for an ArrowExpr, eat trailing throws or async and include them as unexpected nodes after the ->. This is safe because the expression after -> cannot start with throws or async.

This changes the syntax nodes to allow unexpected tokens after the last expected token. Previously @rintaro had concerns about unexpected nodes at the end of a node because this make it even more ambiguous where the unexpected tokens should be placed, but it’s super convenient for this diagnostic, so allowed it again.

@ahoppen ahoppen requested review from rintaro and bnbarham October 5, 2022 19:03
@rintaro
Copy link
Member

rintaro commented Oct 5, 2022

I'm fine with adding UnexpectedAfterLastChild

// unexpectedAfterArrow and inserting the corresponding effects modifier as
// missing into the ArrowExprSyntax. This reflect the semantics the user
// originally intended.
var effectModifiersAfterArrow: [RawTokenSyntax] = []
Copy link
Contributor

Choose a reason for hiding this comment

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

This only handles throws async and not eg.throws throws async. I've put up a very partial PR for function signatures (#889). Any thoughts on something like that instead? There's quite a few places we'll want to change this 😅 (if we decide this is in fact something the parser should handle).

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 know. I was thinking that we should probably allow effects specifiers in any order in the parser.

@ahoppen ahoppen force-pushed the ahoppen/throws-after-arrow-expr branch from 59d0170 to 8bf7809 Compare October 7, 2022 09:05
@ahoppen
Copy link
Member Author

ahoppen commented Oct 7, 2022

@swift-ci Please test

@ahoppen ahoppen force-pushed the ahoppen/throws-after-arrow-expr branch from 8bf7809 to 72085ee Compare October 7, 2022 09:17
@ahoppen
Copy link
Member Author

ahoppen commented Oct 7, 2022

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Oct 7, 2022

swiftlang/swift#61482

@swift-ci Please test

@ahoppen ahoppen force-pushed the ahoppen/throws-after-arrow-expr branch from 72085ee to a4088dd Compare October 7, 2022 12:22
@ahoppen
Copy link
Member Author

ahoppen commented Oct 7, 2022

swiftlang/swift#61482

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Oct 7, 2022

swiftlang/swift#61482

@swift-ci Please test macOS

@ahoppen ahoppen merged commit a6ac31b into swiftlang:main Oct 7, 2022
@ahoppen ahoppen deleted the ahoppen/throws-after-arrow-expr branch October 7, 2022 14:40
Comment on lines -52 to +37
""",
diagnostics: [
// TODO: Old parser expected error on line 5: protocol methods must not have bodies
// TODO: Old parser expected error on line 6: protocol methods must not have bodies
]
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

SwiftSyntax uses the same nodes for functions inside protocols and structs. Since protocols have optional bodies, function bodies are also optional for functions in in structs in SwiftSyntax. This has two advantages:

  • It reduces the nodes we need in SwiftSyntax (we only need a single one and not one for protocols and one for all the other types)
  • .swiftinterface files also have optional function bodies (even for functions in structs). This way, we can parse .swiftinterface files without any problems.

The idea is that the missing function body will be diagnosed by ASTGen further down in the compilation pipeline instead of by the parser.

Copy link
Contributor

Choose a reason for hiding this comment

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

I appreciate the detailed clarification!

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.

4 participants