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
Changes from 2 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
32 changes: 16 additions & 16 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,7 @@
//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;
let idx = (z - 5*t - 5) as Field;
z = y[4];
//Test 1:
for i in 0..5 {
@@ -52,20 +52,20 @@ fn main(x: [u32; 5], y: [u32; 5], mut z: u32, t: u32) {
}
}

//dynamic array test - TODO uncomment the call below when activating dynamic arrays
//dyn_array(x, idx, idx - 3);
//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;
// }
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
@@ -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 {
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
@@ -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);