fix: force_operation_parentheses works under a parenthesised ancestor (#342) - #348
Draft
livingstaccato wants to merge 1 commit into
Draft
fix: force_operation_parentheses works under a parenthesised ancestor (#342)#348livingstaccato wants to merge 1 commit into
livingstaccato wants to merge 1 commit into
Conversation
…amplify-education#342) The option makes precedence explicit, and did so for a top-level expression. Inside one the caller had already parenthesised it added nothing: `(b + c * d)` came back unchanged, so the documents most likely to want explicit precedence got the least of it. `inside_parentheses` answers "did my immediate container already wrap me", which `_wrap_into_parentheses` reads to avoid doubling them. Two places widened it to "some ancestor is parenthesised": `ExprTermRule` carried the flag down with `or`, and the binary, unary and conditional rules passed it to their operands, which nothing directly wraps. Each term now sets the flag from its own `self.parentheses`, and the operation rules clear it for their operands. `((b + c) * d)` is still not doubled, and the option-less path emits exactly what it did before. With b=2, c=3, d=4, OpenTofu evaluates both `(b + c * d)` and the `(b + (c * d))` this now produces to 14.
Contributor
Author
|
Please hold off on merging this one for now — I want to do another review pass over it before it goes in. Opened as a draft for that reason; I will mark it ready and say so here once I am done. |
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.
Fixes #342.
What
force_operation_parenthesesexists to make precedence explicit, and it did so for a top-level expression. Inside one the caller had already parenthesised, it added nothing:So the documents most likely to want explicit precedence — the ones already carrying parentheses — got the least of it.
Why
inside_parenthesesanswers "did my immediate container already wrap me"._wrap_into_parenthesesreads it to avoid doubling. Two places widened it to "some ancestor is parenthesised":ExprTermRule.serializecarried it down asself.parentheses or context.inside_parentheses, so one parenthesised term set it for its whole subtree;Each term now sets the flag from its own
self.parentheses, and the operation rules clear it for their operands.What does not change
((b + c) * d)is not doubled,(b + c) * dkeeps its single pair, and the option-less path emits exactly what it did before — asserted for six shapes, since that is the half most easily broken by a change like this.With b=2, c=3, d=4, OpenTofu v1.12.5 evaluates both
(b + c * d)and the(b + (c * d))this produces to 14.Merging
It touches the same code as #334 (
hcl2/rules/expressions.py). Whichever of those lands first, this one needs a rebase rather than a merge — the overlaps are real edits to the same methods, not adjacent lines, so resolving them by hand risks losing one of the two fixes. Say the word and I will rebase and re-run the suite.This pull request, and the investigation behind it, were produced by an AI assistant (Claude) working on behalf of the author. Every reproduction, test run and benchmark cited was executed rather than inferred, but please review with that provenance in mind.