Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions crates/jett_comptime/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8740,13 +8740,15 @@ impl Interpreter {
require_args!(name, 2, args);
match (&args[0], &args[1]) {
(Value::Int64(a), Value::Int64(b)) => {
let (mut x, mut y) = (a.abs(), b.abs());
let (mut x, mut y) = (a.unsigned_abs(), b.unsigned_abs());
while y != 0 {
let t = y;
y = x % y;
x = t;
}
Some(Ok(Value::Int64(x)))
Some(i64::try_from(x).map(Value::Int64).map_err(|_| {
format!("math.gcd: integer overflow: result {x} does not fit int64")
}))
}
_ => Some(Err(format!("{name} expects two int64 arguments"))),
}
Expand Down Expand Up @@ -17448,6 +17450,24 @@ mod builtin_tests {
assert_eq!(interp.eval_expr(&expr).unwrap(), Value::Float64(2.5));
}

#[test]
fn builtin_math_gcd_handles_int64_min_with_fitting_result() {
let mut interp = Interpreter::new();
let expr = dotted_call("math", "gcd", vec![int(i64::MIN), int(6)]);
assert_eq!(interp.eval_expr(&expr).unwrap(), Value::Int64(2));
}

#[test]
fn builtin_math_gcd_reports_unrepresentable_result() {
let mut interp = Interpreter::new();
let expr = dotted_call("math", "gcd", vec![int(i64::MIN), int(0)]);
let err = interp.eval_expr(&expr).unwrap_err();
assert_eq!(
err,
"math.gcd: integer overflow: result 9223372036854775808 does not fit int64".to_string()
);
}

#[test]
fn builtin_int64_from_string() {
let mut interp = Interpreter::new();
Expand Down
8 changes: 8 additions & 0 deletions crates/jett_driver/tests/fixture_suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,14 @@ fn sized_integer_runtime_bounds_reject_variable_arithmetic() {
}
}

#[test]
fn math_gcd_reports_unrepresentable_result() {
assert_runtime_fail(
"math_gcd_int64_min.jett",
"runtime error: math.gcd: integer overflow: result 9223372036854775808 does not fit int64",
);
}

#[test]
fn math_sum_reports_overflow() {
assert_runtime_fail(
Expand Down
6 changes: 6 additions & 0 deletions tests/runtime_fail/math_gcd_int64_min.jett
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function main() returns nothing:
int64 minimum = int64.from_string("-9223372036854775808") handle error:
return nothing
int64 value = math.gcd(minimum, 0)
println(value)
return nothing