From ac63fa1b115f6318c9800251d66908142d681011 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Thu, 20 Oct 2022 16:06:13 +0530 Subject: [PATCH] parser: improve error for fixed array, when it has `len` and `cap` attributes in the initialisation list (#16120) --- vlib/v/checker/tests/with_check_option/v_tictactoe.out | 2 +- vlib/v/parser/containers.v | 7 ++++++- vlib/v/parser/tests/fixed_arr_len_cap_attr_err.out | 6 ++++++ vlib/v/parser/tests/fixed_arr_len_cap_attr_err.vv | 7 +++++++ 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 vlib/v/parser/tests/fixed_arr_len_cap_attr_err.out create mode 100644 vlib/v/parser/tests/fixed_arr_len_cap_attr_err.vv diff --git a/vlib/v/checker/tests/with_check_option/v_tictactoe.out b/vlib/v/checker/tests/with_check_option/v_tictactoe.out index f41153ee5..8682a2439 100644 --- a/vlib/v/checker/tests/with_check_option/v_tictactoe.out +++ b/vlib/v/checker/tests/with_check_option/v_tictactoe.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/with_check_option/v_tictactoe.vv:4:31: error: expected `init:`, not `len` +vlib/v/checker/tests/with_check_option/v_tictactoe.vv:4:31: error: `len` and `cap` are invalid attributes for fixed array dimension 2 | 3 | fn new_board() [][]string { 4 | mut board := [3][]string{ len: 3, init: []string{ len: 3, init: '' } } diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index bc2847fbb..481c0af2e 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -80,7 +80,12 @@ fn (mut p Parser) array_init() ast.ArrayInit { pos := p.tok.pos() n := p.check_name() if n != 'init' { - p.error_with_pos('expected `init:`, not `$n`', pos) + if is_fixed { + p.error_with_pos('`len` and `cap` are invalid attributes for fixed array dimension', + pos) + } else { + p.error_with_pos('expected `init:`, not `$n`', pos) + } return ast.ArrayInit{} } p.check(.colon) diff --git a/vlib/v/parser/tests/fixed_arr_len_cap_attr_err.out b/vlib/v/parser/tests/fixed_arr_len_cap_attr_err.out new file mode 100644 index 000000000..8a96a6d2a --- /dev/null +++ b/vlib/v/parser/tests/fixed_arr_len_cap_attr_err.out @@ -0,0 +1,6 @@ +vlib/v/parser/tests/fixed_arr_len_cap_attr_err.vv:2:31: error: `len` and `cap` are invalid attributes for fixed array dimension + 1 | fn new_board() [][]string { + 2 | mut board := [3][]string{ len: 3, init: []string{ len: 3, init: '' } } + | ~~~ + 3 | for i in 0..9 { + 4 | board[i / 3][i % 3] = (i + 1).str() diff --git a/vlib/v/parser/tests/fixed_arr_len_cap_attr_err.vv b/vlib/v/parser/tests/fixed_arr_len_cap_attr_err.vv new file mode 100644 index 000000000..a35bea551 --- /dev/null +++ b/vlib/v/parser/tests/fixed_arr_len_cap_attr_err.vv @@ -0,0 +1,7 @@ +fn new_board() [][]string { + mut board := [3][]string{ len: 3, init: []string{ len: 3, init: '' } } + for i in 0..9 { + board[i / 3][i % 3] = (i + 1).str() + } + return board +} -- 2.39.5