Skip to content

Commit

Permalink
Merge branch 'master' into constant-brillig-execution
Browse files Browse the repository at this point in the history
* master:
  fix: Set location before cast instructions in SSA (#2202)
  fix: simplification of overflowing integer operations (#2153)
  • Loading branch information
TomAFrench committed Aug 7, 2023
2 parents e6ee884 + a72cc96 commit 24e52f6
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
4 changes: 4 additions & 0 deletions crates/nargo_cli/tests/execution_success/4_sub/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
fn main(mut x: u32, y: u32, z: u32) {
x -= y;
assert(x == z);

// Test constant underflow (regression for #2045)
let x = -1 as u4;
assert(x == 15);
}
2 changes: 1 addition & 1 deletion crates/noirc_evaluator/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum RuntimeError {
InternalError(#[from] InternalError),
#[error("Index out of bounds, array has size {index:?}, but index was {array_size:?}")]
IndexOutOfBounds { index: usize, array_size: usize, location: Option<Location> },
#[error("All Witnesses are by default u{num_bits:?} Applying this type does not apply any constraints.\n We also currently do not allow integers of size more than {num_bits:?}, this will be handled by BigIntegers.")]
#[error("Range constraint of {num_bits} bits is too large for the Field size")]
InvalidRangeConstraint { num_bits: u32, location: Option<Location> },
#[error("Expected array index to fit into a u64")]
TypeConversion { from: String, into: String, location: Option<Location> },
Expand Down
24 changes: 12 additions & 12 deletions crates/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ impl Binary {

let lhs = truncate(lhs.try_into_u128()?, *bit_size);
let rhs = truncate(rhs.try_into_u128()?, *bit_size);
let result = function(lhs, rhs)?;
let result = function(lhs, rhs);
truncate(result, *bit_size).into()
}
_ => return None,
Expand Down Expand Up @@ -689,18 +689,18 @@ impl BinaryOp {
}
}

fn get_u128_function(self) -> fn(u128, u128) -> Option<u128> {
fn get_u128_function(self) -> fn(u128, u128) -> u128 {
match self {
BinaryOp::Add => u128::checked_add,
BinaryOp::Sub => u128::checked_sub,
BinaryOp::Mul => u128::checked_mul,
BinaryOp::Div => u128::checked_div,
BinaryOp::Mod => u128::checked_rem,
BinaryOp::And => |x, y| Some(x & y),
BinaryOp::Or => |x, y| Some(x | y),
BinaryOp::Xor => |x, y| Some(x ^ y),
BinaryOp::Eq => |x, y| Some((x == y) as u128),
BinaryOp::Lt => |x, y| Some((x < y) as u128),
BinaryOp::Add => u128::wrapping_add,
BinaryOp::Sub => u128::wrapping_sub,
BinaryOp::Mul => u128::wrapping_mul,
BinaryOp::Div => u128::wrapping_div,
BinaryOp::Mod => u128::wrapping_rem,
BinaryOp::And => |x, y| x & y,
BinaryOp::Or => |x, y| x | y,
BinaryOp::Xor => |x, y| x ^ y,
BinaryOp::Eq => |x, y| (x == y) as u128,
BinaryOp::Lt => |x, y| (x < y) as u128,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ impl<'a> FunctionContext<'a> {
fn codegen_cast(&mut self, cast: &ast::Cast) -> Values {
let lhs = self.codegen_non_tuple_expression(&cast.lhs);
let typ = Self::convert_non_tuple_type(&cast.r#type);
self.builder.set_location(cast.location);
self.builder.insert_cast(lhs, typ).into()
}

Expand Down
1 change: 1 addition & 0 deletions crates/noirc_frontend/src/monomorphization/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub struct If {
pub struct Cast {
pub lhs: Box<Expression>,
pub r#type: Type,
pub location: Location,
}

#[derive(Debug, Clone)]
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ impl<'interner> Monomorphizer<'interner> {
HirExpression::Cast(cast) => ast::Expression::Cast(ast::Cast {
lhs: Box::new(self.expr(cast.lhs)),
r#type: Self::convert_type(&cast.r#type),
location: self.interner.expr_location(&expr),
}),

HirExpression::For(for_expr) => {
Expand Down

0 comments on commit 24e52f6

Please sign in to comment.