From 59c3e98c1607699b0630b46f0d65da49d79e7f9f Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 21 Jan 2021 19:45:37 +0800 Subject: [PATCH] cgen: fix complex map_fixed_array (#8209) --- vlib/v/gen/cgen.v | 28 +++++++++++++++++---- vlib/v/tests/map_complex_fixed_array_test.v | 10 ++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 vlib/v/tests/map_complex_fixed_array_test.v diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 076a041703..b26d20b84a 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1950,6 +1950,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { right_sym := g.table.get_type_symbol(val_type) is_fixed_array_copy := right_sym.kind == .array_fixed && val is ast.Ident g.is_assign_lhs = true + g.assign_op = assign_stmt.op if is_interface && right_sym.kind == .interface_ { is_interface = false } @@ -1969,25 +1970,42 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { if right.has_val { for j, expr in right.exprs { g.expr(left) - g.write('[$j] = ') + if g.is_array_set { + g.out.go_back(2) + } else { + g.write('[$j] = ') + } g.expr(expr) - g.writeln(';') + if g.is_array_set { + g.writeln(')') + g.is_array_set = false + } else { + g.writeln(';') + } } } else { fixed_array := right_sym.info as table.ArrayFixed for j in 0 .. fixed_array.size { g.expr(left) - g.write('[$j] = ') + if g.is_array_set { + g.out.go_back(2) + } else { + g.write('[$j] = ') + } if right.has_default { g.expr(right.default_expr) } else { g.write(g.type_default(right.elem_type)) } - g.writeln(';') + if g.is_array_set { + g.writeln(')') + g.is_array_set = false + } else { + g.writeln(';') + } } } } else { - g.assign_op = assign_stmt.op is_inside_ternary := g.inside_ternary != 0 cur_line := if is_inside_ternary && is_decl { g.register_ternary_name(ident.name) diff --git a/vlib/v/tests/map_complex_fixed_array_test.v b/vlib/v/tests/map_complex_fixed_array_test.v new file mode 100644 index 0000000000..c1d2b97af3 --- /dev/null +++ b/vlib/v/tests/map_complex_fixed_array_test.v @@ -0,0 +1,10 @@ +fn foo(mut m map[string][1][2]map[string]int) { + m['foo'] = [[{'bar': 1}, {'baz':3}]!]! +} + +fn test_complex_map_fixed_array() { + mut m := map[string][1][2]map[string]int + foo(mut m) + println(m) + assert '$m' == "{'foo': [[{'bar': 1}, {'baz': 3}]]}" +}