From f2abf79f716c5d12b93ef69e7154e21d07863451 Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Thu, 18 Sep 2025 21:35:13 +0800 Subject: [PATCH] parser: detect wrong multi-dim arr declare (fix #25333) (#25338) --- .../tests/wrong_muti_dim_arr_declare_err.out | 14 ++++++++++++++ .../tests/wrong_muti_dim_arr_declare_err.vv | 9 +++++++++ vlib/v/parser/parse_type.v | 10 +--------- 3 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 vlib/v/checker/tests/wrong_muti_dim_arr_declare_err.out create mode 100644 vlib/v/checker/tests/wrong_muti_dim_arr_declare_err.vv diff --git a/vlib/v/checker/tests/wrong_muti_dim_arr_declare_err.out b/vlib/v/checker/tests/wrong_muti_dim_arr_declare_err.out new file mode 100644 index 000000000..743817ee0 --- /dev/null +++ b/vlib/v/checker/tests/wrong_muti_dim_arr_declare_err.out @@ -0,0 +1,14 @@ +vlib/v/checker/tests/wrong_muti_dim_arr_declare_err.vv:4:15: warning: use `x := []Type{}` instead of `x := []Type` + 2 | + 3 | fn main() { + 4 | a2 := [][][]f32[][]{} + | ~~~~~~~~~~ + 5 | dump(typeof(a2).name) + 6 | +vlib/v/checker/tests/wrong_muti_dim_arr_declare_err.vv:4:25: error: invalid expression: unexpected token `]` + 2 | + 3 | fn main() { + 4 | a2 := [][][]f32[][]{} + | ^ + 5 | dump(typeof(a2).name) + 6 | diff --git a/vlib/v/checker/tests/wrong_muti_dim_arr_declare_err.vv b/vlib/v/checker/tests/wrong_muti_dim_arr_declare_err.vv new file mode 100644 index 000000000..8ad2d6d57 --- /dev/null +++ b/vlib/v/checker/tests/wrong_muti_dim_arr_declare_err.vv @@ -0,0 +1,9 @@ +module main + +fn main() { + a2 := [][][]f32[][]{} + dump(typeof(a2).name) + + assert typeof(a2).name == '[][][]f32' +} + diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 6817e5d62..774e18d73 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -111,15 +111,7 @@ fn (mut p Parser) parse_array_type(expecting token.Kind, is_option bool) ast.Typ if elem_type.idx() == ast.thread_type_idx { p.register_auto_import('sync.threads') } - mut nr_dims := 1 - // detect attr - not_attr := p.peek_tok.kind != .name && p.peek_token(2).kind !in [.semicolon, .rsbr] - for p.tok.kind == expecting && not_attr { - p.next() - p.check(.rsbr) - nr_dims++ - } - idx := p.table.find_or_register_array_with_dims(elem_type, nr_dims) + idx := p.table.find_or_register_array_with_dims(elem_type, 1) if elem_type.has_flag(.generic) { return ast.new_type(idx).set_flag(.generic) } -- 2.39.5