From 3ecbf78707ebca9c67ea2210b296b6affc4e0bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Thu, 21 Jan 2021 12:43:54 +0100 Subject: [PATCH] cgen: support explicit array handling even in [direct_array_access] functions (#8241) --- vlib/v/gen/cgen.v | 2 +- vlib/v/tests/array_map_or_test.v | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index c12ad093dd..d2c897d17d 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -4105,7 +4105,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) { g.write('.val') } } - if is_direct_array_access { + if is_direct_array_access && !gen_or { if left_is_ptr && !node.left_type.has_flag(.shared_f) { g.write('->') } else { diff --git a/vlib/v/tests/array_map_or_test.v b/vlib/v/tests/array_map_or_test.v index d2b5c04772..a7f60f0ebe 100644 --- a/vlib/v/tests/array_map_or_test.v +++ b/vlib/v/tests/array_map_or_test.v @@ -14,6 +14,23 @@ fn test_array_or() { assert good == 4 } +[direct_array_access] +fn test_array_or_direct() { + m := [3, 4, 5] + mut testvar := 17 + el := m[4] or { + testvar = -43 + 999 + } + good := m[1] or { + testvar = 11 + 0 + } + assert testvar == -43 + assert el == 999 + assert good == 4 +} + fn test_map_or() { m := { 'as': 3 @@ -47,6 +64,13 @@ fn get_arr_el(i int) ?int { return r } +[direct_array_access] +fn get_arr_el_direct(i int) ?int { + m := [3, 4, 5] + r := m[i] ? + return r +} + fn test_propagation() { mut testvar1 := 12 mut testvar2 := 78 @@ -66,12 +90,20 @@ fn test_propagation() { testvar2 = 177 int(-67) } + m := get_arr_el_direct(3) or { + 17 + } + n := get_arr_el_direct(0) or { + int(-73) + } assert testvar1 == -34 assert testvar2 == 99 assert e == 7 assert f == 3 assert g == 12 assert h == 3 + assert m == 17 + assert n == 3 } fn get_arr_el_nested(i int) ?int {