From b2ff9d5d0858856d7003f1e88ffe995b080c4146 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 16 Mar 2025 08:28:01 -0300 Subject: [PATCH] ast: fix the registration of fixed arrays, when size_expr is a const (fix #23946) (#23949) --- vlib/v/ast/table.v | 2 +- vlib/v/tests/consts/const_fixed_array_test.v | 33 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/consts/const_fixed_array_test.v diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 2807e3fa7..dc2dec7d7 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -991,7 +991,7 @@ pub fn (t &Table) array_fixed_name(elem_type Type, size int, size_expr Expr) str ptr := if elem_type.is_ptr() { '&'.repeat(elem_type.nr_muls()) } else { '' } opt := if elem_type.has_flag(.option) { '?' } else { '' } res := if elem_type.has_flag(.result) { '!' } else { '' } - size_str := if size_expr is EmptyExpr || size != 987654321 { + size_str := if size_expr is EmptyExpr || size !in [0, 987654321] { size.str() } else { size_expr.str() diff --git a/vlib/v/tests/consts/const_fixed_array_test.v b/vlib/v/tests/consts/const_fixed_array_test.v new file mode 100644 index 000000000..33e74ad5f --- /dev/null +++ b/vlib/v/tests/consts/const_fixed_array_test.v @@ -0,0 +1,33 @@ +module main + +const c_u16_size = sizeof(u16) +const c_u32_size = sizeof(u32) + +pub enum DataKind as u8 { + u8_array +} + +union U16Bytes { + value u16 + bytes [c_u16_size]u8 +} + +union U32Bytes { + value u32 + bytes [c_u32_size]u8 +} + +fn test_main() { + kind := DataKind.u8_array + match kind { + .u8_array { + buf_4u8 := [c_u32_size]u8{} + w := U32Bytes{ + bytes: buf_4u8 + } + unsafe { + assert w.bytes.len == c_u32_size + } + } + } +} -- 2.39.5