Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix codegen for const fixed array initialization with another const as item #23572

Merged
merged 6 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]!]!
}
Loading