Skip to content

Commit 4df53ad

Browse files
authored
fix(acir_gen): Nested dynamic array initialization (#5810)
# Description ## Problem\* Resolves #5782 ## Summary\* We are using our recursive `initialize_array_inner` function to also check whether the outer AcirValue we are trying to initialize is a dynamic array. We should be able to initialize a new AcirValue::Array with internal AcirValue::DynamicArray types. ## Additional Context ## Documentation\* Check one: - [X] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [X] I have tested the changes locally. - [X] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
1 parent 335de05 commit 4df53ad

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,9 @@ impl<F: AcirField> AcirContext<F> {
19331933
}
19341934
Some(optional_value) => {
19351935
let mut values = Vec::new();
1936+
if let AcirValue::DynamicArray(_) = optional_value {
1937+
unreachable!("Dynamic array should already be initialized");
1938+
}
19361939
self.initialize_array_inner(&mut values, optional_value)?;
19371940
values
19381941
}
@@ -1962,8 +1965,16 @@ impl<F: AcirField> AcirContext<F> {
19621965
self.initialize_array_inner(witnesses, value)?;
19631966
}
19641967
}
1965-
AcirValue::DynamicArray(_) => {
1966-
unreachable!("Dynamic array should already be initialized");
1968+
AcirValue::DynamicArray(AcirDynamicArray { block_id, len, .. }) => {
1969+
let dynamic_array_values = try_vecmap(0..len, |i| {
1970+
let index_var = self.add_constant(i);
1971+
1972+
let read = self.read_from_memory(block_id, &index_var)?;
1973+
Ok::<AcirValue, InternalError>(AcirValue::Var(read, AcirType::field()))
1974+
})?;
1975+
for value in dynamic_array_values {
1976+
self.initialize_array_inner(witnesses, value)?;
1977+
}
19671978
}
19681979
}
19691980
Ok(())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "nested_dyn_array_regression_5782"
3+
type = "bin"
4+
authors = [""]
5+
compiler_version = ">=0.33.0"
6+
7+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
array = [5, 10]
2+
i = 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main(mut array: [Field; 2], i: u32) {
2+
assert_eq(array[i - 1], 5);
3+
assert_eq(array[i], 10);
4+
5+
array[i] = 2;
6+
7+
let array2 = [array, array];
8+
9+
assert_eq(array2[0][0], 5);
10+
assert_eq(array2[0][i], 2);
11+
assert_eq(array2[i][0], 5);
12+
assert_eq(array2[i][i], 2);
13+
}

0 commit comments

Comments
 (0)