parser: detect wrong multi-dim arr declare (fix #25333) (#25338)

This commit is contained in:
kbkpbot 2025-09-18 21:35:13 +08:00 committed by GitHub
parent c088cbdbd5
commit f2abf79f71
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 9 deletions

View File

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

View File

@ -0,0 +1,9 @@
module main
fn main() {
a2 := [][][]f32[][]{}
dump(typeof(a2).name)
assert typeof(a2).name == '[][][]f32'
}

View File

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