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 of fixed array #22364

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -6247,7 +6247,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 {
felipensp marked this conversation as resolved.
Show resolved Hide resolved
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
16 changes: 16 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,16 @@
@[has_globals]
module main

struct Foo {
bar int = 120
}

__global (
a Foo
)

const c = [[a.bar]!]!
felipensp marked this conversation as resolved.
Show resolved Hide resolved

fn test_main() {
dump(c == [[120]!]!)
}
Loading