From 56228f3de657ad68eae8c9976bc468923463d9c7 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Sat, 3 Jan 2026 12:26:07 +0530 Subject: [PATCH] parser: disallow `[3]!int{}` (fix #26244) (#26249) --- vlib/v/parser/checks.v | 2 +- vlib/v/parser/containers.v | 6 +++++- vlib/v/parser/tests/fixed_array_init_result_err.out | 3 +++ vlib/v/parser/tests/fixed_array_init_result_err.vv | 1 + 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 vlib/v/parser/tests/fixed_array_init_result_err.out create mode 100644 vlib/v/parser/tests/fixed_array_init_result_err.vv diff --git a/vlib/v/parser/checks.v b/vlib/v/parser/checks.v index cfb9daf7e..1598aa791 100644 --- a/vlib/v/parser/checks.v +++ b/vlib/v/parser/checks.v @@ -73,7 +73,7 @@ fn (p &Parser) is_array_type() bool { if tok.kind in [.name, .amp] { return true } - if tok.kind == .eof { + if tok.kind in [.eof, .colon, .dot] { break } i++ diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index 0dfef0ab8..218f2b4ad 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -119,7 +119,11 @@ fn (mut p Parser) array_init(is_option bool, alias_array_type ast.Type) ast.Arra last_pos = p.tok.pos() is_fixed = true has_val = true - p.next() + if exprs.len == 1 && p.tok.line_nr == line_nr && p.is_array_type() { + p.error('fixed arrays do not support storing Result values') + } else { + p.next() + } } if p.tok.kind == .not && p.tok.line_nr == p.prev_tok.line_nr { last_pos = p.tok.pos() diff --git a/vlib/v/parser/tests/fixed_array_init_result_err.out b/vlib/v/parser/tests/fixed_array_init_result_err.out new file mode 100644 index 000000000..3f77da70c --- /dev/null +++ b/vlib/v/parser/tests/fixed_array_init_result_err.out @@ -0,0 +1,3 @@ +vlib/v/parser/tests/fixed_array_init_result_err.vv:1:8: error: fixed arrays do not support storing Result values + 1 | _ = [3]!int{} + | ^ diff --git a/vlib/v/parser/tests/fixed_array_init_result_err.vv b/vlib/v/parser/tests/fixed_array_init_result_err.vv new file mode 100644 index 000000000..9f9b8f01d --- /dev/null +++ b/vlib/v/parser/tests/fixed_array_init_result_err.vv @@ -0,0 +1 @@ +_ = [3]!int{} -- 2.39.5