cgen: fix error of for_in map_literal (#9310)

pull/9314/head
yuyi 2021-03-15 18:22:32 +08:00 committed by GitHub
parent 3951c351c6
commit e235022e10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -31,6 +31,12 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
p.inside_match = true // reuse the same var for perf instead of inside_sql TODO rename p.inside_match = true // reuse the same var for perf instead of inside_sql TODO rename
node = p.sql_expr() node = p.sql_expr()
p.inside_match = false p.inside_match = false
} else if p.tok.lit == 'map' && p.peek_tok.kind == .lcbr && !(p.builtin_mod
&& p.file_base == 'map.v') {
p.next() // `map`
p.next() // `{`
node = p.map_init()
p.check(.rcbr) // `}`
} else { } else {
if p.inside_if && p.is_generic_name() { if p.inside_if && p.is_generic_name() {
// $if T is string {} // $if T is string {}

View File

@ -75,6 +75,18 @@ fn test_for_in_map_of_fixed_array() {
assert rets[2] == 'cc, [5, 6]' assert rets[2] == 'cc, [5, 6]'
} }
fn test_for_in_map_of_fixed_array_literal() {
mut rets := []string{}
for k, v in map{'aa': [1, 2]!, 'bb': [3, 4]!, 'cc': [5, 6]!} {
println('$k, $v')
rets << '$k, $v'
}
assert rets[0] == 'aa, [1, 2]'
assert rets[1] == 'bb, [3, 4]'
assert rets[2] == 'cc, [5, 6]'
}
fn test_for_mut_in_map_of_fixed_array() { fn test_for_mut_in_map_of_fixed_array() {
mut rets := []string{} mut rets := []string{}
mut m := map{'aa': [1, 2]!, 'bb': [3, 4]!, 'cc': [5, 6]!} mut m := map{'aa': [1, 2]!, 'bb': [3, 4]!, 'cc': [5, 6]!}