Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix codegen for assigning aliased fixed array #22909

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]!
}
Loading