From 708c73b4ea4f2c665445e852968ca1f0d1aa9725 Mon Sep 17 00:00:00 2001 From: Krchi <997054144@qq.com> Date: Tue, 22 Jul 2025 22:52:00 +0800 Subject: [PATCH] cgen: fix const fixed array with type alias (fix #24936) (#24946) --- vlib/v/gen/c/cgen.v | 2 +- .../const_fixed_array_with_type_alias_test.v | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/consts/const_fixed_array_with_type_alias_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index ac7d59a40..914d421dd 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5479,7 +5479,7 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { if node_typ_is_option { g.expr_with_opt(node.expr, expr_type, node.typ) } else { - if node.expr is ast.ArrayInit && g.assign_op != .decl_assign { + if node.expr is ast.ArrayInit && g.assign_op != .decl_assign && !g.inside_const { g.write('(${g.styp(node.expr.typ)})') } g.expr(node.expr) diff --git a/vlib/v/tests/consts/const_fixed_array_with_type_alias_test.v b/vlib/v/tests/consts/const_fixed_array_with_type_alias_test.v new file mode 100644 index 000000000..76b672b67 --- /dev/null +++ b/vlib/v/tests/consts/const_fixed_array_with_type_alias_test.v @@ -0,0 +1,14 @@ +pub const k_palette_colors = [Pixel([u8(255), 255, 255]!), Pixel([u8(192), 192, 192]!), + Pixel([u8(96), 96, 96]!), Pixel([u8(0), 0, 0]!)] + +pub type Pixel = [3]u8 +pub type Palette = []Pixel + +fn test_main() { + a := Palette(k_palette_colors) + + assert a[0] == [u8(255), 255, 255]! + assert a[1] == [u8(192), 192, 192]! + assert a[2] == [u8(96), 96, 96]! + assert a[3] == [u8(0), 0, 0]! +} -- 2.39.5