Skip to content

Commit

Permalink
fix: Numeric generics with impls error (#1148)
Browse files Browse the repository at this point in the history
numeric generic Error reporting change for type rules
  • Loading branch information
vezenovm authored Apr 13, 2023
1 parent 7528f59 commit 5d6e4d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
13 changes: 12 additions & 1 deletion crates/nargo_cli/tests/test_data/numeric_generics/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ fn id<I>(x: [Field; I]) -> [Field; I] {
}

struct MyStruct<S> {
data: [Field; S]
data: [Field; S],
}

impl<S> MyStruct<S> {
fn insert(mut self: Self, index: comptime Field, elem: Field) -> Self {
// Regression test for numeric generics on impls
constrain index as u64 < S as u64;

self.data[index] = elem;
self
}
}

fn foo(mut s: MyStruct<2+1>) -> MyStruct<10/2-2> {
s.data[0] = s.data[0] + 1;
s
}

11 changes: 6 additions & 5 deletions crates/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ impl<'interner> TypeChecker<'interner> {
use crate::BinaryOpKind::{Equal, NotEqual};
use Type::*;
match (lhs_type, rhs_type) {
// Avoid reporting errors multiple times
(Error, _) | (_,Error) => Ok(Bool(CompTime::Yes(None))),

// Matches on PolymorphicInteger and TypeVariable must be first to follow any type
// bindings.
(PolymorphicInteger(comptime, int), other)
Expand Down Expand Up @@ -572,9 +575,6 @@ impl<'interner> TypeChecker<'interner> {
Ok(Bool(comptime))
}

// Avoid reporting errors multiple times
(Error, _) | (_,Error) => Ok(Bool(CompTime::Yes(None))),

// Special-case == and != for arrays
(Array(x_size, x_type), Array(y_size, y_type)) if matches!(op.kind, Equal | NotEqual) => {
x_type.unify(y_type, op.location.span, &mut self.errors, || {
Expand Down Expand Up @@ -728,6 +728,9 @@ impl<'interner> TypeChecker<'interner> {

use Type::*;
match (lhs_type, rhs_type) {
// An error type on either side will always return an error
(Error, _) | (_,Error) => Ok(Error),

// Matches on PolymorphicInteger and TypeVariable must be first so that we follow any type
// bindings.
(PolymorphicInteger(comptime, int), other)
Expand Down Expand Up @@ -793,8 +796,6 @@ impl<'interner> TypeChecker<'interner> {
(Struct(..), _) | (_, Struct(..)) => Err(make_error("Structs cannot be used in an infix operation".to_string())),
(Tuple(_), _) | (_, Tuple(_)) => Err(make_error("Tuples cannot be used in an infix operation".to_string())),

// An error type on either side will always return an error
(Error, _) | (_,Error) => Ok(Error),
(Unit, _) | (_,Unit) => Ok(Unit),

// The result of two Fields is always a witness
Expand Down

0 comments on commit 5d6e4d0

Please sign in to comment.