From 078f498b171f3ac002509620eed0c3d73b421a71 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 20 Mar 2020 17:03:41 +0100 Subject: [PATCH] cgen: fix `vals[i].field = x` --- vlib/v/gen/cgen.v | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 689b0fd8dc..4b88cdba90 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1274,8 +1274,6 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) { } else {} } - // if !is_range && node.container_type == 0 { - // } if !is_range && node.container_type != 0 { sym := g.table.get_type_symbol(node.container_type) if table.type_is_variadic(node.container_type) { @@ -1288,7 +1286,16 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) { else if sym.kind == .array { info := sym.info as table.Array elem_type_str := g.typ(info.elem_type) - if g.is_assign_expr { + // `vals[i].field = x` is an exception and requires `array_get`: + // `(*(Val*)array_get(vals, i)).field = x ;` + mut is_selector := false + match node.left { + ast.SelectorExpr { + is_selector = true + } + else {} + } + if g.is_assign_expr && !is_selector { g.is_array_set = true g.write('array_set(&') g.expr(node.left)