From ce771876a38a94064e834f5b2a2499504ec63257 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 6 Jun 2022 11:29:22 +0800 Subject: [PATCH] cgen: fix nested map index check (fix #14683) (#14687) --- vlib/v/gen/c/index.v | 2 +- vlib/v/tests/nested_map_index_test.v | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/nested_map_index_test.v diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index d70caa28fe..f92fb8f759 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -484,7 +484,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) { if !node.is_option { g.or_block(tmp_opt, node.or_expr, elem_type) } - g.write('\n$cur_line*($elem_type_str*)${tmp_opt}.data') + g.write('\n${cur_line}(*($elem_type_str*)${tmp_opt}.data)') } } } diff --git a/vlib/v/tests/nested_map_index_test.v b/vlib/v/tests/nested_map_index_test.v new file mode 100644 index 0000000000..cbee977577 --- /dev/null +++ b/vlib/v/tests/nested_map_index_test.v @@ -0,0 +1,24 @@ +struct Foo { +mut: + foo map[int]Bar +} + +struct Bar { +mut: + bar map[int]string +} + +fn test_nested_map_index() ? { + f := Foo{ + foo: { + 11: Bar{ + bar: { + 22: 'hello' + } + } + } + } + ret := f.foo[11]?.bar[22]? + println(ret) + assert ret == 'hello' +}