diff --git a/compiler/noirc_evaluator/src/ssa/ir/dfg.rs b/compiler/noirc_evaluator/src/ssa/ir/dfg.rs index 870b5e602f1..d0745019cda 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/dfg.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/dfg.rs @@ -344,10 +344,16 @@ impl DataFlowGraph { pub(crate) fn get_value_max_num_bits(&self, value: ValueId) -> u32 { match self[value] { Value::Instruction { instruction, .. } => { + let bit_size = self.type_of_value(value).bit_size(); if let Instruction::Cast(original_value, _) = self[instruction] { - self.type_of_value(original_value).bit_size() + let original_bit_size = self.type_of_value(original_value).bit_size(); + if original_bit_size < bit_size { + original_bit_size + } else { + bit_size + } } else { - self.type_of_value(value).bit_size() + bit_size } } diff --git a/compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs b/compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs index e491807995b..c5b8e1c165f 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs @@ -174,6 +174,8 @@ impl Binary { if operand_type.is_unsigned() { // If we're comparing a variable against a constant value which lies outside of the range of // values which the variable's type can take, we can assume that the equality will be false. + // When checking the variable's max range we want to make sure to include type information + // from previous casts which tell us whether the variable was cast up from a smaller type. let constant = lhs.or(rhs); let non_constant = if lhs.is_some() { self.rhs } else { self.lhs }; if let Some(constant) = constant {