Skip to content

fix: add modulo-by-zero check and fix super[] without superclass#909

Closed
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/code-review-findings
Closed

fix: add modulo-by-zero check and fix super[] without superclass#909
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/code-review-findings

Conversation

@He-Pin

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

Copy link
Copy Markdown
Contributor

Summary

  • Add zero-divisor check for % operator across all evaluation paths (tryInlineArith, evalBinaryOpNumNum, visitBinaryOpAsDouble, visitBinaryOp), the static optimizer (tryFoldBinaryOp, tryFoldAsDouble), and stdlib (std.mod, std.modulo). Previously 42 % 0 produced NaN; now it reports "Division by zero." matching go-jsonnet.
  • Fix visitLookupSuper (super[key]) to error when there is no superclass, matching the existing visitSelectSuper (super.name) behavior and aligning with go-jsonnet/jrsonnet.

Test plan

  • All 143 existing test suites pass (./mill 'sjsonnet.jvm[3.3.7]'.test)
  • Updated go_test_suite/percent_mod_int5.jsonnet.golden (was "not a number", now "Division by zero.")
  • Added new_test_suite/error.modulo_by_zero.jsonnet — constant 1 % 0
  • Added new_test_suite/error.modulo_by_zero_runtime.jsonnet — runtime x % y with y=0
  • Formatting clean (./mill 'sjsonnet.jvm[3.3.7]'.checkFormat)

Motivation:
The modulo operator (%) did not check for zero divisor, producing NaN
instead of an error. This was inconsistent with go-jsonnet which reports
"Division by zero." and also inconsistent with sjsonnet's own division
operator (/) which already had the zero check.

Additionally, `super[key]` (visitLookupSuper) silently fell back to
`self` when no superclass existed, while `super.name` (visitSelectSuper)
correctly errored. This inconsistency could hide bugs.

Modification:
- Add zero-divisor check for % in all evaluation paths:
  tryInlineArith, evalBinaryOpNumNum, visitBinaryOpAsDouble,
  visitBinaryOp, and StaticOptimizer (tryFoldBinaryOp, tryFoldAsDouble)
- Add zero-divisor check for std.mod and std.modulo builtins
- Fix visitLookupSuper to error when sup is null, matching
  visitSelectSuper behavior
- Change sup from var to val since it is no longer reassigned
- Update go_test_suite golden file for percent_mod_int5
- Add regression tests for modulo-by-zero

Result:
- `42 % 0` now reports "Division by zero." matching go-jsonnet
- `super["key"]` without superclass now errors consistently
  with `super.key`
@He-Pin He-Pin closed this Jun 10, 2026
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