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

cgen: fix map_in error

This commit is contained in:
yuyi 2020-06-25 18:53:12 +08:00 committed by GitHub
parent 5e9c154376
commit f1e17f30f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -276,3 +276,10 @@ fn test_assign_directly() {
a['aaa'] -= 2
assert a['aaa'] == 2
}
fn test_map_in_directly() {
for k, v in {'aa': 1} {
assert k == 'aa'
assert v == 1
}
}

View File

@ -832,14 +832,16 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
idx := g.new_tmp_var()
key := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var }
zero := g.type_default(it.val_type)
g.write('array_$key_styp $keys_tmp = map_keys(&')
atmp := g.new_tmp_var()
atmp_styp := g.typ(it.cond_type)
g.write('$atmp_styp $atmp = ')
g.expr(it.cond)
g.writeln(');')
g.writeln(';')
g.writeln('array_$key_styp $keys_tmp = map_keys(&$atmp);')
g.writeln('for (int $idx = 0; $idx < ${keys_tmp}.len; $idx++) {')
g.writeln('\t$key_styp $key = (($key_styp*)${keys_tmp}.data)[$idx];')
if it.val_var != '_' {
g.write('\t$val_styp ${c_name(it.val_var)} = (*($val_styp*)map_get(')
g.expr(it.cond)
g.write('\t$val_styp ${c_name(it.val_var)} = (*($val_styp*)map_get($atmp')
g.writeln(', $key, &($val_styp[]){ $zero }));')
}
g.stmts(it.stmts)