From fcef8c98ee2afa9c3cfc0f8317d022c5ed0df3ba Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 20 Feb 2023 18:26:48 +0800 Subject: [PATCH] cgen: fix map of array initialisation, with len and no default (fix #17358) (#17367) --- vlib/v/gen/c/array.v | 3 ++- vlib/v/tests/array_init_test.v | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index b200209601..0555c731a5 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -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) is_default_array := elem_type.unaliased_sym.kind == .array && 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 g.inside_lambda = true mut tmp := g.new_tmp_var() diff --git a/vlib/v/tests/array_init_test.v b/vlib/v/tests/array_init_test.v index 910a17f1eb..2da51c8d60 100644 --- a/vlib/v/tests/array_init_test.v +++ b/vlib/v/tests/array_init_test.v @@ -257,3 +257,18 @@ fn test_multi_array_update_data() { println(a) 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 + }] +}