Skip to content

Commit

Permalink
cgen: fix casting aliased fixed array to sumtype array (fix #23009) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 24, 2024
1 parent 5b5e845 commit ba036eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -2695,8 +2695,10 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr
}
g.write('${fname}(')
if !got_is_ptr && !got_is_fn {
if is_comptime_variant || !expr.is_lvalue()
|| (expr is ast.Ident && expr.obj.is_simple_define_const()) {
is_cast_fixed_array_init := expr is ast.CastExpr
&& (expr.expr is ast.ArrayInit && expr.expr.is_fixed)
if !is_cast_fixed_array_init && (is_comptime_variant || !expr.is_lvalue()
|| (expr is ast.Ident && expr.obj.is_simple_define_const())) {
// Note: the `_to_sumtype_` family of functions do call memdup internally, making
// another duplicate with the HEAP macro is redundant, so use ADDR instead:
if expr.is_as_cast() {
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/tests/aliases/alias_array_fixed_sumtype_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module main

type IPv4 = [4]u8
type IPv6 = [16]u8
type IP = IPv4 | IPv6

fn test_main() {
mut arr := []IP{}
arr << IP(IPv4([4]u8{}))
arr << IP(IPv6([16]u8{}))
assert arr.str() == '[IP(IPv4([0, 0, 0, 0])), IP(IPv6([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))]'
}

0 comments on commit ba036eb

Please sign in to comment.