diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 8d4bedca23..59bd660108 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2230,6 +2230,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } mut return_type := ast.void_type is_decl := assign_stmt.op == .decl_assign + g.assign_op = assign_stmt.op op := if is_decl { token.Kind.assign } else { assign_stmt.op } right_expr := assign_stmt.right[0] match right_expr { @@ -2359,21 +2360,41 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } g.expr(lx) noscan := if is_auto_heap { g.check_noscan(return_type) } else { '' } - if is_opt { - mr_base_styp := g.base_type(return_type) - if is_auto_heap { - g.writeln(' = HEAP${noscan}($mr_base_styp, *($mr_base_styp*)${mr_var_name}.data).arg$i);') + if g.is_arraymap_set { + if is_opt { + mr_base_styp := g.base_type(return_type) + if is_auto_heap { + g.writeln('HEAP${noscan}($mr_base_styp, *($mr_base_styp*)${mr_var_name}.data).arg$i) });') + } else { + g.writeln('(*($mr_base_styp*)${mr_var_name}.data).arg$i });') + } } else { - g.writeln(' = (*($mr_base_styp*)${mr_var_name}.data).arg$i;') + if is_auto_heap { + g.writeln('HEAP${noscan}($styp, ${mr_var_name}.arg$i) });') + } else { + g.writeln('${mr_var_name}.arg$i });') + } } } else { - if is_auto_heap { - g.writeln(' = HEAP${noscan}($styp, ${mr_var_name}.arg$i);') + if is_opt { + mr_base_styp := g.base_type(return_type) + if is_auto_heap { + g.writeln(' = HEAP${noscan}($mr_base_styp, *($mr_base_styp*)${mr_var_name}.data).arg$i);') + } else { + g.writeln(' = (*($mr_base_styp*)${mr_var_name}.data).arg$i;') + } } else { - g.writeln(' = ${mr_var_name}.arg$i;') + if is_auto_heap { + g.writeln(' = HEAP${noscan}($styp, ${mr_var_name}.arg$i);') + } else { + g.writeln(' = ${mr_var_name}.arg$i;') + } } } } + if g.is_arraymap_set { + g.is_arraymap_set = false + } return } } diff --git a/vlib/v/tests/multiple_assign_array_index_test.v b/vlib/v/tests/multiple_assign_array_index_test.v new file mode 100644 index 0000000000..46de5dcd21 --- /dev/null +++ b/vlib/v/tests/multiple_assign_array_index_test.v @@ -0,0 +1,24 @@ +fn round(a int, b int, c int) (int, int, int) { + mut ax := a + mut bx := b + mut cx := c + + ax += bx + bx ^= cx + cx -= 1 + + return ax, bx, cx +} + +fn round_assignment(mut res []int) { + _ = res[2] + res[0], res[1], res[2] = round(res[0], res[1], res[2]) +} + +fn test_multiple_assign_array_index() { + mut a := []int{len: 3} + round_assignment(mut a) + + println(a) + assert a == [0, 0, -1] +}