From 672066b65b766cfeeb0eb6a278c3645f844c743b Mon Sep 17 00:00:00 2001 From: StunxFS <56417208+StunxFS@users.noreply.github.com> Date: Sat, 13 Aug 2022 04:58:31 -0400 Subject: [PATCH] cgen: fix optional indexes with mutable arrays (#15399) --- vlib/v/gen/c/index.v | 3 +++ vlib/v/tests/array_index_optional.v | 23 +++++++++++++++++++ .../array_index_optional_with_if_expr_test.v | 7 ------ 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 vlib/v/tests/array_index_optional.v delete mode 100644 vlib/v/tests/array_index_optional_with_if_expr_test.v diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index 2b88741601..c9f122890c 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -252,6 +252,9 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) { tmp_opt_ptr := if gen_or { g.new_tmp_var() } else { '' } if gen_or { g.write('$elem_type_str* $tmp_opt_ptr = ($elem_type_str*)/*ee elem_ptr_typ */(array_get_with_check(') + if left_is_ptr && !node.left_type.has_flag(.shared_f) { + g.write('*') + } } else { if needs_clone { g.write('/*2*/string_clone(') diff --git a/vlib/v/tests/array_index_optional.v b/vlib/v/tests/array_index_optional.v new file mode 100644 index 0000000000..90c6512237 --- /dev/null +++ b/vlib/v/tests/array_index_optional.v @@ -0,0 +1,23 @@ +struct Arrs { +mut: + x [][]int +} + +fn test_mut_array_index_optional() { + mut arr := Arrs{ + x: [[1, 2]] + } + for mut sub_arr in arr.x { + x := sub_arr[0] or { 3 } + println(x) + assert x == 1 + } +} + +fn test_array_index_optional_with_if_expr() { + ret := []string{}[0] or { + if true { 'a' } else { 'b' } + } + println(ret) + assert ret == 'a' +} diff --git a/vlib/v/tests/array_index_optional_with_if_expr_test.v b/vlib/v/tests/array_index_optional_with_if_expr_test.v deleted file mode 100644 index 19002ac0e6..0000000000 --- a/vlib/v/tests/array_index_optional_with_if_expr_test.v +++ /dev/null @@ -1,7 +0,0 @@ -fn test_array_index_optional_with_if_expr() { - ret := []string{}[0] or { - if true { 'a' } else { 'b' } - } - println(ret) - assert ret == 'a' -}