fix: report "Division by zero." for modulo by zero#910
Open
He-Pin wants to merge 1 commit into
Open
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 check in every evaluation path. Modification: Add zero-divisor guard for % in all evaluation paths: - Evaluator: tryInlineArith, evalBinaryOpNumNum, visitBinaryOpAsDouble, visitBinaryOp (polymorphic OP_% case) - StaticOptimizer: tryFoldBinaryOp (constant folding guard), tryFoldAsDouble (range folding sentinel) - stdlib: std.mod and std.modulo builtins - Update go_test_suite golden file for percent_mod_int5 Result: `42 % 0` now reports "Division by zero." matching go-jsonnet, instead of silently producing NaN.
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, the static optimizer, and stdlib builtins (std.mod,std.modulo). Previously42 % 0producedNaN; now it reports"Division by zero."matching go-jsonnet.Motivation
The modulo operator (
%) did not check for zero divisor, producingNaNinstead 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 check in every evaluation path.Modification
Add zero-divisor guard for
%in all evaluation paths:tryInlineArith,evalBinaryOpNumNum,visitBinaryOpAsDouble,visitBinaryOp(polymorphicOP_%case)tryFoldBinaryOp(constant folding guard),tryFoldAsDouble(range folding sentinel)std.modandstd.modulobuiltinsgo_test_suitegolden file forpercent_mod_int5Result
42 % 0now reports"Division by zero."matching go-jsonnet, instead of silently producingNaN.Test plan
go_test_suite/percent_mod_int5.jsonnet.goldennew_test_suite/error.modulo_by_zero.jsonnet(constant path)new_test_suite/error.modulo_by_zero_runtime.jsonnet(runtime path)