From a98d6446373abd0494031359b61f8a3a807e72b1 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 18 Jun 2021 07:51:55 +0800 Subject: [PATCH] cgen: fix fn mut args of array (#10493) --- vlib/v/gen/c/fn.v | 4 ++++ vlib/v/tests/fn_mut_args_test.v | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index b0cdce129a..12010ff673 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -760,6 +760,10 @@ fn (mut g Gen) method_call(node ast.CallExpr) { arg_name := '_arg_expr_${fn_name}_0_$node.pos.pos' g.write('/*af receiver arg*/' + arg_name) } else { + if left_sym.kind == .array && node.left.is_auto_deref_var() + && node.name in ['first', 'last', 'repeat'] { + g.write('*') + } g.expr(node.left) if node.from_embed_type != 0 { embed_name := typ_sym.embed_name() diff --git a/vlib/v/tests/fn_mut_args_test.v b/vlib/v/tests/fn_mut_args_test.v index 9f41b19461..4a0653083d 100644 --- a/vlib/v/tests/fn_mut_args_test.v +++ b/vlib/v/tests/fn_mut_args_test.v @@ -25,3 +25,21 @@ fn test_fn_mut_args_of_map() { 'one': 1 } } + +struct MyData { +pub mut: + ar []int +} + +fn pass_array_mut(mut ar []int) int { + if ar.len > 0 && ar.last() == 99 { + return 99 + } + return 0 +} + +fn test_fn_mut_args_of_array_last() { + mut m := MyData{} + m.ar << 99 + assert pass_array_mut(mut m.ar) == 99 +}