From be90cf31653c21ff114e3d95f4c2d0a19f17c8d4 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 24 Apr 2024 00:48:35 -0300 Subject: [PATCH] checker: allow pass array as mut param to spawn fn (#21283) --- vlib/v/checker/fn.v | 4 ++++ vlib/v/tests/spawn_array_mut_test.v | 14 ++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 vlib/v/tests/spawn_array_mut_test.v diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index a4e84873c7..898afc1bd9 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -2516,6 +2516,10 @@ fn (mut c Checker) spawn_expr(mut node ast.SpawnExpr) ast.Type { // Make sure there are no mutable arguments for arg in node.call_expr.args { if arg.is_mut && !arg.typ.is_ptr() { + if c.table.final_sym(arg.typ).kind == .array { + // allow mutable []array to be passed as mut + continue + } c.error('function in `spawn` statement cannot contain mutable non-reference arguments', arg.expr.pos()) } diff --git a/vlib/v/tests/spawn_array_mut_test.v b/vlib/v/tests/spawn_array_mut_test.v new file mode 100644 index 0000000000..efc66a623e --- /dev/null +++ b/vlib/v/tests/spawn_array_mut_test.v @@ -0,0 +1,14 @@ +fn mutable_array_arg(mut a []int) { + println(a) + a << 4 + assert a.len == 4 +} + +fn test_main() { + mut a := []int{} + a << 1 + a << 2 + a << 3 + w := spawn mutable_array_arg(mut a) + w.wait() +}