Skip to content

Change visit(TokenSyntax) function of SyntaxRewriter to return TokenSyntax #981

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
Oct 19, 2022

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Oct 19, 2022

There should be no reason why a TokenSyntax should get rewritten to a different node kind and this allow us to remove a force unwrap in the diagnostics infrastructure.

@ahoppen ahoppen requested a review from bnbarham October 19, 2022 07:55
@ahoppen
Copy link
Member Author

ahoppen commented Oct 19, 2022

@swift-ci Please test

1 similar comment
@ahoppen
Copy link
Member Author

ahoppen commented Oct 19, 2022

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Oct 19, 2022

swiftlang/swift-stress-tester#202

@swift-ci Please test

@ahoppen ahoppen force-pushed the ahoppen/rewriter-token-return-value branch from c6b2712 to 01e6656 Compare October 19, 2022 16:31
@ahoppen
Copy link
Member Author

ahoppen commented Oct 19, 2022

swiftlang/swift-stress-tester#202

@swift-ci Please test

@bnbarham
Copy link
Contributor

I think this is for the best, but I do wonder if we should extend it to others as well. Ie. should rewriting a function decl be able to rewrite to a statement? Or should we return a decl syntax there?

@ahoppen
Copy link
Member Author

ahoppen commented Oct 19, 2022

I was wondering the same. For TokenSyntax I was pretty sure that there’s no valid use case to rewrite it to any other node. For all the others, I would like to make a survey of the current SyntaxRewriter users (most notable swift-format and SwiftLint), whether they are making use of this option.

@allevato
Copy link
Member

SyntaxRewriter already restricts for many return types; for example, a rewriter for FunctionDeclSyntax can only return other DeclSyntaxes. I think that's the right behavior, and this improvement for TokenSyntax is a good one.

One situation that comes to mind where it's valid to replace a node with one of a different kind is at a statement level; a CodeBlockItem can have a Decl, Expr, or Stmt as its child so one could feasibly want to reach for a rewriter that swaps one out for something else. However, that would also allow for invalid replacements (e.g., replacing a Decl with a Stmt in a member list), so such a replacement is probably better done at the CodeBlockItem level by replacing the child instead of allowing it at the child itself.

@ahoppen ahoppen merged commit b0b249f into swiftlang:main Oct 19, 2022
@ahoppen ahoppen deleted the ahoppen/rewriter-token-return-value branch October 19, 2022 19:22
@bnbarham
Copy link
Contributor

SyntaxRewriter already restricts for many return types; for example, a rewriter for FunctionDeclSyntax can only return other DeclSyntaxes. I think that's the right behavior, and this improvement for TokenSyntax is a good one.

Ah. I was looking at the private implementation method rather than the public one 😅. I'd still say this applies for all the functions that currently return Syntax though.

One situation that comes to mind where it's valid to replace a node with one of a different kind is at a statement level; a CodeBlockItem can have a Decl, Expr, or Stmt as its child so one could feasibly want to reach for a rewriter that swaps one out for something else. However, that would also allow for invalid replacements (e.g., replacing a Decl with a Stmt in a member list), so such a replacement is probably better done at the CodeBlockItem level by replacing the child instead of allowing it at the child itself.

I think in that case I'd be fine with requiring clients to visit the parent instead, ie. I'd rather to default to not being able to rewrite to completely bogus trees but if someone reallllyy wants to...

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