1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: fix raw string as map keys (fix #16285) (#16289)

This commit is contained in:
shove 2022-11-02 22:04:22 +08:00 committed by GitHub
parent 075c025999
commit 5fe4888874
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -208,8 +208,13 @@ fn (mut p Parser) map_init() ast.MapInit {
mut comments := [][]ast.Comment{}
pre_cmnts := p.eat_comments()
for p.tok.kind !in [.rcbr, .eof] {
key := p.expr(0)
keys << key
if p.tok.kind == .name && p.tok.lit in ['r', 'c', 'js'] {
key := p.string_expr()
keys << key
} else {
key := p.expr(0)
keys << key
}
p.check(.colon)
val := p.expr(0)
vals << val

10
vlib/v/tests/map_test.v Normal file
View File

@ -0,0 +1,10 @@
// For issue 16285 Raw string in map literal key triggers compiler error
fn test_raw_string_as_key() {
mut m := {
r'key': 1
}
assert m[r'key'] == 1
m[r'key'] = 2
assert m[r'key'] == 2
}