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
32 changes: 10 additions & 22 deletions compiler/noirc_evaluator/src/ssa/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@
}
}

macro_rules! apply_int_binop {

Check warning on line 880 in compiler/noirc_evaluator/src/ssa/interpreter/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (binop)
($lhs:expr, $rhs:expr, $binary:expr, $f:expr) => {{
use value::NumericValue::*;
match ($lhs, $rhs) {
Expand Down Expand Up @@ -908,7 +908,7 @@
}};
}

macro_rules! apply_int_binop_opt {

Check warning on line 911 in compiler/noirc_evaluator/src/ssa/interpreter/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (binop)
($dfg:expr, $lhs:expr, $rhs:expr, $binary:expr, $f:expr) => {{
use value::NumericValue::*;

Expand Down Expand Up @@ -1008,7 +1008,7 @@
}));
}

// Disable this instruction if it is side-effectful and side effects are disabled.

Check warning on line 1011 in compiler/noirc_evaluator/src/ssa/interpreter/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (effectful)
if !self.side_effects_enabled() && binary.requires_acir_gen_predicate(self.dfg()) {
let zero = NumericValue::zero(lhs.get_type());
return Ok(Value::Numeric(zero));
Expand All @@ -1025,13 +1025,13 @@
let dfg = self.dfg();
let result = match binary.operator {
BinaryOp::Add { unchecked: false } => {
apply_int_binop_opt!(dfg, lhs, rhs, binary, num_traits::CheckedAdd::checked_add)

Check warning on line 1028 in compiler/noirc_evaluator/src/ssa/interpreter/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (binop)
}
BinaryOp::Add { unchecked: true } => {
apply_int_binop!(lhs, rhs, binary, num_traits::WrappingAdd::wrapping_add)

Check warning on line 1031 in compiler/noirc_evaluator/src/ssa/interpreter/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (binop)
}
BinaryOp::Sub { unchecked: false } => {
apply_int_binop_opt!(dfg, lhs, rhs, binary, num_traits::CheckedSub::checked_sub)

Check warning on line 1034 in compiler/noirc_evaluator/src/ssa/interpreter/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (binop)
}
BinaryOp::Sub { unchecked: true } => {
apply_int_binop!(lhs, rhs, binary, num_traits::WrappingSub::wrapping_sub)
Expand Down Expand Up @@ -1069,12 +1069,6 @@
}));
};

let overflow = || {
let instruction =
format!("`{}` ({lhs} << {rhs})", display_binary(binary, self.dfg()));
InterpreterError::Overflow { instruction }
};

let rhs = rhs as u32;
use NumericValue::*;
match lhs {
Expand All @@ -1084,22 +1078,16 @@
typ: "Field",
}));
}
U1(value) => {
if rhs == 0 {
U1(value)
} else {
return Err(overflow());
}
}
U8(value) => U8(value.checked_shl(rhs).ok_or_else(overflow)?),
U16(value) => U16(value.checked_shl(rhs).ok_or_else(overflow)?),
U32(value) => U32(value.checked_shl(rhs).ok_or_else(overflow)?),
U64(value) => U64(value.checked_shl(rhs).ok_or_else(overflow)?),
U128(value) => U128(value.checked_shl(rhs).ok_or_else(overflow)?),
I8(value) => I8(value.checked_shl(rhs).ok_or_else(overflow)?),
I16(value) => I16(value.checked_shl(rhs).ok_or_else(overflow)?),
I32(value) => I32(value.checked_shl(rhs).ok_or_else(overflow)?),
I64(value) => I64(value.checked_shl(rhs).ok_or_else(overflow)?),
U1(value) => U1(if rhs == 0 { value } else { false }),
U8(value) => U8(value.checked_shl(rhs).unwrap_or(0)),
U16(value) => U16(value.checked_shl(rhs).unwrap_or(0)),
U32(value) => U32(value.checked_shl(rhs).unwrap_or(0)),
U64(value) => U64(value.checked_shl(rhs).unwrap_or(0)),
U128(value) => U128(value.checked_shl(rhs).unwrap_or(0)),
I8(value) => I8(value.checked_shl(rhs).unwrap_or(0)),
I16(value) => I16(value.checked_shl(rhs).unwrap_or(0)),
I32(value) => I32(value.checked_shl(rhs).unwrap_or(0)),
I64(value) => I64(value.checked_shl(rhs).unwrap_or(0)),
}
}
BinaryOp::Shr => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ fn shl() {
assert_eq!(value, from_constant(12_u128.into(), NumericType::signed(8)));
}

/// shl should overflow if the rhs is greater than the bit count
/// shl does not error on overflow. It just returns zero.
#[test]
fn shl_overflow() {
let error = expect_error(
let value = expect_value(
"
acir(inline) fn main f0 {
b0():
Expand All @@ -311,7 +311,7 @@ fn shl_overflow() {
}
",
);
assert!(matches!(error, InterpreterError::Overflow { .. }));
assert_eq!(value, from_constant(0_u128.into(), NumericType::unsigned(8)));
}

#[test]
Expand All @@ -333,7 +333,7 @@ fn shr() {
}

#[test]
/// Unlike shl, shr does not error on overflow. It just returns 0. See https://github.com/noir-lang/noir/pull/7509.
/// shr does not error on overflow. It just returns 0. See https://github.com/noir-lang/noir/pull/7509.
fn shr_overflow() {
let value = expect_value(
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub(super) fn evaluate_infix(
(lhs_value as lhs ">>" rhs_value as rhs) => lhs.checked_shr(rhs.into()).or(Some(0))
},
BinaryOpKind::ShiftLeft => match_bitshift! {
(lhs_value as lhs "<<" rhs_value as rhs) => lhs.checked_shl(rhs.into())
(lhs_value as lhs "<<" rhs_value as rhs) => lhs.checked_shl(rhs.into()).or(Some(0))
},
BinaryOpKind::Modulo => match_integer! {
(lhs_value as lhs "%" rhs_value as rhs) => lhs.checked_rem(rhs)
Expand Down
Loading