From 5fe4888874beadbdaf3fd17f5bc8bbc7f8766122 Mon Sep 17 00:00:00 2001 From: shove Date: Wed, 2 Nov 2022 22:04:22 +0800 Subject: [PATCH] parser: fix raw string as map keys (fix #16285) (#16289) --- vlib/v/parser/containers.v | 9 +++++++-- vlib/v/tests/map_test.v | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/map_test.v diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index 481c0af2ef..7f3c624888 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -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 diff --git a/vlib/v/tests/map_test.v b/vlib/v/tests/map_test.v new file mode 100644 index 0000000000..64d0e2e4c2 --- /dev/null +++ b/vlib/v/tests/map_test.v @@ -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 +}