1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: improve error for fixed array, when it has len and cap attributes in the initialisation list (#16120)

This commit is contained in:
Swastik Baranwal 2022-10-20 16:06:13 +05:30 committed by GitHub
parent ffcdac4201
commit ac63fa1b11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 2 deletions

View File

@ -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: '' } }

View File

@ -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)

View File

@ -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()

View File

@ -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
}