Skip to content

Commit

Permalink
cgen: fix codegen for assign from unsafeexpr resulting fixed array (fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Sep 26, 2024
1 parent 31e71e9 commit 08eef34
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
&& (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 is ast.Ident)) && !g.pref.translated
|| (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
21 changes: 21 additions & 0 deletions vlib/v/tests/assign/assign_array_fixed_from_union_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
union Convertor {
su8_array_p [20]u8
sint_array_p [5]int
}

fn test_main() {
a := [1, 2, 3, 4, 5]!
p := voidptr(unsafe { &Convertor(&a[0]) })
c := unsafe { &Convertor(p).su8_array_p }
dump(a)
dump(p)
dump(c)
assert a == [1, 2, 3, 4, 5]!
assert p != 0
$if little_endian {
assert c == [u8(1), 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0]!
}
$if big_endian {
assert c == [u8(0), 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5]!
}
}

0 comments on commit 08eef34

Please sign in to comment.