Skip to content

Commit

Permalink
parser: disallow defining map key more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Feb 25, 2024
1 parent 8f604f8 commit 7e8f54d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/v/parser/parse_type.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 != .rsbr {
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
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/parser/tests/map_key_twice_err.out
Original file line number Diff line number Diff line change
@@ -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 |
2 changes: 2 additions & 0 deletions vlib/v/parser/tests/map_key_twice_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_ := map[u8][u8]{}

0 comments on commit 7e8f54d

Please sign in to comment.