Skip to content

Commit f6de36d

Browse files
authored
parser: sugggest map initialization with the correct syntax (#21817)
1 parent df18047 commit f6de36d

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

vlib/v/parser/parse_type.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ fn (mut p Parser) parse_map_type() ast.Type {
163163
p.check(.rsbr)
164164
if p.tok.kind == .lsbr {
165165
if p.peek_tok.kind !in [.rsbr, .number] {
166-
p.error_with_pos('maps can only have a single key', p.peek_tok.pos())
166+
s := p.table.type_to_str(key_type)
167+
p.error_with_pos('maps can only have a single key. To declare a map use `map[${s}]${p.peek_tok.lit}{}` instead',
168+
p.peek_tok.pos())
167169
return 0
168170
}
169171
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
vlib/v/parser/tests/map_key_twice_err.vv:1:14: error: maps can only have a single key
1+
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
22
1 | _ := map[u8][u8]{}
33
| ~~
44
2 |

0 commit comments

Comments
 (0)