From 86496aa191529d2436288dcb8f0b7845a71d81e8 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 25 Aug 2022 13:00:11 +0800 Subject: [PATCH] cgen: fix array init with it (#15523) --- vlib/v/gen/c/array.v | 2 ++ vlib/v/tests/array_with_it_test.v | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 7996558b6f..92f75f96a5 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -246,6 +246,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp g.indent++ g.writeln('$elem_typ* pelem = ($elem_typ*)${tmp}.data;') g.writeln('for(int it=0; it<${tmp}.len; it++, pelem++) {') + g.set_current_pos_as_last_stmt_pos() g.indent++ g.write('*pelem = ') g.expr(node.default_expr) @@ -254,6 +255,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp g.writeln('}') g.indent-- g.writeln('}') + g.set_current_pos_as_last_stmt_pos() if var_name.len == 0 { if s_ends_with_ln { g.writeln(s) diff --git a/vlib/v/tests/array_with_it_test.v b/vlib/v/tests/array_with_it_test.v index 3e7e53dd93..1117407a62 100644 --- a/vlib/v/tests/array_with_it_test.v +++ b/vlib/v/tests/array_with_it_test.v @@ -15,3 +15,12 @@ fn test_array_with_it() { a4 := []int{len: 5, init: 5 - it} assert a4 == [5, 4, 3, 2, 1] } + +fn test_array_init_with_optional() { + input := [3.1, 1.1] + arr := []f64{len: 3, init: input[it] or { 0.0 }} + println(arr) + assert arr[0] == 3.1 + assert arr[1] == 1.1 + assert arr[2] == 0.0 +}