noir icon indicating copy to clipboard operation
noir copied to clipboard

Use checked arithmetic in comptime constants

Open michaeljklein opened this issue 8 months ago • 0 comments

Problem

We currently don't use checked arithmetic e.g. when evaluating array lengths or other compile time constants:

// try_eval_array_length_id_with_fuel
                match infix.operator.kind {
                    BinaryOpKind::Add => Ok(lhs + rhs),
                    BinaryOpKind::Subtract => Ok(lhs - rhs),
                    BinaryOpKind::Multiply => Ok(lhs * rhs),
                    BinaryOpKind::Divide => Ok(lhs / rhs),
                    BinaryOpKind::Equal => Ok((lhs == rhs) as u128),
                    BinaryOpKind::NotEqual => Ok((lhs != rhs) as u128),
                    BinaryOpKind::Less => Ok((lhs < rhs) as u128),
                    BinaryOpKind::LessEqual => Ok((lhs <= rhs) as u128),
                    BinaryOpKind::Greater => Ok((lhs > rhs) as u128),
                    BinaryOpKind::GreaterEqual => Ok((lhs >= rhs) as u128),
                    BinaryOpKind::And => Ok(lhs & rhs),
                    BinaryOpKind::Or => Ok(lhs | rhs),
                    BinaryOpKind::Xor => Ok(lhs ^ rhs),
                    BinaryOpKind::ShiftRight => Ok(lhs >> rhs),
                    BinaryOpKind::ShiftLeft => Ok(lhs << rhs),
                    BinaryOpKind::Modulo => Ok(lhs % rhs),
                }

Happy Case

Use checked operations and throw a proper error instead of panicking

Project Impact

Nice-to-have

Impact Context

No response

Workaround

None

Workaround Description

Line-by-line debugging and accepting the panics as the expected errors

Additional Context

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

michaeljklein avatar Jun 03 '24 13:06 michaeljklein