From d3c679bdcb039918cfc3ba48bb3ebf27a2743512 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 5 Jun 2023 10:46:48 -0300 Subject: [PATCH] cgen: fix fixed array of map (#18347) --- vlib/v/checker/checker.v | 6 ++++++ vlib/v/gen/c/array.v | 19 ++++++++++++++++++- vlib/v/tests/fixed_array_map_test.v | 13 +++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/fixed_array_map_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 1383243b1b..4970266260 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2530,6 +2530,12 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type { unwrapped_expr_type := c.unwrap_generic(node.expr_type) tsym := c.table.sym(unwrapped_expr_type) + if tsym.kind == .array_fixed { + info := tsym.info as ast.ArrayFixed + // for dumping fixed array we must registed the fixed array struct to return from function + c.table.find_or_register_array_fixed(info.elem_type, info.size, info.size_expr, + true) + } type_cname := if node.expr_type.has_flag(.option) { '_option_${tsym.cname}' } else { diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 2c757feeb5..4430f119d3 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -170,7 +170,24 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st g.expr(node.default_expr) } } else { - g.write('0') + elem_sym := g.table.final_sym(node.elem_type) + if elem_sym.kind == .map { + info := array_type.unaliased_sym.info as ast.ArrayFixed + map_info := elem_sym.map_info() + g.expr(ast.MapInit{ + key_type: map_info.key_type + value_type: map_info.value_type + }) + for _ in 1 .. info.size { + g.write(', ') + g.expr(ast.MapInit{ + key_type: map_info.key_type + value_type: map_info.value_type + }) + } + } else { + g.write('0') + } } g.write('}') if need_tmp_var { diff --git a/vlib/v/tests/fixed_array_map_test.v b/vlib/v/tests/fixed_array_map_test.v new file mode 100644 index 0000000000..bb1048dbab --- /dev/null +++ b/vlib/v/tests/fixed_array_map_test.v @@ -0,0 +1,13 @@ +fn test_main() { + mut arr := [22]map[string]string{} + arr[0]['key'] = 'value' + arr[1]['key2'] = 'value2' + dump(arr[0]) + assert dump(arr[0]) == { + 'key': 'value' + } + assert dump(arr[1]) == { + 'key2': 'value2' + } + assert dump(arr).len == 22 +}