From 7dde82322afc57ba928ce52a1eb2ffc97305665b Mon Sep 17 00:00:00 2001 From: Yarila682 Date: Sat, 22 Aug 2020 13:48:09 +0300 Subject: [PATCH] cgen: fix dereferencing a mutable array (#6191) --- vlib/v/gen/cgen.v | 3 +++ vlib/v/tests/array_mut_slicing_test.v | 8 ++++++++ 2 files changed, 11 insertions(+) create mode 100644 vlib/v/tests/array_mut_slicing_test.v diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 331154f3b0..2ebcfa1de0 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -2766,6 +2766,9 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) { g.expr(node.left) } else if sym.kind == .array { g.write('array_slice(') + if node.left_type.is_ptr() { + g.write('*') + } g.expr(node.left) } else if sym.kind == .array_fixed { // Convert a fixed array to V array when doing `fixed_arr[start..end]` diff --git a/vlib/v/tests/array_mut_slicing_test.v b/vlib/v/tests/array_mut_slicing_test.v new file mode 100644 index 0000000000..6019dcdd16 --- /dev/null +++ b/vlib/v/tests/array_mut_slicing_test.v @@ -0,0 +1,8 @@ +fn array_mut_slice(mut a []int) { + assert a[1..3].map(it) == [3, 5] +} + +fn test_array_mut_slice() { + mut a := [1, 3, 5, 7, 9] + array_mut_slice(mut a) +}