From 7e876ac1f9cbcdca2b51038db4137fabffe9dbaa Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Fri, 23 Aug 2024 15:32:56 +0000 Subject: [PATCH 1/5] nested dynamic array initialization --- .../src/ssa/acir_gen/acir_ir/acir_variable.rs | 18 ++++++++++++++++-- .../Nargo.toml | 7 +++++++ .../Prover.toml | 2 ++ .../src/main.nr | 13 +++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 test_programs/execution_success/nested_dyn_array_regression_5782/Nargo.toml create mode 100644 test_programs/execution_success/nested_dyn_array_regression_5782/Prover.toml create mode 100644 test_programs/execution_success/nested_dyn_array_regression_5782/src/main.nr diff --git a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs index 0cae7d77448..548e60a0946 100644 --- a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs +++ b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs @@ -1933,6 +1933,12 @@ impl AcirContext { } Some(optional_value) => { let mut values = Vec::new(); + match optional_value { + AcirValue::DynamicArray(_) => { + unreachable!("Dynamic array should already be initialized"); + } + _ => {}, + } self.initialize_array_inner(&mut values, optional_value)?; values } @@ -1962,8 +1968,16 @@ impl AcirContext { self.initialize_array_inner(witnesses, value)?; } } - AcirValue::DynamicArray(_) => { - unreachable!("Dynamic array should already be initialized"); + AcirValue::DynamicArray(AcirDynamicArray { block_id, len, value_types, element_type_sizes }) => { + let dynamic_array_values = try_vecmap(0..len, |i| { + let index_var = self.add_constant(i); + + let read = self.read_from_memory(block_id, &index_var)?; + Ok::(AcirValue::Var(read, AcirType::field())) + })?; + for value in dynamic_array_values { + self.initialize_array_inner(witnesses, value)?; + } } } Ok(()) diff --git a/test_programs/execution_success/nested_dyn_array_regression_5782/Nargo.toml b/test_programs/execution_success/nested_dyn_array_regression_5782/Nargo.toml new file mode 100644 index 00000000000..b5cdd19e186 --- /dev/null +++ b/test_programs/execution_success/nested_dyn_array_regression_5782/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "nested_dyn_array_regression_5782" +type = "bin" +authors = [""] +compiler_version = ">=0.33.0" + +[dependencies] \ No newline at end of file diff --git a/test_programs/execution_success/nested_dyn_array_regression_5782/Prover.toml b/test_programs/execution_success/nested_dyn_array_regression_5782/Prover.toml new file mode 100644 index 00000000000..de2960def06 --- /dev/null +++ b/test_programs/execution_success/nested_dyn_array_regression_5782/Prover.toml @@ -0,0 +1,2 @@ +array = [5, 10] +i = 1 diff --git a/test_programs/execution_success/nested_dyn_array_regression_5782/src/main.nr b/test_programs/execution_success/nested_dyn_array_regression_5782/src/main.nr new file mode 100644 index 00000000000..86dbf5416c1 --- /dev/null +++ b/test_programs/execution_success/nested_dyn_array_regression_5782/src/main.nr @@ -0,0 +1,13 @@ +fn main(mut array: [Field; 2], i: u32) { + assert_eq(array[i - 1], 5); + assert_eq(array[i], 10); + + array[i] = 2; + + let array2 = [array, array]; + + assert_eq(array2[0][0], 5); + assert_eq(array2[0][i], 2); + assert_eq(array2[i][0], 5); + assert_eq(array2[i][i], 2); +} \ No newline at end of file From 0e4a95ea062dcfc269637cc42b705d3b3233ccd4 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Fri, 23 Aug 2024 15:34:37 +0000 Subject: [PATCH 2/5] fmt --- .../src/ssa/acir_gen/acir_ir/acir_variable.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs index 548e60a0946..7c5393f3704 100644 --- a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs +++ b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs @@ -1937,7 +1937,7 @@ impl AcirContext { AcirValue::DynamicArray(_) => { unreachable!("Dynamic array should already be initialized"); } - _ => {}, + _ => {} } self.initialize_array_inner(&mut values, optional_value)?; values @@ -1968,10 +1968,15 @@ impl AcirContext { self.initialize_array_inner(witnesses, value)?; } } - AcirValue::DynamicArray(AcirDynamicArray { block_id, len, value_types, element_type_sizes }) => { + AcirValue::DynamicArray(AcirDynamicArray { + block_id, + len, + value_types, + element_type_sizes, + }) => { let dynamic_array_values = try_vecmap(0..len, |i| { let index_var = self.add_constant(i); - + let read = self.read_from_memory(block_id, &index_var)?; Ok::(AcirValue::Var(read, AcirType::field())) })?; From 95adedc9124d35458231caaa282e57db4efcd514 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Fri, 23 Aug 2024 15:34:49 +0000 Subject: [PATCH 3/5] nargo fmt --- .../nested_dyn_array_regression_5782/src/main.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_programs/execution_success/nested_dyn_array_regression_5782/src/main.nr b/test_programs/execution_success/nested_dyn_array_regression_5782/src/main.nr index 86dbf5416c1..b6a1238a9de 100644 --- a/test_programs/execution_success/nested_dyn_array_regression_5782/src/main.nr +++ b/test_programs/execution_success/nested_dyn_array_regression_5782/src/main.nr @@ -10,4 +10,4 @@ fn main(mut array: [Field; 2], i: u32) { assert_eq(array2[0][i], 2); assert_eq(array2[i][0], 5); assert_eq(array2[i][i], 2); -} \ No newline at end of file +} From 2153087b877452a70d611e3ff48a63dc368ea088 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Fri, 23 Aug 2024 15:37:00 +0000 Subject: [PATCH 4/5] use if let not match --- .../src/ssa/acir_gen/acir_ir/acir_variable.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs index 7c5393f3704..8733456a75b 100644 --- a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs +++ b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs @@ -1933,11 +1933,8 @@ impl AcirContext { } Some(optional_value) => { let mut values = Vec::new(); - match optional_value { - AcirValue::DynamicArray(_) => { - unreachable!("Dynamic array should already be initialized"); - } - _ => {} + if let AcirValue::DynamicArray(_) = optional_value { + unreachable!("Dynamic array should already be initialized"); } self.initialize_array_inner(&mut values, optional_value)?; values From 72254bf8b042224727071aceabc021d6857f4cc8 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Fri, 23 Aug 2024 15:38:04 +0000 Subject: [PATCH 5/5] clippy --- .../src/ssa/acir_gen/acir_ir/acir_variable.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs index 8733456a75b..c2cea6e6a00 100644 --- a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs +++ b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs @@ -1965,12 +1965,7 @@ impl AcirContext { self.initialize_array_inner(witnesses, value)?; } } - AcirValue::DynamicArray(AcirDynamicArray { - block_id, - len, - value_types, - element_type_sizes, - }) => { + AcirValue::DynamicArray(AcirDynamicArray { block_id, len, .. }) => { let dynamic_array_values = try_vecmap(0..len, |i| { let index_var = self.add_constant(i);