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:
parent
1aec40a126
commit
9921598c91
3
vlib/v/checker/tests/struct_fixed_array_init_test.out
Normal file
3
vlib/v/checker/tests/struct_fixed_array_init_test.out
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
Sample{
|
||||||
|
data: Data([5, 10, 15])
|
||||||
|
}
|
17
vlib/v/checker/tests/struct_fixed_array_init_test.vv
Normal file
17
vlib/v/checker/tests/struct_fixed_array_init_test.vv
Normal 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])'
|
||||||
|
}
|
|
@ -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_type = unwrapped_got_sym.info.types[g.aggregate_type_idx]
|
||||||
unwrapped_got_sym = g.table.sym(unwrapped_got_type)
|
unwrapped_got_sym = g.table.sym(unwrapped_got_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
fname := g.get_sumtype_casting_fn(unwrapped_got_type, unwrapped_expected_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
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user