Skip to content

Commit 9921598

Browse files
authored
cgen: fix struct field initialisation with a fixed array (#16692)
1 parent 1aec40a commit 9921598

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Sample{
2+
data: Data([5, 10, 15])
3+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module main
2+
3+
type Data = [3]int | int
4+
5+
struct Sample {
6+
data Data
7+
}
8+
9+
fn test_main() {
10+
test := Sample{
11+
data: [5, 10, 15]!
12+
// data: 5 /* works fine */
13+
}
14+
15+
println(test)
16+
assert test.data.str() == 'Data([5, 10, 15])'
17+
}

vlib/v/gen/c/cgen.v

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,9 +2473,23 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
24732473
unwrapped_got_type = unwrapped_got_sym.info.types[g.aggregate_type_idx]
24742474
unwrapped_got_sym = g.table.sym(unwrapped_got_type)
24752475
}
2476+
24762477
fname := g.get_sumtype_casting_fn(unwrapped_got_type, unwrapped_expected_type)
2477-
g.call_cfn_for_casting_expr(fname, expr, expected_is_ptr, unwrapped_exp_sym.cname,
2478-
got_is_ptr, got_is_fn, got_styp)
2478+
2479+
if expr is ast.ArrayInit && got_sym.kind == .array_fixed {
2480+
stmt_str := g.go_before_stmt(0).trim_space()
2481+
g.empty_line = true
2482+
tmp_var := g.new_tmp_var()
2483+
g.write('${got_styp} ${tmp_var} = ')
2484+
g.expr(expr)
2485+
g.write(';')
2486+
g.write(stmt_str)
2487+
g.write('${fname}(&${tmp_var})')
2488+
return
2489+
} else {
2490+
g.call_cfn_for_casting_expr(fname, expr, expected_is_ptr, unwrapped_exp_sym.cname,
2491+
got_is_ptr, got_is_fn, got_styp)
2492+
}
24792493
}
24802494
return
24812495
}

0 commit comments

Comments
 (0)