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 unification #4099

Conversation

srenatus
Copy link
Contributor

@srenatus srenatus commented Dec 6, 2021

outputVarsForExprEq, which is used in the rewriting of print() calls,
wasn't able to find x in

[_, x, _] = f(1)

and thus certain print expressions failed during the rewriting stage of
print() calls in the compiler.

Now, the "collection (array/object) = func call" is handled.

Furthermore, this adds calls to isRefSafe in a few places: as it turned
out, the test cases

{"array/ref", "[1,2,x] = a[_]", "[a]", "[x]"},
{"object/ref", `{"x": x} = a[_]`, "[a]", "[x]"},

would have, without the check, passed without "a" in "safe" (3rd field).
The fact that these cases are including "a" is evidence for this being
the correct behaviour.

Fixes #4078.

@srenatus srenatus marked this pull request as ready for review December 6, 2021 13:26
tsandall
tsandall previously approved these changes Dec 7, 2021
Copy link
Member

@tsandall tsandall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I think there's room for a small improvement on the unify test cases.

@@ -31,7 +31,14 @@ func TestUnify(t *testing.T) {
{"object/var-3", `{"x": 1, "y": x} = y`, "[]", "[]"},
{"object/uneven", `{"x": x, "y": 1} = {"x": y}`, "[]", "[]"},
{"object/uneven", `{"x": x, "y": 1} = {"x": y}`, "[x]", "[]"},
{"call", "x = f(y)[z]", "[y]", "[x]"},
{"var/call-ref", "x = f(y)[z]", "[y]", "[x]"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I could imagine adding negative cases where one or more of the input arguments are not safe and therefore the output should not include any new variables. I'd include cases w/ vars as args as well as arrays/sets/objects containing vars.

outputVarsForExprEq, which is used in the rewriting of `print()` calls,
wasn't able to find `x` in

    [_, x, _] = f(1)

and thus certain print expressions failed during the rewriting stage of
print() calls in the compiler.

Now, the "collection (array/object) = func call" is handled.

Furthermore, this adds calls to isRefSafe in a few places: as it turned
out, the test cases

    {"array/ref", "[1,2,x] = a[_]", "[a]", "[x]"},
    {"object/ref", `{"x": x} = a[_]`, "[a]", "[x]"},

would have, without the check, passed without "a" in "safe" (3rd field).
The fact that these cases are including "a" is evidence for this being
the correct behaviour.

Fixes open-policy-agent#4078.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
@srenatus srenatus force-pushed the sr/ast/fix-rewriting-print-calls-via-output-vars-of-eq-expr branch from 3a99fcb to 6f830dc Compare December 8, 2021 09:58
case Call:
if isCallSafe(b, u.safe) {
u.markSafe(a)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Found this when adding extra negative test cases.

@srenatus srenatus merged commit eea2ba1 into open-policy-agent:main Dec 8, 2021
@srenatus srenatus deleted the sr/ast/fix-rewriting-print-calls-via-output-vars-of-eq-expr branch December 8, 2021 10:11
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