-
Notifications
You must be signed in to change notification settings - Fork 199
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
Piranha hangs infinitely when removing a feature flag from Kotlin closure #701
Comments
Interesting. I will investigate and get back to you |
Let me know if you need more details I can share the complete test-case with you |
This works fine on my side? Can you share the code you're running, or provide a full test case where this happens? Try running this: from polyglot_piranha import execute_piranha, PiranhaArguments, Rule, RuleGraph, OutgoingEdges
# Original code snippet
code = """
class Sample {
val lambda1 = {
val x = featureService.isEnabled(STALE_FLAG)
print(x)
}
val lambda2 = {
val x = featureService.isEnabled(STALE_FLAG)
print(x)
}
val lambda3 = {
val x = featureService.isEnabled(STALE_FLAG)
print(x)
}
}
"""
# Define the rule to replace the method call
r1 = Rule(
name="replace_method",
query="""((call_expression
(navigation_expression
(simple_identifier)
(navigation_suffix
(simple_identifier) @fn_name
)
)
(call_suffix
(value_arguments
(value_argument
(simple_identifier) @flag_name
)
)
)
) @call_expression
(#eq? @fn_name "isEnabled")
(#eq? @flag_name "@stale_flag_name"))
""", # cs indicates we are using concrete syntax
replace_node="call_expression",
replace="true",
holes={"stale_flag_name"},
is_seed_rule=True
)
# Define the edges for the rule graph.
# In this case, boolean_literal_cleanup is already defined for java [see src/cleanup_rules]
edge = OutgoingEdges("replace_method", to=["boolean_literal_cleanup"], scope="Parent")
# Create Piranha arguments
piranha_arguments = PiranhaArguments(
code_snippet=code,
language="kt",
rule_graph=RuleGraph(rules=[r1], edges=[edge],),
substitutions={"stale_flag_name": "STALE_FLAG"}
)
# Execute Piranha and print the transformed code
piranha_summary = execute_piranha(piranha_arguments)
print(piranha_summary[0].content) |
Hello @danieltrt . I pasted this code to my Jupyter notebook and the result is the same - Piranha hangs. I also executed this rule in the Rust debugger and I observe the same behaviour. |
I activated debug logging:
and from the logs I can see that Piranha has stuck on the following log:
|
Ah! You're right! I was using my patched version of Piranha, which is why it wasn't hanging. The issue here is that Piranha erroneously propagates the value to the subsequent declarations, causing it to get stuck in the loop with. Piranha operates at the syntax level and propagates the scope within Parent scopes (3 levels) incorrectly. After the first replacement, it propagates the value erroneously: class Sample {
val lambda1 = {
val x = featureService.isEnabled(STALE_FLAG)
print(x)
}
val lambda2 = {
val x = featureService.isEnabled(STALE_FLAG)
print(x)
}
val lambda3 = {
val x = featureService.isEnabled(STALE_FLAG)
print(x)
}
} gets transformed to: class Sample {
val lambda1 = {
print(true)
}
val lambda2 = {
val true = featureService.isEnabled(STALE_FLAG) // incorrect propagation
print(true)
}
val lambda3 = {
val true = featureService.isEnabled(STALE_FLAG)
print(true)
}
} This causes it to get stuck in the loop of @ketkarameya , do you know how to resolve this? One potential solution might be to use a new scope or reduce the parent scope from 3 to 2 or 1? |
I don’t think we can solve this as of now. But if you want a workaround you can try patching Piranha and see if it works for your use case piranha/src/models/default_configs.rs Line 47 in 58e0be9
If you lower the value in the config file you can config Piranha to not propagate the value erroneously outside the lambda by being more conservative in the Parent scope applications. I think if you set this value to 1 or 2, this problem should go away |
Thanks for the response @danieltrt . Would it be ok if I at some point in the future I will try to solve it and create a PR with the fix? I played around with adding a new scope |
Absolutely! Let me know if you need any help |
"stale_flag_name" => "STALE_FLAG"
"treated" => "true"
rules:
Expected result:
The text was updated successfully, but these errors were encountered: