Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: sugggest map initialization with the correct syntax #21817

Merged
merged 3 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion vlib/v/parser/parse_type.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/parser/tests/map_key_twice_err.out
Original file line number Diff line number Diff line change
@@ -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 |
Loading