Skip to content

Commit

Permalink
cgen: fix struct update embed expr for fixed arrays (fix vlang#22999) (
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 authored Dec 2, 2024
1 parent f29fb5a commit c45b8d4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
20 changes: 18 additions & 2 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,7 @@ fn (mut g Gen) fixed_array_init_with_cast(expr ast.ArrayInit, typ ast.Type) {
}
}

fn (mut g Gen) fixed_array_update_expr_field(expr_str string, field_type ast.Type, field_name string, is_auto_deref bool, elem_type ast.Type, size int) {
fn (mut g Gen) fixed_array_update_expr_field(expr_str string, field_type ast.Type, field_name string, is_auto_deref bool, elem_type ast.Type, size int, is_update_embed bool) {
elem_sym := g.table.sym(elem_type)
if !g.inside_array_fixed_struct {
g.write('{')
Expand All @@ -1665,7 +1665,7 @@ fn (mut g Gen) fixed_array_update_expr_field(expr_str string, field_type ast.Typ
'${expr_str}->${c_name(field_name)}[${i}]'
}
g.fixed_array_update_expr_field(init_str, field_type, field_name, is_auto_deref,
elem_sym.info.elem_type, elem_sym.info.size)
elem_sym.info.elem_type, elem_sym.info.size, is_update_embed)
} else {
g.write(expr_str)
if !expr_str.ends_with(']') {
Expand All @@ -1674,6 +1674,22 @@ fn (mut g Gen) fixed_array_update_expr_field(expr_str string, field_type ast.Typ
} else {
g.write('.')
}
if is_update_embed {
update_sym := g.table.sym(field_type)
_, embeds := g.table.find_field_from_embeds(update_sym, field_name) or {
ast.StructField{}, []ast.Type{}
}
for embed in embeds {
esym := g.table.sym(embed)
ename := esym.embed_name()
g.write(ename)
if embed.is_ptr() {
g.write('->')
} else {
g.write('.')
}
}
}
g.write(c_name(field_name))
}
if !expr_str.starts_with('(') && !expr_str.starts_with('{') {
Expand Down
3 changes: 2 additions & 1 deletion vlib/v/gen/c/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
is_arr_fixed = true
g.fixed_array_update_expr_field(g.expr_string(node.update_expr),
node.update_expr_type, field.name, node.update_expr.is_auto_deref_var(),
update_expr_sym.info.elem_type, update_expr_sym.info.size)
update_expr_sym.info.elem_type, update_expr_sym.info.size,
node.is_update_embed)
} else {
g.write('(')
g.expr(node.update_expr)
Expand Down
30 changes: 30 additions & 0 deletions vlib/v/tests/fixed_array_update_embed_expr_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module main

type Mat4 = [14]f32

struct GameObject {
mut:
transform Mat4
}

struct Ship {
GameObject
}

fn (mut ship Ship) instance() &Ship {
return &Ship{
...ship
}
}

fn test_fixed_array_update_embed_expr() {
mut v := Ship{}
mut v2 := v.instance()

assert v2.transform.len == 14
assert v2.transform[0] == 0
assert v2.transform[13] == 0
assert v2.transform.all(it == 0)

dump('V=${v2}')
}

0 comments on commit c45b8d4

Please sign in to comment.