Skip to content

Commit

Permalink
feat: enable dynamic arrays (#1271)
Browse files Browse the repository at this point in the history
* enable dynamic arrays

* code review: add a new test for dynamic arrays
  • Loading branch information
guipublic authored May 3, 2023
1 parent 144ebf5 commit 9f43450
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 51 deletions.
17 changes: 0 additions & 17 deletions crates/nargo_cli/tests/test_data/6_array/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//Basic tests for arrays
fn main(x: [u32; 5], y: [u32; 5], mut z: u32, t: u32) {
let mut c = 2301;
let _idx = (z - 5*t - 5) as Field;
z = y[4];
//Test 1:
for i in 0..5 {
Expand Down Expand Up @@ -51,21 +50,5 @@ fn main(x: [u32; 5], y: [u32; 5], mut z: u32, t: u32) {
assert(x_elem != y_elem);
}
}

//dynamic array test - TODO uncomment the call below when activating dynamic arrays
//dyn_array(x, idx, idx - 3);
}

// fn dyn_array(mut x: [u32; 5], y: Field, z: Field) {
// assert(x[y] == 111);
// assert(x[z] == 101);
// x[z] = 0;
// assert(x[y] == 111);
// assert(x[1] == 0);
// if y as u32 < 10 {
// x[y] = x[y] - 2;
// } else {
// x[y] = 0;
// }
// assert(x[4] == 109);
// }
5 changes: 5 additions & 0 deletions crates/nargo_cli/tests/test_data/array_dynamic/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
5 changes: 5 additions & 0 deletions crates/nargo_cli/tests/test_data/array_dynamic/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x = [104, 101, 108, 108, 111]
z = "59"
t = "10"


20 changes: 20 additions & 0 deletions crates/nargo_cli/tests/test_data/array_dynamic/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

fn main(x: [u32; 5], mut z: u32, t: u32) {
let idx = (z - 5*t - 5) as Field;
//dynamic array test
dyn_array(x, idx, idx - 3);
}

fn dyn_array(mut x: [u32; 5], y: Field, z: Field) {
constrain x[y] == 111;
constrain x[z] == 101;
x[z] = 0;
constrain x[y] == 111;
constrain x[1] == 0;
if y as u32 < 10 {
x[y] = x[y] - 2;
} else {
x[y] = 0;
}
constrain x[4] == 109;
}
26 changes: 5 additions & 21 deletions crates/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,29 +252,13 @@ impl<'interner> TypeChecker<'interner> {
let index_type = self.check_expression(&index_expr.index);
let span = self.interner.expr_span(&index_expr.index);

self.unify(&index_type, &Type::comp_time(Some(span)), span, || {
// Specialize the error in the case the user has a Field, just not a `comptime` one.
if matches!(index_type, Type::FieldElement(..)) {
TypeCheckError::Unstructured {
msg: format!("Array index must be known at compile-time, but here a non-comptime {index_type} was used instead"),
span,
}
} else {
TypeCheckError::TypeMismatch {
expected_typ: "comptime Field".to_owned(),
expr_typ: index_type.to_string(),
expr_span: span,
}
index_type.make_subtype_of(&Type::field(Some(span)), span, &mut self.errors, || {
TypeCheckError::TypeMismatch {
expected_typ: "Field".to_owned(),
expr_typ: index_type.to_string(),
expr_span: span,
}
});
// TODO: replace the above by the below in order to activate dynamic arrays
// index_type.make_subtype_of(&Type::field(Some(span)), span, errors, || {
// TypeCheckError::TypeMismatch {
// expected_typ: "Field".to_owned(),
// expr_typ: index_type.to_string(),
// expr_span: span,
// }
// });

let lhs_type = self.check_expression(&index_expr.collection);
match lhs_type {
Expand Down
21 changes: 8 additions & 13 deletions crates/noirc_frontend/src/hir/type_check/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,16 @@ impl<'interner> TypeChecker<'interner> {
let index_type = self.check_expression(&index);
let expr_span = self.interner.expr_span(&index);

self.unify(&index_type, &Type::comp_time(Some(expr_span)), expr_span, || {
TypeCheckError::TypeMismatch {
expected_typ: "comptime Field".to_owned(),
index_type.make_subtype_of(
&Type::field(Some(expr_span)),
expr_span,
&mut self.errors,
|| TypeCheckError::TypeMismatch {
expected_typ: "Field".to_owned(),
expr_typ: index_type.to_string(),
expr_span,
}
});
//TODO replace the above by the below in order to activate dynamic arrays
// index_type.make_subtype_of(&Type::field(Some(expr_span)), expr_span, || {
// TypeCheckError::TypeMismatch {
// expected_typ: "Field".to_owned(),
// expr_typ: index_type.to_string(),
// expr_span,
// }
// });
},
);

let (result, array) = self.check_lvalue(*array, assign_span);
let array = Box::new(array);
Expand Down

0 comments on commit 9f43450

Please sign in to comment.