From 268cee82fc77e3f26dd9354dc737587f7d797b26 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 18 Mar 2023 05:12:59 +0800 Subject: [PATCH] cgen: fix map of complex array (#17660) --- vlib/v/gen/c/cgen.v | 1 + vlib/v/gen/c/index.v | 12 ++++++++++-- vlib/v/tests/map_complex_array_test.v | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/map_complex_array_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 3c2d24516d..6e493ca13f 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -123,6 +123,7 @@ mut: inside_map_infix bool // inside map<val') @@ -393,7 +400,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) { g.typ(elem_type.clear_flag(.option).clear_flag(.result)) } } - get_and_set_types := elem_sym.kind in [.struct_, .map] + get_and_set_types := elem_sym.kind in [.struct_, .map, .array] if g.is_assign_lhs && !g.is_arraymap_set && !get_and_set_types { if g.assign_op == .assign || info.value_type == ast.string_type { g.is_arraymap_set = true @@ -434,6 +441,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) { g.write('${zero} })))') } } else if g.inside_map_postfix || g.inside_map_infix || g.inside_map_index + || g.inside_array_index || (g.is_assign_lhs && !g.is_arraymap_set && get_and_set_types) { zero := g.type_default(info.value_type) if node.is_setter { diff --git a/vlib/v/tests/map_complex_array_test.v b/vlib/v/tests/map_complex_array_test.v new file mode 100644 index 0000000000..b7da5707ac --- /dev/null +++ b/vlib/v/tests/map_complex_array_test.v @@ -0,0 +1,19 @@ +struct Instr { +mut: + a int + b int +} + +fn test_map_complex_array() { + mut map1 := map[string][]&Instr{} + instr := &Instr{ + a: 1 + b: 2 + } + arr := [instr] + map1['Hello'] = arr + map1['Hello'][0].a = 2 + println(map1['Hello'][0].a) + assert map1['Hello'][0].a == 2 + assert map1['Hello'][0].b == 2 +}