From 6c8182cc25c430d2b3f914e98f28e932e9d6b7c4 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 20 Jun 2021 18:36:01 +0800 Subject: [PATCH] cgen: fix assigning literal fixed arrays to map values (#10525) --- vlib/v/gen/c/cgen.v | 7 +++++-- .../assign_map_value_of_fixed_array_test.v | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/assign_map_value_of_fixed_array_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index a0f3e85388..616e742b03 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2473,7 +2473,9 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { else {} } right_sym := g.table.get_type_symbol(g.unwrap_generic(val_type)) - is_fixed_array_var := right_sym.kind == .array_fixed && val is ast.Ident + is_fixed_array_var := right_sym.kind == .array_fixed && (val is ast.Ident + || val is ast.IndexExpr || val is ast.CallExpr + || val is ast.SelectorExpr) g.is_assign_lhs = true g.assign_op = assign_stmt.op if val_type.has_flag(.optional) { @@ -2490,7 +2492,8 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { g.expr(val) g.writeln(';}') } - } else if assign_stmt.op == .assign && (is_fixed_array_init || is_fixed_array_var) { + } else if assign_stmt.op == .assign + && (is_fixed_array_init || (right_sym.kind == .array_fixed && val is ast.Ident)) { mut v_var := '' arr_typ := styp.trim('*') if is_fixed_array_init { diff --git a/vlib/v/tests/assign_map_value_of_fixed_array_test.v b/vlib/v/tests/assign_map_value_of_fixed_array_test.v new file mode 100644 index 0000000000..d9568f1a0f --- /dev/null +++ b/vlib/v/tests/assign_map_value_of_fixed_array_test.v @@ -0,0 +1,18 @@ +fn test_assign_map_value_of_fixed_array() { + mut m := map[string][2]f64{} + + m['A'] = [1.1, 2.2]! + m['B'] = [0.1, 0.2]! + + mut arr := m['A'] + println(arr) + assert '$arr' == '[1.1, 2.2]' + + arr = m['B'] + println(arr) + assert '$arr' == '[0.1, 0.2]' + + arr = m['C'] + println(arr) + assert '$arr' == '[0, 0]' +}