checker: allow pass array as mut param to spawn fn (#21283)

This commit is contained in:
Felipe Pena 2024-04-24 00:48:35 -03:00 committed by GitHub
parent 0a14bef008
commit be90cf3165
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 0 deletions

View file

@ -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())
}

View file

@ -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()
}