You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
go/printer is inconsistent in handling the absence of ParenExpr. First, let me show an example that automatically inserts required parentheses, despite no ParenExpr existing in the AST:
that is, it doesn't insert the necessary parentheses to make the printed form syntactically valid.
I am of the opinion that the second example should produce correct output, inserting parentheses as necessary. go/printer already has code that strips an existing ParenExpr if it is not required (i.e. it turns if (b) into if b), but leaves it intact otherwise. It should similarly be possible to insert missing parentheses, based on the same logic.
Users of gofmt can also run into this issue: the rewrite rule (a) -> a, which is documented as stripping unnecessary parentheses, incorrectly strips parentheses of the if (x == T{}) form.
IMHO, an AST should not require explicit parentheses to be valid, and go/printer should take care of turning a valid AST into valid code. I'd like to go over go/printer and fix all instances of this problem, thus making ParenExpr optional.
go/printer is inconsistent in handling the absence of ParenExpr. First, let me show an example that automatically inserts required parentheses, despite no ParenExpr existing in the AST:
prints as
despite the lack of a ParenExpr. I posit that this is correct behavior.
Now, consider this example:
prints as
that is, it doesn't insert the necessary parentheses to make the printed form syntactically valid.
I am of the opinion that the second example should produce correct output, inserting parentheses as necessary. go/printer already has code that strips an existing ParenExpr if it is not required (i.e. it turns
if (b)
intoif b
), but leaves it intact otherwise. It should similarly be possible to insert missing parentheses, based on the same logic.Users of gofmt can also run into this issue: the rewrite rule
(a) -> a
, which is documented as stripping unnecessary parentheses, incorrectly strips parentheses of theif (x == T{})
form.IMHO, an AST should not require explicit parentheses to be valid, and go/printer should take care of turning a valid AST into valid code. I'd like to go over go/printer and fix all instances of this problem, thus making ParenExpr optional.
/cc @griesemer for his expert opinion
The text was updated successfully, but these errors were encountered: