fix: reject function equality comparisons with clear error#966
Closed
He-Pin wants to merge 1 commit into
Closed
Conversation
Motivation:
Jsonnet semantics dictate that functions cannot be compared for equality.
sjsonnet silently returned false for `f == g` (two functions), diverging
from go-jsonnet, C++ jsonnet, and jrsonnet which all raise runtime errors.
This silent divergence masked bugs where users accidentally compared
functions instead of their return values.
Modification:
- Evaluator.scala: Add Val.Func checks in `equal()` for object field
comparison, array element comparison, and top-level comparison
- Raise Error.fail("cannot test equality of functions") when both operands
are Val.Func instances
- Handle asymmetric cases (func == non-func) by returning false after
checking the non-func operand
Result:
`function(x) x == function(x) x` now raises:
sjsonnet.Error: cannot test equality of functions
matching go-jsonnet, C++ jsonnet, and jrsonnet behavior. Prevents silent
false results that mask comparison bugs.
References:
- go-jsonnet: "RUNTIME ERROR: Cannot test equality of functions"
- C++ jsonnet: "RUNTIME ERROR: cannot test equality of functions"
- jrsonnet: "runtime error: cannot test equality of functions"
Contributor
Author
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.
Motivation
Jsonnet semantics dictate that functions cannot be compared for equality. sjsonnet silently returned
falseforf == g(two functions), diverging from go-jsonnet, C++ jsonnet, and jrsonnet which all raise runtime errors. This silent divergence masked bugs where users accidentally compared functions instead of their return values.Modification
Val.Funcchecks inequal()for object field comparison (lazy-evaluated fields), array element comparison, and top-level scalar comparisonError.fail("cannot test equality of functions")when both operands areVal.FuncinstancesResult
function(x) x == function(x) xnow raisessjsonnet.Error: cannot test equality of functions, matching go-jsonnet, C++ jsonnet, and jrsonnet behavior.RUNTIME ERROR: Cannot test equality of functionsRUNTIME ERROR: cannot test equality of functionsruntime error: cannot test equality of functionsfalsesjsonnet.Error: cannot test equality of functionsTest plan
function(x) x == function(x) xraises errorfunction(x) x != function(x) xraises error[f] == [g]raises error{a: f} == {a: g}raises error1 == 2,"a" == "b", etc.