From 064be5213df771af92af36f5268b77e798640f03 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 26 Feb 2026 20:56:07 +0300 Subject: [PATCH] checker: change array initialization syntax (fixes #7061) --- vlib/v/checker/checker.v | 10 ++++++++++ vlib/v/tests/casts/cast_to_array_test.v | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 589ef9886..473faaad3 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3769,6 +3769,16 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { if node.typ.has_flag(.generic) { c.table.used_features.comptime_syms[to_type] = true } + base_to_type := to_type.clear_option_and_result() + if mut node.expr is ast.ArrayInit && node.expr.typ == ast.void_type + && c.table.final_sym(base_to_type).kind == .array { + cast_array_type := c.table.unaliased_type(base_to_type).clear_option_and_result() + cast_array_sym := c.table.sym(cast_array_type) + if cast_array_sym.kind == .array { + node.expr.typ = cast_array_type + node.expr.elem_type = cast_array_sym.array_info().elem_type + } + } old_inside_integer_literal_cast := c.inside_integer_literal_cast c.inside_integer_literal_cast = to_type.is_int() && node.expr is ast.IntegerLiteral node.expr_type = c.expr(mut node.expr) // type to be casted diff --git a/vlib/v/tests/casts/cast_to_array_test.v b/vlib/v/tests/casts/cast_to_array_test.v index 0dd1847f6..deb7a2fa2 100644 --- a/vlib/v/tests/casts/cast_to_array_test.v +++ b/vlib/v/tests/casts/cast_to_array_test.v @@ -2,6 +2,7 @@ module main type Bytes = [3]u8 type Str = []u8 +type NumOrText = int | string fn test_cast_to_fixed_array() { mut x := Bytes{} @@ -34,3 +35,13 @@ fn test_cast_to_array() { dump(z) assert z.str() == 'Str([10, 20, 30])' } + +fn test_cast_array_literal_to_array_type() { + values := []u32([10, 5, 10]) + assert values == [u32(10), 5, 10] +} + +fn test_cast_array_literal_to_sumtype_array() { + values := []NumOrText([1, 'x', 2]) + assert values == [NumOrText(1), 'x', 2] +} -- 2.39.5