From 443f66723c4c0896264ac6c9ed15b954c7fe0a40 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 20 Oct 2025 08:25:25 -0300 Subject: [PATCH] cgen: fix codegen for alias for array fixed initialization (fix #25512) (#25540) --- vlib/v/gen/c/array.v | 4 ++-- vlib/v/gen/c/cgen.v | 4 ++++ .../casts/cast_sumtype_fixed_array_test.v | 21 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/casts/cast_sumtype_fixed_array_test.v diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 74afc0c97..14e224958 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -134,8 +134,8 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st } is_none := node.is_option && !node.has_init && !node.has_val - if (g.inside_struct_init && g.inside_cast && !g.inside_memset && !g.inside_opt_or_res) - || (node.is_option && !is_none) { + if (g.inside_struct_init && g.inside_cast && !g.inside_memset && !g.inside_opt_or_res + && !g.inside_sumtype_cast) || (node.is_option && !is_none) { ret_typ_str := g.styp(node.typ) g.write('(${ret_typ_str})') } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index ada86dd0c..e0f1eeeb6 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -154,6 +154,7 @@ mut: inside_for_c_stmt bool inside_cast_in_heap int // inside cast to interface type in heap (resolve recursive calls) inside_cast bool + inside_sumtype_cast bool inside_selector bool inside_selector_deref bool // indicates if the inside selector was already dereferenced inside_memset bool @@ -2968,10 +2969,13 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp ast.Ty ast.void_type) g.write(g.type_default(ctyp)) } else { + old_inside_sumtype_cast := g.inside_sumtype_cast + g.inside_sumtype_cast = true old_left_is_opt := g.left_is_opt g.left_is_opt = !exp.has_flag(.option) g.expr(expr) g.left_is_opt = old_left_is_opt + g.inside_sumtype_cast = old_inside_sumtype_cast } if is_sumtype_cast { // the `_to_sumtype_` family of functions last `is_mut` param diff --git a/vlib/v/tests/casts/cast_sumtype_fixed_array_test.v b/vlib/v/tests/casts/cast_sumtype_fixed_array_test.v new file mode 100644 index 000000000..8e2fc8c95 --- /dev/null +++ b/vlib/v/tests/casts/cast_sumtype_fixed_array_test.v @@ -0,0 +1,21 @@ +module main + +struct Ipv4 { + address [4]u8 +} + +struct Ipv6 { + address [16]u8 +} + +type Address = Ipv4 | Ipv6 + +fn test_main() { + address := Address(Ipv4{ + address: [u8(192), 168, 1, 1]! + }) + + assert address == Address(Ipv4{ + address: [u8(192), 168, 1, 1]! + }) +} -- 2.39.5