Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Numeric generics with impls error #1148

Merged
merged 1 commit into from
Apr 13, 2023
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
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