From 29cda252f1263dc719afffeefa6f3c9c2238d1bb Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 24 Jul 2021 04:25:12 +0800 Subject: [PATCH] builtin: fix multiple array init (#10929) --- vlib/builtin/array.v | 4 ++-- vlib/v/tests/array_init_test.v | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 1457a1950f..99055f3958 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -55,7 +55,7 @@ fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array) a cap: cap_ } for i in 0 .. arr.len { - val_clone := val.clone() + val_clone := unsafe { val.clone_to_depth(1) } unsafe { arr.set_unsafe(i, &val_clone) } } return arr @@ -382,7 +382,7 @@ pub fn (a &array) clone_to_depth(depth int) array { cap: a.cap } // Recursively clone-generated elements if array element is array type - if depth > 0 { + if depth > 0 && a.element_size == sizeof(array) && a.len >= 0 && a.cap >= a.len { for i in 0 .. a.len { ar := array{} unsafe { C.memcpy(&ar, a.get_unsafe(i), int(sizeof(array))) } diff --git a/vlib/v/tests/array_init_test.v b/vlib/v/tests/array_init_test.v index 58b4c68a6a..d75db0de3d 100644 --- a/vlib/v/tests/array_init_test.v +++ b/vlib/v/tests/array_init_test.v @@ -250,3 +250,10 @@ fn test_array_init_inferred_from_optional() { fn read() ?[]string { return error('failed') } + +fn test_multi_array_update_data() { + mut a := [][][]int{len: 2, init: [][]int{len: 3, init: []int{len: 2}}} + a[0][1][1] = 2 + println(a) + assert '$a' == '[[[0, 0], [0, 2], [0, 0]], [[0, 0], [0, 0], [0, 0]]]' +}