diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index 99a7f78165..f57ade3d93 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -449,7 +449,10 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) { g.write('->val') } g.write(', &(${key_type_str}[]){') + old_is_assign_lhs := g.is_assign_lhs + g.is_assign_lhs = false g.expr(node.index) + g.is_assign_lhs = old_is_assign_lhs g.write('}, &(${elem_type_str}[]){ ${zero} }))') } else { zero := g.type_default(info.value_type) diff --git a/vlib/v/tests/result_with_index_expr_test.v b/vlib/v/tests/result_with_index_expr_test.v new file mode 100644 index 0000000000..b11dff2ffd --- /dev/null +++ b/vlib/v/tests/result_with_index_expr_test.v @@ -0,0 +1,25 @@ +import x.json2 + +struct SomeStruct { + title string +} + +fn test_result_with_index() { + resp := '{ + "userId": 1, + "id": 1, + "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", + "body": "quia et suscipitsuscipit recusandae consequuntur expedita et cumreprehenderit molestiae ut ut quas totamnostrum rerum est autem sunt rem eveniet architecto" +}' + raw_data := json2.raw_decode(resp)! + + data := raw_data as map[string]json2.Any + + mut ss := map[int]SomeStruct{} + s := SomeStruct{ + title: data['title']!.str() + } + ss[data['id']!.int()] = s + t := ss[data['id']!.int()] + assert t.title == 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit' +}