Skip to content
Merged
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
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir/type_check/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub enum TypeCheckError {
location: Location,
},
#[error("Evaluating `{op}` on `{lhs}`, `{rhs}` failed")]
FailingBinaryOp { op: BinaryTypeOperator, lhs: i128, rhs: i128, location: Location },
FailingBinaryOp { op: BinaryTypeOperator, lhs: String, rhs: String, location: Location },
#[error("Type {typ:?} cannot be used in a {place:?}")]
TypeCannotBeUsed { typ: Type, place: &'static str, location: Location },
#[error("Expected type {expected_typ:?} is not the same as {expr_typ:?}")]
Expand Down
56 changes: 42 additions & 14 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2637,20 +2637,48 @@ impl BinaryTypeOperator {
Err(TypeCheckError::ModuloOnFields { lhs: a, rhs: b, location })
}
},
Some(_maximum_size) => {
let a = a.to_i128();
let b = b.to_i128();

let err = TypeCheckError::FailingBinaryOp { op: self, lhs: a, rhs: b, location };
let result = match self {
BinaryTypeOperator::Addition => a.checked_add(b).ok_or(err)?,
BinaryTypeOperator::Subtraction => a.checked_sub(b).ok_or(err)?,
BinaryTypeOperator::Multiplication => a.checked_mul(b).ok_or(err)?,
BinaryTypeOperator::Division => a.checked_div(b).ok_or(err)?,
BinaryTypeOperator::Modulo => a.checked_rem(b).ok_or(err)?,
};

Ok(result.into())
Some(maximum_size) => {
if maximum_size.to_u128() == u128::MAX {
// For u128 operations we need to use u128
let a = a.to_u128();
let b = b.to_u128();

let err = TypeCheckError::FailingBinaryOp {
op: self,
lhs: a.to_string(),
rhs: b.to_string(),
location,
};
let result = match self {
BinaryTypeOperator::Addition => a.checked_add(b).ok_or(err)?,
BinaryTypeOperator::Subtraction => a.checked_sub(b).ok_or(err)?,
BinaryTypeOperator::Multiplication => a.checked_mul(b).ok_or(err)?,
BinaryTypeOperator::Division => a.checked_div(b).ok_or(err)?,
BinaryTypeOperator::Modulo => a.checked_rem(b).ok_or(err)?,
};

Ok(result.into())
} else {
// Every other type first in i128, allowing both positive and negative values
let a = a.to_i128();
let b = b.to_i128();

let err = TypeCheckError::FailingBinaryOp {
op: self,
lhs: a.to_string(),
rhs: b.to_string(),
location,
};
let result = match self {
BinaryTypeOperator::Addition => a.checked_add(b).ok_or(err)?,
BinaryTypeOperator::Subtraction => a.checked_sub(b).ok_or(err)?,
BinaryTypeOperator::Multiplication => a.checked_mul(b).ok_or(err)?,
BinaryTypeOperator::Division => a.checked_div(b).ok_or(err)?,
BinaryTypeOperator::Modulo => a.checked_rem(b).ok_or(err)?,
};

Ok(result.into())
}
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions compiler/noirc_frontend/src/tests/arithmetic_generics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg(test)]

use core::panic;

use acvm::{AcirField, FieldElement};

use crate::assert_no_errors;
Expand Down Expand Up @@ -93,10 +95,12 @@ fn arithmetic_generics_checked_cast_zeros() {
}
_ => panic!("unexpected length: {length:?}"),
}
assert!(matches!(
err,
TypeCheckError::FailingBinaryOp { op: BinaryTypeOperator::Modulo, lhs: 0, rhs: 0, .. }
));
let TypeCheckError::FailingBinaryOp { op, lhs, rhs, .. } = err else {
panic!("Expected FailingBinaryOp, but found: {err:?}");
};
assert_eq!(op, &BinaryTypeOperator::Modulo);
assert_eq!(lhs, "0");
assert_eq!(rhs, "0");
} else {
panic!("unexpected error: {monomorphization_error:?}");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "comptime_u128_ops"
type = "bin"
authors = [""]

[dependencies]
19 changes: 19 additions & 0 deletions test_programs/compile_success_empty/comptime_u128_ops/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
fn main() {
comptime {
let y = foo::<340282366920938463463374607431768211455, 1>();
assert_eq(y.n(), 340282366920938463463374607431768211454);
}
}

struct T<let N: u128> {}

impl<let N: u128> T<N> {
fn n(self) -> u128 {
let _ = self;
N
}
}

fn foo<let x: u128, let y: u128>() -> T<x - y> {
T {}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading