fix(interpreter): return runtime errors for division by zero and integer overflow - #328
Merged
Merged
Conversation
…ger overflow
eval_integer_infix and eval_prefix_minus used raw i64 arithmetic, so
`1 / 0` and `i64::MIN / -1` panicked in every build profile, while
`+ - *` and unary minus panicked in debug builds and silently wrapped
in release builds - evaluation semantics depended on the build profile.
Switch to checked_add/sub/mul/div/neg and surface failures as EvalError
with the same wording the bytecode VM uses ("division by zero",
"integer overflow in addition" etc., compiler/vm.rs), so the two
engines report these cases identically. Boundary values that do fit in
i64 still evaluate; regression tests pin both the errors and the
boundaries.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Problem
The tree-walking interpreter still did raw
i64arithmetic ineval_integer_infixandeval_prefix_minus:1 / 0;→panicked at interpreter/lib.rs:425: attempt to divide by zero— in release builds too (Rust always checks division), killing the REPL/process.let m = -9223372036854775807 - 1; m / -1;→ panic (divide with overflow), all profiles.9223372036854775807 + 1;→ panic in debug builds, silent wrap in release builds — evaluation semantics depended on the build profile.The bytecode VM was already hardened (
compiler/vm.rsuseschecked_*and returns classified runtime errors); the interpreter was the un-hardened outlier.Fix
Use
checked_add/sub/mul/div/negand returnEvalErrorwith the exact wording the bytecode VM uses (division by zero,integer overflow in addition/subtraction/multiplication/division/negation), so both engines report these cases identically. Boundary values that fit ini64(e.g.-9223372036854775807 - 1) still evaluate normally.Before/after on the REPL:
Testing
test_integer_arithmetic_errorspins all six error cases;test_integer_arithmetic_boundaries_still_evaluatepins thati64::MIN-adjacent values still work.cargo test --workspace: 396 passed, 0 failed. Clippy andcargo fmt --checkclean.Affected crates:
interpreteronly. (The GC VM'swrapping_*add/sub/mul drift vs the compiler VM is a separate known issue, not touched here.)🤖 Generated with Claude Code