diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index 83938014f4..2b88741601 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -327,15 +327,27 @@ fn (mut g Gen) index_of_fixed_array(node ast.IndexExpr, sym ast.TypeSymbol) { elem_sym := g.table.sym(elem_type) is_fn_index_call := g.is_fn_index_call && elem_sym.info is ast.FnType - if is_fn_index_call { - g.write('(*') - } - if node.left_type.is_ptr() { - g.write('(*') + if node.left is ast.ArrayInit { + tmp := g.new_tmp_var() + line := g.go_before_stmt(0).trim_space() + styp := g.typ(node.left_type) + g.empty_line = true + g.write('$styp $tmp = ') g.expr(node.left) - g.write(')') + g.writeln(';') + g.write(line) + g.write(tmp) } else { - g.expr(node.left) + if is_fn_index_call { + g.write('(*') + } + if node.left_type.is_ptr() { + g.write('(*') + g.expr(node.left) + g.write(')') + } else { + g.expr(node.left) + } } g.write('[') direct := unsafe { g.fn_decl != 0 } && g.fn_decl.is_direct_arr diff --git a/vlib/v/tests/fixed_array_literal_index_test.v b/vlib/v/tests/fixed_array_literal_index_test.v new file mode 100644 index 0000000000..aab5bc9046 --- /dev/null +++ b/vlib/v/tests/fixed_array_literal_index_test.v @@ -0,0 +1,7 @@ +fn test_fixed_array_literal_index() { + println([1]int{}[0]) + assert [1]int{}[0] == 0 + + println([1, 2]![1]) + assert [1, 2]![1] == 2 +}