Skip to content

Commit

Permalink
cgen: fix error of 'map_a[map_b[key]] += 2' (#12872)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Dec 17, 2021
1 parent c9f6a96 commit 66070ec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/v/gen/c/index.v
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) { ')
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/tests/nested_map_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 66070ec

Please sign in to comment.