Skip to content

Commit

Permalink
checker: allow pass array as mut param to spawn fn (#21283)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Apr 24, 2024
1 parent 0a14bef commit be90cf3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions vlib/v/checker/fn.v
Expand Up @@ -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())
}
Expand Down
14 changes: 14 additions & 0 deletions 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()
}

0 comments on commit be90cf3

Please sign in to comment.