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

cgen: fix map of array initialisation, with len and no default (fix #17358) (#17367)

This commit is contained in:
yuyi 2023-02-20 18:26:48 +08:00 committed by GitHub
parent e9a3817aed
commit fcef8c98ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -195,7 +195,8 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
noscan := g.check_noscan(elem_type.typ) noscan := g.check_noscan(elem_type.typ)
is_default_array := elem_type.unaliased_sym.kind == .array && node.has_default is_default_array := elem_type.unaliased_sym.kind == .array && node.has_default
is_default_map := elem_type.unaliased_sym.kind == .map && node.has_default is_default_map := elem_type.unaliased_sym.kind == .map && node.has_default
needs_more_defaults := node.has_len && g.struct_has_array_or_map_field(elem_type.typ) needs_more_defaults := node.has_len && (g.struct_has_array_or_map_field(elem_type.typ)
|| elem_type.unaliased_sym.kind in [.array, .map])
if node.has_it { // []int{len: 6, init: it * it} when variable it is used in init expression if node.has_it { // []int{len: 6, init: it * it} when variable it is used in init expression
g.inside_lambda = true g.inside_lambda = true
mut tmp := g.new_tmp_var() mut tmp := g.new_tmp_var()

View File

@ -257,3 +257,18 @@ fn test_multi_array_update_data() {
println(a) println(a)
assert '${a}' == '[[[0, 0], [0, 2], [0, 0]], [[0, 0], [0, 0], [0, 0]]]' assert '${a}' == '[[[0, 0], [0, 2], [0, 0]], [[0, 0], [0, 0], [0, 0]]]'
} }
fn test_array_of_map_with_len_no_default() {
mut arr := []map[int]int{len: 3}
arr[0][0] = 0
arr[1][1] = 1
arr[2][2] = 2
println(arr)
assert arr == [{
0: 0
}, {
1: 1
}, {
2: 2
}]
}