Skip to content

Delete SyntaxTransformVisitor #2260

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

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Oct 4, 2023

Since ASTGen is no longer using it, we can delete it.

@ahoppen ahoppen requested a review from bnbarham as a code owner October 4, 2023 17:19
@ahoppen
Copy link
Member Author

ahoppen commented Oct 4, 2023

@swift-ci Please test

Copy link
Contributor

@bnbarham bnbarham left a comment

Choose a reason for hiding this comment

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

Will need to remove from CMake as well

@allevato
Copy link
Member

allevato commented Oct 4, 2023

Heh, I was experimenting with using this in a swift-format branch to rewrite parts of the pretty-printer 😭 What I'm in need of is a visitor where each visit method is truly recursive instead of splitting visit and visitPost into separate operations.

I could coax SyntaxRewriter to do what I want by just always returning the input node; what would the overhead of that be? If the visit methods always return the same node, would there be any excessive overhead there?

Since ASTGen is no longer using it, we can delete it.
@ahoppen ahoppen force-pushed the ahoppen/delete-syntaxtransformvisitor branch from 69852ca to 0877a61 Compare October 4, 2023 17:45
@ahoppen
Copy link
Member Author

ahoppen commented Oct 4, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Oct 4, 2023

Heh, I was experimenting with using this in a swift-format branch to rewrite parts of the pretty-printer 😭 What I'm in need of is a visitor where each visit method is truly recursive instead of splitting visit and visitPost into separate operations.

Why doesn’t SyntaxVisitor work for you? You could just ignore visitPost, right? I feel like I’m missing something.

I could coax SyntaxRewriter to do what I want by just always returning the input node; what would the overhead of that be? If the visit methods always return the same node, would there be any excessive overhead there?

The overhead shouldn’t be big. SyntaxRewriter has a special check to see if the syntax node has changed and if no node is changed, it doesn’t actually do any rewriting.

https://github.com/apple/swift-syntax/blob/1b7b2dbbcf03905ae410f29d54846f45c44c0584/Sources/SwiftSyntax/generated/SyntaxRewriter.swift#L3826

@allevato
Copy link
Member

allevato commented Oct 4, 2023

Why doesn’t SyntaxVisitor work for you? You could just ignore visitPost, right? I feel like I’m missing something.

One of the challenges of today's implementation is that because visitation of children is handled by returning .visitChildren/.skipChildren, we're not doing any direct recursion within the visit methods. That makes it error-prone because we can't use recursion and scoping to require that certain formatting tokens are correctly positioned before and after syntax nodes/tokens. For example, every open grouping command should have a corresponding close, and they need to be nested correctly.

Here's the structure I'd like to have instead, using true recursion and closure-based helper methods to define scopes that ensure everything lines up: https://github.com/allevato/swift-format/blob/new-pretty-printer/Sources/SwiftFormat/PrettyPrint/NewTokenStreamCreator.swift#L340-L371

But maybe I'm overthinking this. I guess I could stick with SyntaxVisitor but manually call visit on a node's children and always return .skipChildren from the visit methods to short-circuit the built-in traversal? Do you see any problems with that approach?

@ahoppen
Copy link
Member Author

ahoppen commented Oct 4, 2023

Oh, I see what you mean now. Using SyntaxRewriter and always returning the node itself should work for you and AFAICT there shouldn’t be any real performance overhead associated with it.

@ahoppen
Copy link
Member Author

ahoppen commented Oct 4, 2023

@swift-ci Please test

@ahoppen ahoppen merged commit 8e54964 into swiftlang:main Oct 5, 2023
@ahoppen ahoppen deleted the ahoppen/delete-syntaxtransformvisitor branch October 5, 2023 21:40
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.

3 participants