Skip to content
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

Go Cleanup Built-in Rules #265

Merged
merged 56 commits into from
Dec 19, 2022
Merged

Go Cleanup Built-in Rules #265

merged 56 commits into from
Dec 19, 2022

Conversation

dvmarcilio
Copy link
Collaborator

Ongoing work on writing built-in cleanup rules for Go.

@dvmarcilio dvmarcilio changed the title Go Cleanup Built-in Rules [WIP] Go Cleanup Built-in Rules Nov 10, 2022
@dvmarcilio dvmarcilio force-pushed the go_develop branch 3 times, most recently from ee5aa14 to 1db699d Compare November 18, 2022 18:12
polyglot/piranha/README.md Outdated Show resolved Hide resolved
(binary_expression
left: [(true) (parenthesized_expression (true))]
operator:"&&"
right : (_)* @rhs
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need * ? There has to be one RHS in the binary expression right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done. Thanks!

polyglot/piranha/src/cleanup_rules/go/rules.toml Outdated Show resolved Hide resolved
(
(binary_expression
left: (
[
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if we just use a wildcard for @lhs and @rhs ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done. Thanks!

replace = "@nested.statements"
replace_node = "nested.block"

# This rule is not deleting multiple statements after return.
Copy link
Collaborator

Choose a reason for hiding this comment

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

IS that some issue with the grammar? Can we look into it ?

Copy link
Collaborator Author

@dvmarcilio dvmarcilio Dec 2, 2022

Choose a reason for hiding this comment

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

We can now successfully match all statements after return_statement in a similar way as e.g., Java and Javascript.
Accounting for the \nterminator did the trick:

(((statement) "\\n"?)+ @post)

However, replace is only working for the first statement. This is also happening in Java (draft PR #284).
The self-edge makes it work for the current implementation.

Copy link
Collaborator

Choose a reason for hiding this comment

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

use dummy node

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Introduced dummy node return_cleanup and now we have a cycle

polyglot/piranha/src/cleanup_rules/go/rules.toml Outdated Show resolved Hide resolved
query = """
(
(binary_expression
left : (_)* @lhs
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
left : (_)* @lhs
left : (_) @lhs

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

)
"""
queries = [
"""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add more constraints.

Delete var declaration if all assignments' rhs eq init of var decl;
Delete all assignments if no var decl exists in scope

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It turns out that this delete_parent_assignment rule was doing nothing. I removed it, and everything is working as expected.
Well-spotted that the constraint was not working.

@dvmarcilio dvmarcilio marked this pull request as ready for review December 7, 2022 13:40
@dvmarcilio dvmarcilio changed the title [WIP] Go Cleanup Built-in Rules Go Cleanup Built-in Rules Dec 7, 2022
#
# Note that this rule *won't* rewrite when @lhs is a call.
[[rules]]
groups = ["boolean_expression_simplify"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have to think of a strategy to prevent it from simplifying after <something> || true

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@ketkarameya should we deal with this in this PR? or do we open a new issue after this is accepted?

Copy link
Collaborator

Choose a reason for hiding this comment

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

i think a follow up would be ok. We can address this alongside the fix for java/kt

#####
# Dummy rule to introduce a cycle for `delete_statement_after_return`
[[rules]]
name = "return_cleanup"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think there is some naming convention for dummy rules in the readme

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Renamed to return_statement_cleanup

holes = ["stale_flag_name"]

[[rules]]
name = "replace_identifier_with_str_literal"
Copy link
Collaborator

Choose a reason for hiding this comment

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

what is the purpose of this rule (And the above one)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the review, @ketkarameya. I agree that this rule is just noise. I restructured this test to have two rules:

  • find_const_str_literal: finds a constant (@const_id with a value equal to stale_flag_name piranha_argument.
  • false_flag: replaces with false calls to BoolValue() with an identifier argument equal to @const_id (hole).

Now the test doesn't remove the constant declaration.

Copy link
Collaborator

@ketkarameya ketkarameya left a comment

Choose a reason for hiding this comment

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

I have left a small comment on the tests. Otherwise this PR looks good to me.

@dvmarcilio dvmarcilio merged commit e2c9338 into master Dec 19, 2022
@dvmarcilio dvmarcilio deleted the go_develop branch December 19, 2022 18: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.

None yet

2 participants