Skip to content

fix: error when comparing arrays containing function elements#958

Open
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/array-function-element-equality
Open

fix: error when comparing arrays containing function elements#958
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/array-function-element-equality

Conversation

@He-Pin

@He-Pin He-Pin commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix array equality silently returning true when comparing arrays containing function elements.

Motivation

Array equality silently returned true when comparing arrays containing function elements that shared the same scope binding reference. The (x eq y) reference-equality shortcut and the array equality loop's shared-reference optimization bypassed the "cannot test equality of functions" error that go-jsonnet raises.

Modification

  • Guarded the (x eq y) shortcut in equal() to exclude Val.Func, so same-reference functions fall through to an explicit Val.Func case that errors
  • Added Val.Func case in equal() that errors when both sides are functions
  • Fixed the array equality loop: shared Eval references now force the value and check for Val.Func before skipping

Result

[f, 1] == [f, 1] and f == f now correctly error with "cannot test equality of functions", matching go-jsonnet, jsonnet-cpp, and jrsonnet behavior.

Behavior Comparison

Expression go-jsonnet v0.22.0 jsonnet-cpp v0.22.0 jrsonnet v0.5.0-pre99 sjsonnet (before) sjsonnet (after)
local f = function(x) x; f == f ERROR ERROR ERROR ERROR ERROR
local f = function(x) x; [f, 1] == [f, 1] ERROR ERROR ERROR true (bug) ERROR
local f = function(x) x; f == 42 false false false false false
[1, 2, 3] == [1, 2, 3] true true true true true

References

  • go-jsonnet function equality check in builtins.go

@He-Pin He-Pin force-pushed the fix/array-function-element-equality branch 2 times, most recently from 425bca0 to d819081 Compare June 17, 2026 19:30
@He-Pin He-Pin marked this pull request as draft June 17, 2026 19:37
@He-Pin He-Pin force-pushed the fix/array-function-element-equality branch from d819081 to 363d981 Compare June 17, 2026 23:23
@He-Pin He-Pin marked this pull request as ready for review June 18, 2026 03:48
@He-Pin

He-Pin commented Jun 18, 2026

Copy link
Copy Markdown
Contributor Author

@stephenamar-db This is ready too.

Motivation:
go-jsonnet errors with "cannot test equality of functions" when comparing
arrays that contain function elements (e.g. [f, 1] == [f, 1]). sjsonnet
silently returned true because the shared-reference optimization in the
array equality loop skipped forcing lazy thunks without checking if the
resolved value was a function.

Modification:
- Guard the (x eq y) shortcut in equal() to exclude Val.Func
- Add a Val.Func case in equal() that errors when both sides are functions
- In the array equality loop, force shared Eval references and check for
  Val.Func before skipping
- Fix Scala 3.3.7 compile error from discarded non-Unit value

Result:
[f, 1] == [f, 1] now correctly errors with "cannot test equality of
functions", matching go-jsonnet, jsonnet-cpp, and jrsonnet behavior.

| Expression | go-jsonnet v0.22.0 | jsonnet-cpp v0.22.0 | jrsonnet v0.5.0-pre99 | sjsonnet (before) | sjsonnet (after) |
|---|---|---|---|---|---|
| f == f | ERROR | ERROR | ERROR | ERROR | ERROR |
| [f, 1] == [f, 1] | ERROR | ERROR | ERROR | true (bug) | ERROR |
| f == 42 | false | false | false | false | false |
| [1,2,3] == [1,2,3] | true | true | true | true | true |
@He-Pin He-Pin force-pushed the fix/array-function-element-equality branch from e523442 to d9f67f3 Compare June 18, 2026 10:47
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.

1 participant