Skip to content

Remove unnecessary casts in SyntaxRewriter #2119

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

natikgadzhi
Copy link
Contributor

@natikgadzhi natikgadzhi commented Aug 30, 2023

This pull request removes unnecessary casts in SyntaxRewriter, i.e. funcs that look like this:

open func visit(_ node: AttributeListSyntax) -> AttributeListSyntax {
    return Syntax(visitChildren(node)).cast(AttributeListSyntax.self)
}

become this:

open func visit(_ node: AttributeListSyntax) -> AttributeListSyntax {
    return visitChildren(node)
}

It's more readable, and visitChildren(node) is guaranteed to return the same type as node anyway, so the cast is unnecessary. Thank you, @Matejkob, for filing the issue with pointers on how to get started!

Closes #2102.

Disclaimer: I'm here to learn, and just making my first steps. Please feel free to "well, actually" me.

TODOs

  • Tests crash locally without SKIP_LONG_TESTS. The crash is in SyntaxVisitor.visit and pretty deep. But, they also crash for me on main. 👀

@natikgadzhi natikgadzhi requested a review from ahoppen as a code owner August 30, 2023 06:30
@@ -23,11 +23,11 @@

open class SyntaxRewriter {
public let viewMode: SyntaxTreeViewMode

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, a lot of these is just swift-format 508.0.1.

Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/apple/swift-syntax/blob/b4749403093de49b11418d7bd80f7ee2b4a926dd/CONTRIBUTING.md?plain=1#L26

swift-format doesn't format generated code, so I think something went awry during the code generation process. Would you mind re-running the code generation to try to fix it?

@Matejkob
Copy link
Contributor

Thank you, @natikgadzhi, for working on this! ❤️

@natikgadzhi
Copy link
Contributor Author

natikgadzhi commented Aug 30, 2023 via email

@natikgadzhi natikgadzhi force-pushed the natikgadzhi/syntax-rewriter-refactor-cast branch from 359d684 to 6bfac1e Compare August 30, 2023 16:18
@natikgadzhi
Copy link
Contributor Author

There! Much cleaner now.

@natikgadzhi natikgadzhi requested a review from Matejkob August 30, 2023 16:20
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Nice. Thank you. So much cleaner. 🤩

@ahoppen
Copy link
Member

ahoppen commented Aug 30, 2023

Happy to generate without formatting through — my bad, I should have just ran format.py

We don’t manually format the generated files because the formatting will disappear the next time that you re-run CodeGeneration, which makes it hard to spot the modifications made by CodeGeneration.

@ahoppen
Copy link
Member

ahoppen commented Aug 30, 2023

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge August 30, 2023 17:19
Copy link
Contributor

@kimdv kimdv left a comment

Choose a reason for hiding this comment

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

Thanks @natikgadzhi 🙌

@ahoppen
Copy link
Member

ahoppen commented Aug 30, 2023

@swift-ci Please test

@ahoppen ahoppen merged commit 4652ad2 into swiftlang:main Aug 31, 2023
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.

Redundant type casting in generated visit methods of SyntaxRewriter
4 participants