cgen: fix map cross assign (#5603)

pull/5604/head
yuyi 2020-07-01 20:03:12 +08:00 committed by GitHub
parent 673fe98cf5
commit 92eea7f95a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -303,3 +303,10 @@ fn test_map_keys_to_array() {
assert sarr == "['a', 'c']"
}
fn test_map_cross_assign() {
mut a := {'one':1, 'two':2}
a['one'], a['two'] = a['two'], a['one']
println(a)
assert a['one'] == 2
assert a['two'] == 1
}

View File

@ -1099,6 +1099,15 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
g.write(', ')
g.expr(it.index)
g.writeln(');')
} else if sym.kind == .map {
info := sym.info as table.Map
styp := g.typ(info.value_type)
zero := g.type_default(info.value_type)
g.write('$styp _var_$left.pos.pos = *($styp*)map_get(')
g.expr(it.left)
g.write(', ')
g.expr(it.index)
g.writeln(', &($styp[]){ $zero });')
}
}
else {}