cgen: fix error of 'map_a[map_b[key]] += 2' (#12872)

pull/12885/head
yuyi 2021-12-17 21:17:08 +08:00 committed by GitHub
parent c9f6a96936
commit 66070ec63e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -350,7 +350,13 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
g.write('->val')
}
g.write(', &($key_type_str[]){')
old_is_arraymap_set := g.is_arraymap_set
old_is_assign_lhs := g.is_assign_lhs
g.is_arraymap_set = false
g.is_assign_lhs = false
g.expr(node.index)
g.is_arraymap_set = old_is_arraymap_set
g.is_assign_lhs = old_is_assign_lhs
g.write('}')
if elem_typ.kind == .function {
g.write(', &(voidptr[]) { ')

View File

@ -32,3 +32,15 @@ fn test_map_of_map_to_struct() {
foos['zza']['zzb'].name = 'baz'
assert foos['zza']['zzb'].name == 'baz'
}
fn test_map_of_map_key_plus_assign() {
m := {
'a': 'x'
}
mut n := {
'x': 1
}
n[m['a']] += 2
println(n)
assert n['x'] == 3
}