cgen: fix array fixed initialization on struct from call (#21568)

This commit is contained in:
Felipe Pena 2024-05-26 18:40:57 -03:00 committed by GitHub
parent 37f385c9d0
commit 6b2d527d9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 0 deletions

View File

@ -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)

View File

@ -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))

View 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('')
}