Skip to content

Commit 443f667

Browse files
authored
cgen: fix codegen for alias for array fixed initialization (fix #25512) (#25540)
1 parent d072437 commit 443f667

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎vlib/v/gen/c/array.v‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
134134
}
135135
is_none := node.is_option && !node.has_init && !node.has_val
136136

137-
if (g.inside_struct_init && g.inside_cast && !g.inside_memset && !g.inside_opt_or_res)
138-
|| (node.is_option && !is_none) {
137+
if (g.inside_struct_init && g.inside_cast && !g.inside_memset && !g.inside_opt_or_res
138+
&& !g.inside_sumtype_cast) || (node.is_option && !is_none) {
139139
ret_typ_str := g.styp(node.typ)
140140
g.write('(${ret_typ_str})')
141141
}

‎vlib/v/gen/c/cgen.v‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ mut:
154154
inside_for_c_stmt bool
155155
inside_cast_in_heap int // inside cast to interface type in heap (resolve recursive calls)
156156
inside_cast bool
157+
inside_sumtype_cast bool
157158
inside_selector bool
158159
inside_selector_deref bool // indicates if the inside selector was already dereferenced
159160
inside_memset bool
@@ -2968,10 +2969,13 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp ast.Ty
29682969
ast.void_type)
29692970
g.write(g.type_default(ctyp))
29702971
} else {
2972+
old_inside_sumtype_cast := g.inside_sumtype_cast
2973+
g.inside_sumtype_cast = true
29712974
old_left_is_opt := g.left_is_opt
29722975
g.left_is_opt = !exp.has_flag(.option)
29732976
g.expr(expr)
29742977
g.left_is_opt = old_left_is_opt
2978+
g.inside_sumtype_cast = old_inside_sumtype_cast
29752979
}
29762980
if is_sumtype_cast {
29772981
// the `_to_sumtype_` family of functions last `is_mut` param
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module main
2+
3+
struct Ipv4 {
4+
address [4]u8
5+
}
6+
7+
struct Ipv6 {
8+
address [16]u8
9+
}
10+
11+
type Address = Ipv4 | Ipv6
12+
13+
fn test_main() {
14+
address := Address(Ipv4{
15+
address: [u8(192), 168, 1, 1]!
16+
})
17+
18+
assert address == Address(Ipv4{
19+
address: [u8(192), 168, 1, 1]!
20+
})
21+
}

0 commit comments

Comments
 (0)