diff --git a/crates/nargo_cli/tests/test_data/6_array/src/main.nr b/crates/nargo_cli/tests/test_data/6_array/src/main.nr index 30d3ab5a22f..9593c56524f 100644 --- a/crates/nargo_cli/tests/test_data/6_array/src/main.nr +++ b/crates/nargo_cli/tests/test_data/6_array/src/main.nr @@ -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 { @@ -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); -// } diff --git a/crates/nargo_cli/tests/test_data/array_dynamic/Nargo.toml b/crates/nargo_cli/tests/test_data/array_dynamic/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data/array_dynamic/Nargo.toml @@ -0,0 +1,5 @@ +[package] +authors = [""] +compiler_version = "0.1" + +[dependencies] \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data/array_dynamic/Prover.toml b/crates/nargo_cli/tests/test_data/array_dynamic/Prover.toml new file mode 100644 index 00000000000..1e652e28d1c --- /dev/null +++ b/crates/nargo_cli/tests/test_data/array_dynamic/Prover.toml @@ -0,0 +1,5 @@ +x = [104, 101, 108, 108, 111] +z = "59" +t = "10" + + diff --git a/crates/nargo_cli/tests/test_data/array_dynamic/src/main.nr b/crates/nargo_cli/tests/test_data/array_dynamic/src/main.nr new file mode 100644 index 00000000000..73dc162eb1b --- /dev/null +++ b/crates/nargo_cli/tests/test_data/array_dynamic/src/main.nr @@ -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; +} \ No newline at end of file diff --git a/crates/noirc_frontend/src/hir/type_check/expr.rs b/crates/noirc_frontend/src/hir/type_check/expr.rs index aba44e36d2c..1929af8d223 100644 --- a/crates/noirc_frontend/src/hir/type_check/expr.rs +++ b/crates/noirc_frontend/src/hir/type_check/expr.rs @@ -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 { diff --git a/crates/noirc_frontend/src/hir/type_check/stmt.rs b/crates/noirc_frontend/src/hir/type_check/stmt.rs index c5ad7011414..ccb35070a36 100644 --- a/crates/noirc_frontend/src/hir/type_check/stmt.rs +++ b/crates/noirc_frontend/src/hir/type_check/stmt.rs @@ -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);