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
29 changes: 29 additions & 0 deletions libs/@local/hashql/mir/src/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,17 @@ pub enum RuntimeError<'heap, E, A: Allocator> {
limit: usize,
},

/// Integer arithmetic overflowed the supported range.
///
/// This is a user-facing error caused by an implementation limitation:
/// the interpreter uses 128-bit integers, but the language specifies
/// arbitrary-precision integers. Operations that produce results outside
/// the `i128` range fail here.
IntegerOverflow {
/// Description of the operation that overflowed (e.g., "addition").
operation: &'static str,
},

/// Value has the wrong runtime type.
///
/// This is an ICE: type checking should ensure values have the
Expand Down Expand Up @@ -423,6 +434,7 @@ impl<E, A: Allocator> RuntimeError<'_, E, A> {
Self::OutOfRange { length, index } => out_of_range(span, length, index),
Self::InputNotFound { name } => input_not_found(span, name),
Self::RecursionLimitExceeded { limit } => recursion_limit_exceeded(span, limit),
Self::IntegerOverflow { operation } => integer_overflow(span, operation),
Self::UnexpectedValueType { expected, actual } => {
unexpected_value_type(span, &expected, &actual)
}
Expand Down Expand Up @@ -474,6 +486,7 @@ impl<'heap, A: Allocator> RuntimeError<'heap, !, A> {
Self::RecursionLimitExceeded { limit } => {
RuntimeError::RecursionLimitExceeded { limit }
}
Self::IntegerOverflow { operation } => RuntimeError::IntegerOverflow { operation },
Self::UnexpectedValueType { expected, actual } => {
RuntimeError::UnexpectedValueType { expected, actual }
}
Expand Down Expand Up @@ -808,3 +821,19 @@ fn recursion_limit_exceeded(span: SpanId, limit: usize) -> InterpretDiagnostic {

diagnostic
}

fn integer_overflow(span: SpanId, operation: &str) -> InterpretDiagnostic {
let mut diagnostic =
Diagnostic::new(InterpretDiagnosticCategory::RuntimeLimit, Severity::Error).primary(
Label::new(
span,
format!("integer {operation} produced a result outside the supported range"),
),
);

diagnostic.add_message(Message::note(
"the interpreter currently supports integers up to 128 bits",
));

diagnostic
}
34 changes: 25 additions & 9 deletions libs/@local/hashql/mir/src/interpret/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,15 @@ impl<'ctx, 'heap, A: Allocator + Clone> Runtime<'ctx, 'heap, A> {

match op {
BinOp::Add => match (lhs.as_ref(), rhs.as_ref()) {
(Value::Integer(lhs), Value::Integer(rhs)) => Ok(Value::from(lhs + rhs)),
(Value::Integer(lhs), Value::Number(rhs)) => Ok(Value::Number(lhs + rhs)),
(Value::Number(lhs), Value::Integer(rhs)) => Ok(Value::Number(lhs + rhs)),
(Value::Number(lhs), Value::Number(rhs)) => Ok(Value::Number(lhs + rhs)),
(Value::Integer(lhs), Value::Integer(rhs)) => lhs
.checked_add(*rhs)
.map(Value::Integer)
.ok_or(RuntimeError::IntegerOverflow {
operation: "addition",
}),
(Value::Integer(lhs), Value::Number(rhs)) => Ok(Value::Number(*lhs + *rhs)),
(Value::Number(lhs), Value::Integer(rhs)) => Ok(Value::Number(*lhs + *rhs)),
(Value::Number(lhs), Value::Number(rhs)) => Ok(Value::Number(*lhs + *rhs)),
_ => {
cold_path();

Expand All @@ -532,10 +537,15 @@ impl<'ctx, 'heap, A: Allocator + Clone> Runtime<'ctx, 'heap, A> {
}
},
BinOp::Sub => match (lhs.as_ref(), rhs.as_ref()) {
(Value::Integer(lhs), Value::Integer(rhs)) => Ok(Value::from(lhs - rhs)),
(Value::Integer(lhs), Value::Number(rhs)) => Ok(Value::Number(lhs - rhs)),
(Value::Number(lhs), Value::Integer(rhs)) => Ok(Value::Number(lhs - rhs)),
(Value::Number(lhs), Value::Number(rhs)) => Ok(Value::Number(lhs - rhs)),
(Value::Integer(lhs), Value::Integer(rhs)) => lhs
.checked_sub(*rhs)
.map(Value::Integer)
.ok_or(RuntimeError::IntegerOverflow {
operation: "subtraction",
}),
(Value::Integer(lhs), Value::Number(rhs)) => Ok(Value::Number(*lhs - *rhs)),
(Value::Number(lhs), Value::Integer(rhs)) => Ok(Value::Number(*lhs - *rhs)),
(Value::Number(lhs), Value::Number(rhs)) => Ok(Value::Number(*lhs - *rhs)),
_ => {
cold_path();

Expand Down Expand Up @@ -623,7 +633,13 @@ impl<'ctx, 'heap, A: Allocator + Clone> Runtime<'ctx, 'heap, A> {
}
},
UnOp::Neg => match operand.as_ref() {
Value::Integer(int) => Ok((-int).into()),
Value::Integer(int) => {
int.checked_neg()
.map(Value::Integer)
.ok_or(RuntimeError::IntegerOverflow {
operation: "negation",
})
}
Value::Number(number) => Ok(Value::Number(-number)),
Value::Unit
| Value::String(_)
Expand Down
57 changes: 57 additions & 0 deletions libs/@local/hashql/mir/src/interpret/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,63 @@ fn recursion_limit_exceeded() {
assert_eq!(result.category, InterpretDiagnosticCategory::RuntimeLimit);
}

#[test]
fn integer_add_overflow() {
let heap = Heap::new();
let interner = Interner::new(&heap);
let env = Environment::new(&heap);

let body = body!(interner, env; fn@0/0 -> Int {
decl result: Int;

bb0() {
result = bin.+ 170141183460469231731687303715884105727 1;
return result;
}
});

let result = run_body(body).expect_err("should fail with integer overflow");
assert_eq!(result.category, InterpretDiagnosticCategory::RuntimeLimit);
}

#[test]
fn integer_sub_overflow() {
let heap = Heap::new();
let interner = Interner::new(&heap);
let env = Environment::new(&heap);

let body = body!(interner, env; fn@0/0 -> Int {
decl result: Int;

bb0() {
result = bin.- (-0x8000_0000_0000_0000_0000_0000_0000_0000) 1;
return result;
}
});

let result = run_body(body).expect_err("should fail with integer overflow");
assert_eq!(result.category, InterpretDiagnosticCategory::RuntimeLimit);
}

#[test]
fn integer_neg_overflow() {
let heap = Heap::new();
let interner = Interner::new(&heap);
let env = Environment::new(&heap);

let body = body!(interner, env; fn@0/0 -> Int {
decl result: Int;

bb0() {
result = un.neg (-0x8000_0000_0000_0000_0000_0000_0000_0000);
return result;
}
});

let result = run_body(body).expect_err("should fail with integer overflow");
assert_eq!(result.category, InterpretDiagnosticCategory::RuntimeLimit);
}

#[test]
fn out_of_range_list_index() {
let heap = Heap::new();
Expand Down
Loading
Loading