diff --git a/vlib/arrays/arrays_test.v b/vlib/arrays/arrays_test.v index adb87b774e..df7f5f51d4 100644 --- a/vlib/arrays/arrays_test.v +++ b/vlib/arrays/arrays_test.v @@ -79,3 +79,25 @@ fn test_merge() { assert merge(a, c) == a assert merge(d, b) == b } + +fn test_fixed_array_assignment() { + mut a := [2]int{} + a[0] = 111 + a[1] = 222 + b := a + assert b[0] == a[0] + assert b[1] == a[1] + mut c := [2]int{} + c = a + assert c[0] == a[0] + assert c[1] == a[1] + + d := [3]int{init: 333} + for val in d { + assert val == 333 + } + e := [3]string{init: 'vlang'} + for val in e { + assert val == 'vlang' + } +} \ No newline at end of file diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 295b5d6d55..46ce1d7e8c 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1842,6 +1842,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { else {} } 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 if is_interface && right_sym.kind == .interface_ { is_interface = false @@ -1857,7 +1858,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { g.expr(val) g.writeln(';}') } - } else if right_sym.kind == .array_fixed && assign_stmt.op == .assign { + } else if is_fixed_array_init && assign_stmt.op == .assign { right := val as ast.ArrayInit if right.has_val { for j, expr in right.exprs { @@ -1925,7 +1926,9 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { if left is ast.Ident || left is ast.SelectorExpr { g.prevent_sum_type_unwrapping_once = true } - g.expr(left) + if !is_fixed_array_copy || is_decl { + g.expr(left) + } } if is_inside_ternary && is_decl { g.write(';\n$cur_line') @@ -1934,7 +1937,11 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } g.is_assign_lhs = false g.is_assign_rhs = true - if !g.is_array_set && !str_add { + if is_fixed_array_copy { + if is_decl { + g.writeln(';') + } + } else if !g.is_array_set && !str_add { g.write(' $op ') } else if str_add { g.write(', ') @@ -1963,7 +1970,16 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } g.is_shared = var_type.has_flag(.shared_f) if !cloned { - if is_decl { + if is_fixed_array_copy { + i_var := g.new_tmp_var() + fixed_array := right_sym.info as table.ArrayFixed + g.write('for(int $i_var=0; $i_var<$fixed_array.size; $i_var++) {') + g.expr(left) + g.write('[$i_var] = ') + g.expr(val) + g.write('[$i_var];') + g.writeln('}') + } else if is_decl { if is_fixed_array_init && !has_val { if val is ast.ArrayInit { if val.has_default {