Skip to content

fix: error when comparing arrays containing function elements#958

Merged
stephenamar-db merged 3 commits into
databricks:masterfrom
He-Pin:fix/array-function-element-equality
Jun 18, 2026
Merged

fix: error when comparing arrays containing function elements#958
stephenamar-db merged 3 commits 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

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. Additionally, equal() had no top-level check for Val.Func operands, so function comparisons in any context silently returned false instead of raising an error.

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, returns false for func-vs-non-func
  • Fixed the array equality loop: shared Eval references now force the value and check for Val.Func before skipping

The top-level Val.Func case in equal() handles function detection at any nesting depth through recursion — no explicit checks needed in array/object comparison loops.

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. All nested cases ([f] == [g], {a: f} == {a: g}, [[f]] == [[g]]) also work correctly through equal() recursion.

Expression go-jsonnet v0.22.0 sjsonnet (before) sjsonnet (after)
local f = function(x) x; f == f ERROR ERROR ERROR
local f = function(x) x; [f, 1] == [f, 1] ERROR true (bug) ERROR
local f = function() 3; local g = function() 4; [f] == [g] ERROR false (bug) ERROR
local f = function() 3; local g = function() 4; {a: f} == {a: g} ERROR false (bug) ERROR
local f = function(x) x; f == 42 false false false
[1, 2, 3] == [1, 2, 3] true true true
[f] == [1] false false false
{a: f} == {a: 1} false false false

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.

@He-Pin He-Pin force-pushed the fix/array-function-element-equality branch from e523442 to d9f67f3 Compare June 18, 2026 10:47
He-Pin added 2 commits June 18, 2026 20:46
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, so same-
  reference functions fall through to the explicit Func case that errors
- 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 if
  the resolved value is a Val.Func before skipping

Result:
[f, 1] == [f, 1] now correctly errors with "cannot test equality of
functions", matching go-jsonnet behavior. Non-function array comparisons
are unaffected.
Motivation:
Scala 3.3.7 strict mode rejects discarded non-Unit values. The else
branch in the array equality loop referenced `v` (a Val) without using
it, causing a compilation error in native/js/wasm builds.

Modification:
Replace the pointless `v` reference with a comment clarifying that
shared non-Func values are implicitly equal.

Result:
All Scala 3.3.7 targets (jvm, native, js, wasm) compile successfully.
@He-Pin He-Pin force-pushed the fix/array-function-element-equality branch from d9f67f3 to dbac5b8 Compare June 18, 2026 12:46
Add test coverage for function equality comparisons inside arrays and
objects: [f] == [g] and {a: f} == {a: g} raise errors, while mixed
containers (function vs non-function) correctly return false.
@stephenamar-db stephenamar-db merged commit eea7d11 into databricks:master Jun 18, 2026
5 checks passed
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.

2 participants