Skip to content

Commit 6b2d527

Browse files
authored
cgen: fix array fixed initialization on struct from call (#21568)
1 parent 37f385c commit 6b2d527

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

vlib/v/gen/c/cgen.v

+14
Original file line numberDiff line numberDiff line change
@@ -2455,6 +2455,20 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr
24552455
g.write(')'.repeat(rparen_n))
24562456
}
24572457

2458+
// use instead of expr() when you need a var to use as reference
2459+
fn (mut g Gen) expr_with_var(expr ast.Expr, got_type_raw ast.Type, expected_type ast.Type) string {
2460+
stmt_str := g.go_before_last_stmt().trim_space()
2461+
g.empty_line = true
2462+
tmp_var := g.new_tmp_var()
2463+
styp := g.typ(expected_type)
2464+
g.writeln('${styp} ${tmp_var};')
2465+
g.write('memcpy(&${tmp_var}, ')
2466+
g.expr(expr)
2467+
g.writeln(', sizeof(${styp}));')
2468+
g.write(stmt_str)
2469+
return tmp_var
2470+
}
2471+
24582472
// use instead of expr() when you need to cast to a different type
24592473
fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_type ast.Type) {
24602474
got_type := ast.mktyp(got_type_raw)

vlib/v/gen/c/struct.v

+4
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,10 @@ fn (mut g Gen) struct_init_field(sfield ast.StructInitField, language ast.Langua
652652
info := field_unwrap_sym.info as ast.ArrayFixed
653653
g.fixed_array_var_init(g.expr_string(sfield.expr), sfield.expr.is_auto_deref_var(),
654654
info.elem_type, info.size)
655+
} else if field_unwrap_sym.kind == .array_fixed && sfield.expr is ast.CallExpr {
656+
info := field_unwrap_sym.info as ast.ArrayFixed
657+
tmp_var := g.expr_with_var(sfield.expr, sfield.typ, sfield.expected_type)
658+
g.fixed_array_var_init(tmp_var, false, info.elem_type, info.size)
655659
} else {
656660
if sfield.typ != ast.voidptr_type && sfield.typ != ast.nil_type
657661
&& (sfield.expected_type.is_ptr() && !sfield.expected_type.has_flag(.shared_f))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
struct Args {
2+
bytes [2]int
3+
}
4+
5+
fn test_main() {
6+
make_args() or { assert err.msg() == 'a' }
7+
}
8+
9+
fn make_args() !Args {
10+
return Args{
11+
bytes: get_range() or { return error('a') }
12+
}
13+
}
14+
15+
fn get_range() ![2]int {
16+
return error('')
17+
}

0 commit comments

Comments
 (0)