cgen: fix codegen for const of fixed array (fix #22357) (#22364)

This commit is contained in:
Felipe Pena 2024-10-01 13:04:00 -03:00 committed by GitHub
parent 856fb8b257
commit 30952cf2bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 1 deletions

View File

@ -6248,8 +6248,14 @@ fn (mut g Gen) const_decl_init_later_msvc_string_fixed_array(mod string, name st
cname := g.c_const_name(name) cname := g.c_const_name(name)
mut init := strings.new_builder(100) mut init := strings.new_builder(100)
for i, elem_expr in expr.exprs { for i, elem_expr in expr.exprs {
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, ';')) init.writeln(g.expr_string_surround('\t${cname}[${i}] = ', elem_expr, ';'))
} }
}
mut def := '${styp} ${cname}' mut def := '${styp} ${cname}'
g.global_const_defs[util.no_dots(name)] = GlobalConstDef{ g.global_const_defs[util.no_dots(name)] = GlobalConstDef{
mod: mod mod: mod

View File

@ -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])
}