Skip to content

Commit

Permalink
cgen: fix codegen for const of fixed array (fix #22357) (#22364)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Oct 1, 2024
1 parent 856fb8b commit 30952cf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -6248,7 +6248,13 @@ fn (mut g Gen) const_decl_init_later_msvc_string_fixed_array(mod string, name st
cname := g.c_const_name(name)
mut init := strings.new_builder(100)
for i, elem_expr in expr.exprs {
init.writeln(g.expr_string_surround('\t${cname}[${i}] = ', elem_expr, ';'))
if elem_expr is ast.ArrayInit && elem_expr.is_fixed {
elem_typ := g.typ(elem_expr.typ)
init.writeln(g.expr_string_surround('\tmemcpy(${cname}[${i}], (${elem_typ})',
elem_expr, ', sizeof(${elem_typ}));'))
} else {
init.writeln(g.expr_string_surround('\t${cname}[${i}] = ', elem_expr, ';'))
}
}
mut def := '${styp} ${cname}'
g.global_const_defs[util.no_dots(name)] = GlobalConstDef{
Expand Down
32 changes: 32 additions & 0 deletions vlib/v/tests/consts/const_array_fixed_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@[has_globals]
module main

struct Foo {
bar int = 120
}

__global (
a Foo
)

const c = [[a.bar]!]!
const d = [[a.bar]]!
const e = [[a.bar]!]
const f = [[[a.bar]!]!]!
const g = [a.bar]

fn test_selector() {
assert dump(c == [[a.bar]!]!)
assert dump(d == [[a.bar]]!)
assert dump(e == [[a.bar]!])
assert dump(f == [[[a.bar]!]!]!)
assert dump(g == [a.bar])
}

fn test_literal() {
assert dump(c == [[120]!]!)
assert dump(d == [[120]]!)
assert dump(e == [[120]!])
assert dump(f == [[[120]!]!]!)
assert dump(g == [120])
}

0 comments on commit 30952cf

Please sign in to comment.