Skip to content

Commit

Permalink
cgen: fix codegen for assigning aliased fixed array (fix vlang#22907) (
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Nov 19, 2024
1 parent 67d53aa commit dc15f9d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
14 changes: 10 additions & 4 deletions vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,12 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
unwrapped_val_type := g.unwrap_generic(val_type)
right_sym := g.table.sym(unwrapped_val_type)
unaliased_right_sym := g.table.final_sym(unwrapped_val_type)
is_fixed_array_var := unaliased_right_sym.kind == .array_fixed && val !is ast.ArrayInit
is_fixed_array_var := !g.pref.translated && unaliased_right_sym.kind == .array_fixed
&& val !is ast.ArrayInit
&& (val in [ast.Ident, ast.IndexExpr, ast.CallExpr, ast.SelectorExpr, ast.DumpExpr, ast.InfixExpr]
|| (val is ast.CastExpr && val.expr !is ast.ArrayInit)
|| (val is ast.PrefixExpr && val.op == .arrow)
|| (val is ast.UnsafeExpr && val.expr in [ast.SelectorExpr, ast.Ident]))
&& !g.pref.translated
g.is_assign_lhs = true
g.assign_op = node.op

Expand Down Expand Up @@ -450,13 +450,19 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
g.expr(val)
g.writeln(';}')
}
} else if node.op == .assign && !g.pref.translated
&& (is_fixed_array_init || (right_sym.kind == .array_fixed && val is ast.Ident)) {
} else if node.op == .assign && !g.pref.translated && (is_fixed_array_init
|| (unaliased_right_sym.kind == .array_fixed && val in [ast.Ident, ast.CastExpr])) {
// Fixed arrays
if is_fixed_array_init && var_type.has_flag(.option) {
g.expr(left)
g.write(' = ')
g.expr_with_opt(val, val_type, var_type)
} else if unaliased_right_sym.kind == .array_fixed && val is ast.CastExpr {
g.write('memcpy(')
g.expr(left)
g.write(', ')
g.expr(val)
g.writeln(', sizeof(${g.styp(var_type)}));')
} else {
mut v_var := ''
arr_typ := styp.trim('*')
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/tests/aliases/alias_array_fixed_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module main

type Arr = [4]u8

fn test_main() {
mut a := Arr{}
a = Arr([u8(5), 4, 3, 2]!)
assert a == Arr([u8(5), 4, 3, 2]!)
assert a == [u8(5), 4, 3, 2]!
}

0 comments on commit dc15f9d

Please sign in to comment.