mirror of
https://github.com/vlang/v.git
synced 2025-09-17 11:26:17 -04:00
cgen: fix array fixed initialization on struct from call (#21568)
This commit is contained in:
parent
37f385c9d0
commit
6b2d527d9e
@ -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)
|
||||
|
@ -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))
|
||||
|
17
vlib/v/tests/array_fixed_struct_field_test.v
Normal file
17
vlib/v/tests/array_fixed_struct_field_test.v
Normal file
@ -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('')
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user