fix: add modulo-by-zero check and fix super[] without superclass#909
Closed
He-Pin wants to merge 1 commit into
Closed
fix: add modulo-by-zero check and fix super[] without superclass#909He-Pin wants to merge 1 commit into
He-Pin wants to merge 1 commit into
Conversation
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`
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.
Summary
%operator across all evaluation paths (tryInlineArith,evalBinaryOpNumNum,visitBinaryOpAsDouble,visitBinaryOp), the static optimizer (tryFoldBinaryOp,tryFoldAsDouble), and stdlib (std.mod,std.modulo). Previously42 % 0producedNaN; now it reports"Division by zero."matching go-jsonnet.visitLookupSuper(super[key]) to error when there is no superclass, matching the existingvisitSelectSuper(super.name) behavior and aligning with go-jsonnet/jrsonnet.Test plan
./mill 'sjsonnet.jvm[3.3.7]'.test)go_test_suite/percent_mod_int5.jsonnet.golden(was"not a number", now"Division by zero.")new_test_suite/error.modulo_by_zero.jsonnet— constant1 % 0new_test_suite/error.modulo_by_zero_runtime.jsonnet— runtimex % ywithy=0./mill 'sjsonnet.jvm[3.3.7]'.checkFormat)