From 8a0fc258a4a998d8e3164ce7473c3d01cc3bf5f4 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 20 Jul 2024 15:10:14 -0300 Subject: [PATCH] checker: fix global fixed array key resolution when it is a constant ident (#21900) --- vlib/v/checker/checker.v | 10 +++++++++ vlib/v/tests/modules/sub/foo/const.v | 3 +++ .../modules/sub/global_fixed_array_test.v | 22 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 vlib/v/tests/modules/sub/foo/const.v create mode 100644 vlib/v/tests/modules/sub/global_fixed_array_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index cd7352221..a2cb58297 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2278,6 +2278,16 @@ fn (mut c Checker) global_decl(mut node ast.GlobalDecl) { panic('internal compiler error - could not find global in scope') } v.typ = ast.mktyp(field.typ) + } else { + field_sym := c.table.sym(field.typ) + if field_sym.info is ast.ArrayFixed && c.array_fixed_has_unresolved_size(field_sym.info) { + mut size_expr := field_sym.info.size_expr + field.typ = c.eval_array_fixed_sizes(mut size_expr, 0, field_sym.info.elem_type) + mut v := c.file.global_scope.find_global(field.name) or { + panic('internal compiler error - could not find global in scope') + } + v.typ = ast.mktyp(field.typ) + } } c.global_names << field.name } diff --git a/vlib/v/tests/modules/sub/foo/const.v b/vlib/v/tests/modules/sub/foo/const.v new file mode 100644 index 000000000..dca8005e2 --- /dev/null +++ b/vlib/v/tests/modules/sub/foo/const.v @@ -0,0 +1,3 @@ +module foo + +pub const num_elements = 2 diff --git a/vlib/v/tests/modules/sub/global_fixed_array_test.v b/vlib/v/tests/modules/sub/global_fixed_array_test.v new file mode 100644 index 000000000..b4b516e4f --- /dev/null +++ b/vlib/v/tests/modules/sub/global_fixed_array_test.v @@ -0,0 +1,22 @@ +@[has_globals] +module main + +import sub.foo + +struct DummyStruct { + dummy_item i32 +} + +// vfmt off +__global ( + d [foo.num_elements]DummyStruct +) +// vfmt on + +const f = [foo.num_elements]DummyStruct{} + +fn test_main() { + assert dump(foo.num_elements) == 2 + assert dump(f) == [foo.num_elements]DummyStruct{} + assert dump(d) == [foo.num_elements]DummyStruct{} +} -- 2.39.5