Skip to content

Commit

Permalink
cgen: fix array fixed initialization on struct from call (#21568)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored May 26, 2024
1 parent 37f385c commit 6b2d527
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -2455,6 +2455,20 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr
g.write(')'.repeat(rparen_n))
}

// use instead of expr() when you need a var to use as reference
fn (mut g Gen) expr_with_var(expr ast.Expr, got_type_raw ast.Type, expected_type ast.Type) string {
stmt_str := g.go_before_last_stmt().trim_space()
g.empty_line = true
tmp_var := g.new_tmp_var()
styp := g.typ(expected_type)
g.writeln('${styp} ${tmp_var};')
g.write('memcpy(&${tmp_var}, ')
g.expr(expr)
g.writeln(', sizeof(${styp}));')
g.write(stmt_str)
return tmp_var
}

// use instead of expr() when you need to cast to a different type
fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_type ast.Type) {
got_type := ast.mktyp(got_type_raw)
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/gen/c/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,10 @@ fn (mut g Gen) struct_init_field(sfield ast.StructInitField, language ast.Langua
info := field_unwrap_sym.info as ast.ArrayFixed
g.fixed_array_var_init(g.expr_string(sfield.expr), sfield.expr.is_auto_deref_var(),
info.elem_type, info.size)
} else if field_unwrap_sym.kind == .array_fixed && sfield.expr is ast.CallExpr {
info := field_unwrap_sym.info as ast.ArrayFixed
tmp_var := g.expr_with_var(sfield.expr, sfield.typ, sfield.expected_type)
g.fixed_array_var_init(tmp_var, false, info.elem_type, info.size)
} else {
if sfield.typ != ast.voidptr_type && sfield.typ != ast.nil_type
&& (sfield.expected_type.is_ptr() && !sfield.expected_type.has_flag(.shared_f))
Expand Down
17 changes: 17 additions & 0 deletions vlib/v/tests/array_fixed_struct_field_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
struct Args {
bytes [2]int
}

fn test_main() {
make_args() or { assert err.msg() == 'a' }
}

fn make_args() !Args {
return Args{
bytes: get_range() or { return error('a') }
}
}

fn get_range() ![2]int {
return error('')
}

0 comments on commit 6b2d527

Please sign in to comment.