ReviewOS

also looking at this

home-lang/home

fix(interpreter): report overflow when dividing the minimum integer by -1

#931
Open mkantautas wants to merge fix/interpreter-div-overflow into main
2 files +36 -4
packages/interpreter/tests/program_eval_test.zigmodified+29-0
Changes to packages/interpreter/tests/program_eval_test.zig
@@ -59,6 +59,35 @@ test "eval: nested arithmetic expression" {
5959 try runProgram("fn main() { print((1 + 2) * 3 - 4) }");
6060}
6161
62test "eval: dividing the minimum integer by -1 reports the overflow" {
63 try testing.expectError(error.RuntimeError, runProgram(
64 \\fn main() {
65 \\ let x = -9223372036854775807 - 1
66 \\ print(x / -1)
67 \\}
68 ));
69 try testing.expectError(error.RuntimeError, runProgram(
70 \\fn main() {
71 \\ let x = -9223372036854775807 - 1
72 \\ print(x ~/ -1)
73 \\}
74 ));
75 try testing.expectError(error.RuntimeError, runProgram(
76 \\fn main() {
77 \\ let x = -9223372036854775807 - 1
78 \\ print(x /! -1)
79 \\}
80 ));
81 try runProgram(
82 \\fn main() {
83 \\ let x = -9223372036854775807 - 1
84 \\ assert(is_none(x /? -1))
85 \\ assert(x / 1 == x)
86 \\ assert(x ~/ 2 == -4611686018427387904)
87 \\}
88 );
89}
90
6291test "eval: multiple statements and a returning function" {
6392 // Exercises several evaluateExpression entries across statements and a
6493 // user-function call — all of which were unreachable while expression depth