cgen: fix array map to fixed array (fix #23116) (#23118)

This commit is contained in:
yuyi 2024-12-10 13:40:49 +08:00 committed by GitHub
parent 031ed0fa63
commit 43e27d2443
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 0 deletions

View File

@ -576,6 +576,28 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) {
g.write('${ret_elem_styp} ${tmp_map_expr_result_name} = ') g.write('${ret_elem_styp} ${tmp_map_expr_result_name} = ')
g.expr(expr.expr) g.expr(expr.expr)
} }
ast.SelectorExpr {
if expr.typ != ast.void_type && g.table.final_sym(expr.typ).kind == .array_fixed {
atype := g.styp(expr.typ)
if closure_var_decl != '' {
g.write('memcpy(&${closure_var_decl}, &')
g.expr(expr)
g.write(', sizeof(${atype}))')
} else {
g.writeln('${ret_elem_styp} ${tmp_map_expr_result_name};')
g.write('memcpy(&${tmp_map_expr_result_name}, &')
g.expr(expr)
g.write(', sizeof(${atype}))')
}
} else {
if closure_var_decl != '' {
g.write('${closure_var_decl} = ')
} else {
g.write('${ret_elem_styp} ${tmp_map_expr_result_name} = ')
}
g.expr(expr)
}
}
else { else {
if closure_var_decl != '' { if closure_var_decl != '' {
g.write('${closure_var_decl} = ') g.write('${closure_var_decl} = ')

View File

@ -0,0 +1,13 @@
type Mat4 = [16]f32
struct GameObject {
mut:
transform Mat4
children []&GameObject
}
fn test_array_map_to_fixed_array() {
mut v1 := &GameObject{}
eprintln('children: ${v1.children.map(it.transform)}')
assert '${v1.children.map(it.transform)}' == '[]'
}