Skip to content

Commit 4a5d2de

Browse files
authoredAug 7, 2023
fix: simplification of overflowing integer operations (#2153)
Fix simplification of overflowing integer operations
1 parent 478c026 commit 4a5d2de

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed
 

‎crates/nargo_cli/tests/execution_success/4_sub/src/main.nr

+4
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
fn main(mut x: u32, y: u32, z: u32) {
33
x -= y;
44
assert(x == z);
5+
6+
// Test constant underflow (regression for #2045)
7+
let x = -1 as u4;
8+
assert(x == 15);
59
}

‎crates/noirc_evaluator/src/ssa/ir/instruction.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ impl Binary {
653653

654654
let lhs = truncate(lhs.try_into_u128()?, *bit_size);
655655
let rhs = truncate(rhs.try_into_u128()?, *bit_size);
656-
let result = function(lhs, rhs)?;
656+
let result = function(lhs, rhs);
657657
truncate(result, *bit_size).into()
658658
}
659659
_ => return None,
@@ -689,18 +689,18 @@ impl BinaryOp {
689689
}
690690
}
691691

692-
fn get_u128_function(self) -> fn(u128, u128) -> Option<u128> {
692+
fn get_u128_function(self) -> fn(u128, u128) -> u128 {
693693
match self {
694-
BinaryOp::Add => u128::checked_add,
695-
BinaryOp::Sub => u128::checked_sub,
696-
BinaryOp::Mul => u128::checked_mul,
697-
BinaryOp::Div => u128::checked_div,
698-
BinaryOp::Mod => u128::checked_rem,
699-
BinaryOp::And => |x, y| Some(x & y),
700-
BinaryOp::Or => |x, y| Some(x | y),
701-
BinaryOp::Xor => |x, y| Some(x ^ y),
702-
BinaryOp::Eq => |x, y| Some((x == y) as u128),
703-
BinaryOp::Lt => |x, y| Some((x < y) as u128),
694+
BinaryOp::Add => u128::wrapping_add,
695+
BinaryOp::Sub => u128::wrapping_sub,
696+
BinaryOp::Mul => u128::wrapping_mul,
697+
BinaryOp::Div => u128::wrapping_div,
698+
BinaryOp::Mod => u128::wrapping_rem,
699+
BinaryOp::And => |x, y| x & y,
700+
BinaryOp::Or => |x, y| x | y,
701+
BinaryOp::Xor => |x, y| x ^ y,
702+
BinaryOp::Eq => |x, y| (x == y) as u128,
703+
BinaryOp::Lt => |x, y| (x < y) as u128,
704704
}
705705
}
706706
}

0 commit comments

Comments
 (0)