1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

builtin: fix multiple array init (#10929)

This commit is contained in:
yuyi 2021-07-24 04:25:12 +08:00 committed by GitHub
parent 3d907caa3f
commit 29cda252f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View File

@ -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))) }

View File

@ -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]]]'
}