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

feat: enable dynamic arrays #1271

Merged
merged 4 commits into from
May 3, 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
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