cgen: fix fixed array global variable (fix #14712) (#14730)

yuyi 2022-06-09 21:53:19 +08:00 committed by Chewing_Bever
parent e22c3d2a64
commit 04f8b0cb1e
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 12 additions and 1 deletions

View File

@ -0,0 +1 @@
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

View File

@ -0,0 +1,9 @@
module main
__global (
heap = [10]u8{}
)
fn main() {
println(heap)
}

View File

@ -4667,7 +4667,8 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) {
if field.has_expr || cinit {
if g.pref.translated {
g.definitions.write_string(' = ${g.expr_string(field.expr)}')
} else if (field.expr.is_literal() && should_init) || cinit {
} else if (field.expr.is_literal() && should_init) || cinit
|| (field.expr is ast.ArrayInit && (field.expr as ast.ArrayInit).is_fixed) {
// Simple literals can be initialized right away in global scope in C.
// e.g. `int myglobal = 10;`
g.definitions.write_string(' = ${g.expr_string(field.expr)}')