From 5a0b6309505bf68559c1ae282c29c34c35f5c84b Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 6 Jul 2023 15:02:09 +0000 Subject: [PATCH 1/3] feat: make array indexes poly int --- .../integer_array_indexing/Nargo.toml | 5 +++++ .../integer_array_indexing/Prover.toml | 2 ++ .../integer_array_indexing/src/main.nr | 14 ++++++++++++++ crates/noirc_frontend/src/hir/type_check/expr.rs | 4 ++-- crates/noirc_frontend/src/hir/type_check/stmt.rs | 6 +++--- 5 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/integer_array_indexing/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/integer_array_indexing/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data_ssa_refactor/integer_array_indexing/src/main.nr diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/integer_array_indexing/Nargo.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/integer_array_indexing/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/integer_array_indexing/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_ssa_refactor/integer_array_indexing/Prover.toml b/crates/nargo_cli/tests/test_data_ssa_refactor/integer_array_indexing/Prover.toml new file mode 100644 index 00000000000..1496028f60a --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/integer_array_indexing/Prover.toml @@ -0,0 +1,2 @@ +arr = [1, 2, 3] +x = 2 diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/integer_array_indexing/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/integer_array_indexing/src/main.nr new file mode 100644 index 00000000000..e89e2ffbb80 --- /dev/null +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/integer_array_indexing/src/main.nr @@ -0,0 +1,14 @@ +use dep::std; + +global ARRAY_LEN: u32 = 3; + +fn main(arr: [Field; ARRAY_LEN], x: u32) -> pub Field { + + let mut value = arr[ARRAY_LEN - 1]; + + value += arr[0 as u32]; + value += arr[1 as Field]; + + value + (x as Field) + +} diff --git a/crates/noirc_frontend/src/hir/type_check/expr.rs b/crates/noirc_frontend/src/hir/type_check/expr.rs index 03f4ef09a92..0b5c892c343 100644 --- a/crates/noirc_frontend/src/hir/type_check/expr.rs +++ b/crates/noirc_frontend/src/hir/type_check/expr.rs @@ -307,9 +307,9 @@ impl<'interner> TypeChecker<'interner> { let index_type = self.check_expression(&index_expr.index); let span = self.interner.expr_span(&index_expr.index); - index_type.make_subtype_of(&Type::field(Some(span)), span, &mut self.errors, || { + index_type.unify(&Type::polymorphic_integer(self.interner), span, &mut self.errors, || { TypeCheckError::TypeMismatch { - expected_typ: "Field".to_owned(), + expected_typ: "Integer".to_owned(), expr_typ: index_type.to_string(), expr_span: span, } diff --git a/crates/noirc_frontend/src/hir/type_check/stmt.rs b/crates/noirc_frontend/src/hir/type_check/stmt.rs index 5606547a568..981e73f291c 100644 --- a/crates/noirc_frontend/src/hir/type_check/stmt.rs +++ b/crates/noirc_frontend/src/hir/type_check/stmt.rs @@ -177,12 +177,12 @@ impl<'interner> TypeChecker<'interner> { let index_type = self.check_expression(&index); let expr_span = self.interner.expr_span(&index); - index_type.make_subtype_of( - &Type::field(Some(expr_span)), + index_type.unify( + &Type::polymorphic_integer(self.interner), expr_span, &mut self.errors, || TypeCheckError::TypeMismatch { - expected_typ: "Field".to_owned(), + expected_typ: "Integer".to_owned(), expr_typ: index_type.to_string(), expr_span, }, From b1595d79dcb1862ec0adbf7a5955bb50c7c6188e Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 6 Jul 2023 17:13:23 +0200 Subject: [PATCH 2/3] Update crates/noirc_frontend/src/hir/type_check/expr.rs --- crates/noirc_frontend/src/hir/type_check/expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/noirc_frontend/src/hir/type_check/expr.rs b/crates/noirc_frontend/src/hir/type_check/expr.rs index 0b5c892c343..b310215a2f4 100644 --- a/crates/noirc_frontend/src/hir/type_check/expr.rs +++ b/crates/noirc_frontend/src/hir/type_check/expr.rs @@ -309,7 +309,7 @@ impl<'interner> TypeChecker<'interner> { index_type.unify(&Type::polymorphic_integer(self.interner), span, &mut self.errors, || { TypeCheckError::TypeMismatch { - expected_typ: "Integer".to_owned(), + expected_typ: "an integer".to_owned(), expr_typ: index_type.to_string(), expr_span: span, } From 6a5b6590cf222badb1291cf43ae20f5933162b11 Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 6 Jul 2023 17:13:30 +0200 Subject: [PATCH 3/3] Update crates/noirc_frontend/src/hir/type_check/stmt.rs --- crates/noirc_frontend/src/hir/type_check/stmt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/noirc_frontend/src/hir/type_check/stmt.rs b/crates/noirc_frontend/src/hir/type_check/stmt.rs index 981e73f291c..a6d7442d98a 100644 --- a/crates/noirc_frontend/src/hir/type_check/stmt.rs +++ b/crates/noirc_frontend/src/hir/type_check/stmt.rs @@ -182,7 +182,7 @@ impl<'interner> TypeChecker<'interner> { expr_span, &mut self.errors, || TypeCheckError::TypeMismatch { - expected_typ: "Integer".to_owned(), + expected_typ: "an integer".to_owned(), expr_typ: index_type.to_string(), expr_span, },