From 66070ec63ef9d29fd5c3186a4e69651a698c13dd Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 17 Dec 2021 21:17:08 +0800 Subject: [PATCH] cgen: fix error of 'map_a[map_b[key]] += 2' (#12872) --- vlib/v/gen/c/index.v | 6 ++++++ vlib/v/tests/nested_map_test.v | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index ab975fc131..b025f13caa 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -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[]) { ') diff --git a/vlib/v/tests/nested_map_test.v b/vlib/v/tests/nested_map_test.v index 3cf12bc1dd..140cbce92e 100644 --- a/vlib/v/tests/nested_map_test.v +++ b/vlib/v/tests/nested_map_test.v @@ -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 +}