1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix struct field initialisation with a fixed array (#16692)

This commit is contained in:
Felipe Pena 2022-12-17 13:15:17 -03:00 committed by GitHub
parent 1aec40a126
commit 9921598c91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 2 deletions

View File

@ -0,0 +1,3 @@
Sample{
data: Data([5, 10, 15])
}

View File

@ -0,0 +1,17 @@
module main
type Data = [3]int | int
struct Sample {
data Data
}
fn test_main() {
test := Sample{
data: [5, 10, 15]!
// data: 5 /* works fine */
}
println(test)
assert test.data.str() == 'Data([5, 10, 15])'
}

View File

@ -2473,9 +2473,23 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
unwrapped_got_type = unwrapped_got_sym.info.types[g.aggregate_type_idx]
unwrapped_got_sym = g.table.sym(unwrapped_got_type)
}
fname := g.get_sumtype_casting_fn(unwrapped_got_type, unwrapped_expected_type)
g.call_cfn_for_casting_expr(fname, expr, expected_is_ptr, unwrapped_exp_sym.cname,
got_is_ptr, got_is_fn, got_styp)
if expr is ast.ArrayInit && got_sym.kind == .array_fixed {
stmt_str := g.go_before_stmt(0).trim_space()
g.empty_line = true
tmp_var := g.new_tmp_var()
g.write('${got_styp} ${tmp_var} = ')
g.expr(expr)
g.write(';')
g.write(stmt_str)
g.write('${fname}(&${tmp_var})')
return
} else {
g.call_cfn_for_casting_expr(fname, expr, expected_is_ptr, unwrapped_exp_sym.cname,
got_is_ptr, got_is_fn, got_styp)
}
}
return
}