x/tools/internal/refactor/inline: simplify control statements when the operand is known #65572
Labels
gopls
Issues related to the Go language server, gopls.
Refactoring
Issues related to refactoring tools
Tools
This label describes issues relating to any tools in the x/tools repository.
Milestone
When inlining a call
f(true)
to a function such as this one:the inliner should apply logical simplifications to the body when the argument value (here "true") is known. The result would then be simply
fmt.Println("hello")
.Logical simplification is conceptually orthogonal to inlining: all it requires is a piece of code and some extra knowledge about the values or variables (e.g. x = true). But unfortunately the inliner doesn't get to see the typed syntax after the inlining, so the analysis would need to be integrated into the callee portion of the inliner analysis.
A good starting point would be to tabulate the set of statements of the form
for range x {...}
orif x {...} else {...}
where the operand x depends only on constants and parameters. The expression can then be retained and reevaluated (similar to the approach in the falcon analysis), and if it is degenerate (true, false, zero, empty, a singleton, etc) then the statement can be reduced according to a set of rules:@stapelberg
The text was updated successfully, but these errors were encountered: