Skip to content

Commit

Permalink
fix: add ne method to Eq trait
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Dec 21, 2023
1 parent 1ffe9f3 commit 3f4de20
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
36 changes: 29 additions & 7 deletions compiler/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,35 @@ impl<'interner> TypeChecker<'interner> {

if matches!(lhs_type, Type::Array(_, _) | Type::Struct(_, _)) {
// Replace with call to type's implementation of the `Eq` trait
let method_call = HirExpression::MethodCall(HirMethodCallExpression {
method: "eq".into(),
object: infix_expr.lhs,
arguments: vec![infix_expr.rhs],
location: self.interner.expr_location(expr_id),
});
self.interner.replace_expr(expr_id, method_call);
let method_call = match infix_expr.operator.kind {
BinaryOpKind::Equal => HirMethodCallExpression {
method: "eq".into(),
object: infix_expr.lhs,
arguments: vec![infix_expr.rhs],
location: self.interner.expr_location(expr_id),
},
BinaryOpKind::Add => todo!(),
BinaryOpKind::Subtract => todo!(),
BinaryOpKind::Multiply => todo!(),
BinaryOpKind::Divide => todo!(),
BinaryOpKind::NotEqual => HirMethodCallExpression {
method: "ne".into(),
object: infix_expr.lhs,
arguments: vec![infix_expr.rhs],
location: self.interner.expr_location(expr_id),
},
BinaryOpKind::Less => todo!(),
BinaryOpKind::LessEqual => todo!(),
BinaryOpKind::Greater => todo!(),
BinaryOpKind::GreaterEqual => todo!(),
BinaryOpKind::And => todo!(),
BinaryOpKind::Or => todo!(),
BinaryOpKind::Xor => todo!(),
BinaryOpKind::ShiftRight => todo!(),
BinaryOpKind::ShiftLeft => todo!(),
BinaryOpKind::Modulo => todo!(),
};
self.interner.replace_expr(expr_id, HirExpression::MethodCall(method_call));

// We want to convert this method call into a pure function call so need to check again.
self.check_expression(expr_id)
Expand Down
5 changes: 4 additions & 1 deletion noir_stdlib/src/ops.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

trait Add {
fn add(self, other: Self) -> Self;
}
Expand Down Expand Up @@ -65,6 +64,10 @@ impl Div for i64 { fn div(self, other: i64) -> i64 { self / other } }

trait Eq {
fn eq(self, other: Self) -> bool;

fn ne(self, other: Self) -> bool {
!Eq::eq(self, other)
}
}

impl Eq for Field { fn eq(self, other: Field) -> bool { self == other } }
Expand Down

0 comments on commit 3f4de20

Please sign in to comment.