Repository navigation
ast: print expressions that parse back to the same nodes - #1026
Merged
Merged
Conversation
Node.String() left out parentheses that change the meaning, for example in (a ? b : c)[1:] and (a < b) < c, and wrote 2.0 as 2. The optimizer compares the collections of all, any and none by their string, so it combined calls on different collections.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Node.String()leaves out parentheses that the parser needs, so many expressions print as a different one:(a ? b : c)[1:]a ? b : c[1:](a + b)[1:2]a + b[1:2](-a).b-a.b(a < b) < ca < b < c, which isa < b && b < c(a * not b) * ca * not b * c, which isa * not (b * c)(a?.b)["c"]a?.b.c(let x = a; x) + blet x = a; x + b2.0 * a2 * areduce(a, #acc.b)reduce(a, .b)map(a, {{b: #}})map(a, {b: #}), a syntax error{"not": a}{not: a}, a syntax errorThis also gives wrong results, because
predicateCombinationcompares the collections by their string. Withcond = true,a = [-1, 5]andb = [7, 8]:is
true, andfalsewithexpr.Optimize(false). The second collection isa, which has a -1, sofalseis right.The printer now adds parentheses wherever the parser needs them. The output of all expressions in the existing print tests is the same as before. The visible changes for other expressions are the extra parentheses,
2.0for a float that is a whole number, and braces around a predicate that is a list of expressions or starts with a map.To find the cases I generated random expressions, and compared the nodes with those of the printed expression: 184480 of 269828 were equal before, and all 265254 are now (the second run also has
reduce, more pointers and escapes).TestPrint_parsehas one of each kind, and checks that it parses back to the same nodes: 34 of the 40 fail before. 3 of the 4 cases of the optimizer test fail before.