parser: disallow defining map key more than once (#20905)

This commit is contained in:
Swastik Baranwal 2024-02-26 02:09:56 +05:30 committed by GitHub
parent 9703029dbe
commit 43e96ce5bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 12 additions and 0 deletions

View file

@ -140,6 +140,12 @@ fn (mut p Parser) parse_map_type() ast.Type {
return 0
}
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())
return 0
}
}
value_type := p.parse_type()
if value_type.idx() == 0 {
// error is reported in parse_type

View file

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

View file

@ -0,0 +1,2 @@
_ := map[u8][u8]{}