Skip to content

Commit 5b27233

Browse files
authored
cgen: fix codegen for const fixed array initialization with another const as item (fix #23565) (#23572)
1 parent d710d9e commit 5b27233

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎vlib/v/gen/c/cgen.v‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6076,14 +6076,17 @@ fn (mut g Gen) check_expr_is_const(expr ast.Expr) bool {
60766076
ast.InfixExpr {
60776077
return g.check_expr_is_const(expr.left) && g.check_expr_is_const(expr.right)
60786078
}
6079-
ast.Ident, ast.StructInit, ast.EnumVal {
6079+
ast.Ident {
6080+
return expr.kind == .function || g.table.final_sym(expr.obj.typ).kind != .array_fixed
6081+
}
6082+
ast.StructInit, ast.EnumVal {
60806083
return true
60816084
}
60826085
ast.CastExpr {
60836086
return g.check_expr_is_const(expr.expr)
60846087
}
60856088
ast.PrefixExpr {
6086-
return g.check_expr_is_const(expr.right)
6089+
return expr.right is ast.Ident || g.check_expr_is_const(expr.right)
60876090
}
60886091
else {
60896092
return false

‎vlib/v/gen/c/consts_and_globals.v‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,16 @@ fn (mut g Gen) const_decl_init_later_msvc_string_fixed_array(mod string, name st
367367
elem_typ := g.styp(elem_expr.typ)
368368
init.writeln(g.expr_string_surround('\tmemcpy(${cname}[${i}], (${elem_typ})',
369369
elem_expr, ', sizeof(${elem_typ}));'))
370+
} else if elem_expr is ast.Ident {
371+
elem_typ := elem_expr.obj.typ
372+
if g.table.final_sym(elem_typ).kind == .array_fixed {
373+
elem_styp := g.styp(elem_expr.obj.typ)
374+
init.writeln(g.expr_string_surround('\tmemcpy(${cname}[${i}], ', elem_expr,
375+
', sizeof(${elem_styp}));'))
376+
} else {
377+
init.writeln(g.expr_string_surround('\t${cname}[${i}] = ', elem_expr,
378+
';'))
379+
}
370380
} else {
371381
init.writeln(g.expr_string_surround('\t${cname}[${i}] = ', elem_expr, ';'))
372382
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const f = [u8(0), 1, 2]!
2+
3+
const ff = [[u8(1), 2, 3]!, [u8(5), 4, 3]!, f]!
4+
5+
fn test_main() {
6+
assert ff == [[u8(1), 2, 3]!, [u8(5), 4, 3]!, [u8(0), 1, 2]!]!
7+
}

0 commit comments

Comments
 (0)