From 1ecb2947de0f8de0c343893fa377b22565c1e252 Mon Sep 17 00:00:00 2001 From: vycdev2 Date: Sat, 8 Aug 2026 10:23:49 +0000 Subject: [PATCH] fix: handle math abs int64 minimum --- crates/jett_comptime/src/interpreter.rs | 16 +++++++++++++++- crates/jett_driver/tests/fixture_suite.rs | 8 ++++++++ tests/runtime_fail/math_abs_int64_min.jett | 5 +++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/runtime_fail/math_abs_int64_min.jett diff --git a/crates/jett_comptime/src/interpreter.rs b/crates/jett_comptime/src/interpreter.rs index 930d86d2..9605dee1 100644 --- a/crates/jett_comptime/src/interpreter.rs +++ b/crates/jett_comptime/src/interpreter.rs @@ -8494,7 +8494,11 @@ impl Interpreter { "math.abs" => { require_args!(name, 1, args); match &args[0] { - Value::Int64(n) => Some(Ok(Value::Int64(n.abs()))), + Value::Int64(n) => Some( + n.checked_abs() + .map(Value::Int64) + .ok_or_else(|| format!("{name}: integer overflow: abs({n})")), + ), Value::Float64(n) => Some(Ok(Value::Float64(n.abs()))), _ => Some(Err(format!("{name} expects a numeric argument"))), } @@ -17413,6 +17417,16 @@ mod builtin_tests { assert_eq!(interp.eval_expr(&expr).unwrap(), Value::Int64(7)); } + #[test] + fn builtin_math_abs_int64_min_reports_overflow() { + let mut interp = Interpreter::new(); + let expr = dotted_call("math", "abs", vec![int(i64::MIN)]); + assert_eq!( + interp.eval_expr(&expr).unwrap_err(), + "math.abs: integer overflow: abs(-9223372036854775808)" + ); + } + #[test] fn builtin_math_abs_float() { let mut interp = Interpreter::new(); diff --git a/crates/jett_driver/tests/fixture_suite.rs b/crates/jett_driver/tests/fixture_suite.rs index d3810271..3e97bf25 100644 --- a/crates/jett_driver/tests/fixture_suite.rs +++ b/crates/jett_driver/tests/fixture_suite.rs @@ -396,6 +396,14 @@ fn math_sum_reports_overflow() { ); } +#[test] +fn math_abs_reports_overflow() { + assert_runtime_fail( + "math_abs_int64_min.jett", + "runtime error: math.abs: integer overflow: abs(-9223372036854775808)", + ); +} + #[test] fn run_file_capture_stdout_captures_json_runtime_output() { assert_run_stdout( diff --git a/tests/runtime_fail/math_abs_int64_min.jett b/tests/runtime_fail/math_abs_int64_min.jett new file mode 100644 index 00000000..b22ac0ba --- /dev/null +++ b/tests/runtime_fail/math_abs_int64_min.jett @@ -0,0 +1,5 @@ +function main() returns nothing: + int64 minimum = int64.from_string("-9223372036854775808") handle error: + return nothing + int64 absolute = math.abs(minimum) + return nothing