Skip to content
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

ast/compile: fix print rewriting for arrays in := and head vars #4086

Conversation

srenatus
Copy link
Contributor

@srenatus srenatus commented Dec 2, 2021

For both partial and complete rules, when variables from destructured
arrays in assignments had been used in the rule head, the print rewriting
would fail.

Now, this situation is accounted for by starting the safe VarSet with

r.Head.Vars()

instead of

r.Head.Args.Vars()

The former will check each of the Args, Key and Value fields of Head: if
it's non-empty, it'll add .Vars() to the returned VarSet.

Fixes #4078.

For both partial and complete rules, when variables from destructured
arrays in assignments had been used in the rule head, the print rewriting
would fail.

Now, this situation is accounted for by starting the safe VarSet with

    r.Head.Vars()

instead of

    r.Head.Args.Vars()

The former will check each of the Args, Key and Value fields of Head: if
it's non-empty, it'll add .Vars() to the returned VarSet.

Fixes open-policy-agent#4078.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
@srenatus srenatus force-pushed the sr/ast/compile-print-rewrite-and-head-vars branch from 940fa77 to 892ee15 Compare December 2, 2021 12:59
@srenatus srenatus marked this pull request as ready for review December 2, 2021 13:08
@srenatus srenatus marked this pull request as draft December 2, 2021 13:11
@srenatus
Copy link
Contributor Author

srenatus commented Dec 2, 2021

While this change does the trick, I'm a still wondering if it's the right fix: alternatively, outputVarsForExprEq([_, x, _] = f(1)) could yield x. It doesn't.

@srenatus
Copy link
Contributor Author

srenatus commented Dec 2, 2021

Hm I think this is OK. After all, [x, y] = z for three variables yields no output vars, and that's pretty much the same as [x, y] = f(1).

@srenatus srenatus marked this pull request as ready for review December 2, 2021 13:28
@tsandall tsandall merged commit d1525c1 into open-policy-agent:main Dec 3, 2021
@srenatus srenatus deleted the sr/ast/compile-print-rewrite-and-head-vars branch December 3, 2021 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

print fails for variables assigned in destructured array
2 participants