From 758c18a9066c9c168c019cbfe526e7a6a5b2bf77 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 8 Nov 2021 17:12:10 +0800 Subject: [PATCH] cgen: fix for in mut val in array.index() (#12410) --- vlib/v/gen/c/array.v | 3 +++ vlib/v/tests/for_in_mut_array_index_test.v | 12 ++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 vlib/v/tests/for_in_mut_array_index_test.v diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index c3a06d3ec6..d264a4c7c7 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -762,6 +762,9 @@ fn (mut g Gen) gen_array_index(node ast.CallExpr) { } g.expr(node.left) g.write(', ') + if node.args[0].expr.is_auto_deref_var() { + g.write('*') + } g.expr(node.args[0].expr) g.write(')') } diff --git a/vlib/v/tests/for_in_mut_array_index_test.v b/vlib/v/tests/for_in_mut_array_index_test.v new file mode 100644 index 0000000000..f4c628e880 --- /dev/null +++ b/vlib/v/tests/for_in_mut_array_index_test.v @@ -0,0 +1,12 @@ +fn test_for_in_mut_array_index() { + mut arr := [1, 2, 3, 4, 5] + mut rets := []int{} + + for mut val in arr { + inx := arr.index(val) + println(inx) + rets << inx + } + + assert rets == [0, 1, 2, 3, 4] +}