Skip to content

Commit

Permalink
cgen: fix codegen for const fixed array initialization with another c…
Browse files Browse the repository at this point in the history
…onst as item (fix #23565) (#23572)
  • Loading branch information
felipensp authored Jan 25, 2025
1 parent d710d9e commit 5b27233
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
7 changes: 5 additions & 2 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -6076,14 +6076,17 @@ fn (mut g Gen) check_expr_is_const(expr ast.Expr) bool {
ast.InfixExpr {
return g.check_expr_is_const(expr.left) && g.check_expr_is_const(expr.right)
}
ast.Ident, ast.StructInit, ast.EnumVal {
ast.Ident {
return expr.kind == .function || g.table.final_sym(expr.obj.typ).kind != .array_fixed
}
ast.StructInit, ast.EnumVal {
return true
}
ast.CastExpr {
return g.check_expr_is_const(expr.expr)
}
ast.PrefixExpr {
return g.check_expr_is_const(expr.right)
return expr.right is ast.Ident || g.check_expr_is_const(expr.right)
}
else {
return false
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/gen/c/consts_and_globals.v
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,16 @@ fn (mut g Gen) const_decl_init_later_msvc_string_fixed_array(mod string, name st
elem_typ := g.styp(elem_expr.typ)
init.writeln(g.expr_string_surround('\tmemcpy(${cname}[${i}], (${elem_typ})',
elem_expr, ', sizeof(${elem_typ}));'))
} else if elem_expr is ast.Ident {
elem_typ := elem_expr.obj.typ
if g.table.final_sym(elem_typ).kind == .array_fixed {
elem_styp := g.styp(elem_expr.obj.typ)
init.writeln(g.expr_string_surround('\tmemcpy(${cname}[${i}], ', elem_expr,
', sizeof(${elem_styp}));'))
} else {
init.writeln(g.expr_string_surround('\t${cname}[${i}] = ', elem_expr,
';'))
}
} else {
init.writeln(g.expr_string_surround('\t${cname}[${i}] = ', elem_expr, ';'))
}
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/tests/consts/const_fixed_array_with_var_item_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const f = [u8(0), 1, 2]!

const ff = [[u8(1), 2, 3]!, [u8(5), 4, 3]!, f]!

fn test_main() {
assert ff == [[u8(1), 2, 3]!, [u8(5), 4, 3]!, [u8(0), 1, 2]!]!
}

0 comments on commit 5b27233

Please sign in to comment.