parser: sugggest map initialization with the correct syntax (#21817)

This commit is contained in:
Swastik Baranwal 2024-07-07 18:32:38 +05:30 committed by GitHub
parent df18047347
commit f6de36db51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 2 deletions

View File

@ -163,7 +163,9 @@ fn (mut p Parser) parse_map_type() ast.Type {
p.check(.rsbr)
if p.tok.kind == .lsbr {
if p.peek_tok.kind !in [.rsbr, .number] {
p.error_with_pos('maps can only have a single key', p.peek_tok.pos())
s := p.table.type_to_str(key_type)
p.error_with_pos('maps can only have a single key. To declare a map use `map[${s}]${p.peek_tok.lit}{}` instead',
p.peek_tok.pos())
return 0
}
}

View File

@ -1,4 +1,4 @@
vlib/v/parser/tests/map_key_twice_err.vv:1:14: error: maps can only have a single key
vlib/v/parser/tests/map_key_twice_err.vv:1:14: error: maps can only have a single key. To declare a map use `map[u8]u8{}` instead
1 | _ := map[u8][u8]{}
| ~~
2 |