From 922f003729708f7d31af220538b3a5e278e6d17f Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 9 Jun 2022 21:53:19 +0800 Subject: [PATCH] cgen: fix fixed array global variable (fix #14712) (#14730) --- .../global_fixed_array_initialisation.run.out | 1 + .../globals_run/global_fixed_array_initialisation.vv | 9 +++++++++ vlib/v/gen/c/cgen.v | 3 ++- 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/globals_run/global_fixed_array_initialisation.run.out create mode 100644 vlib/v/checker/tests/globals_run/global_fixed_array_initialisation.vv diff --git a/vlib/v/checker/tests/globals_run/global_fixed_array_initialisation.run.out b/vlib/v/checker/tests/globals_run/global_fixed_array_initialisation.run.out new file mode 100644 index 0000000000..3b10daad5a --- /dev/null +++ b/vlib/v/checker/tests/globals_run/global_fixed_array_initialisation.run.out @@ -0,0 +1 @@ +[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] diff --git a/vlib/v/checker/tests/globals_run/global_fixed_array_initialisation.vv b/vlib/v/checker/tests/globals_run/global_fixed_array_initialisation.vv new file mode 100644 index 0000000000..af71a8473e --- /dev/null +++ b/vlib/v/checker/tests/globals_run/global_fixed_array_initialisation.vv @@ -0,0 +1,9 @@ +module main + +__global ( + heap = [10]u8{} +) + +fn main() { + println(heap) +} diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 423914ff75..81bace58d2 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -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)}')