1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix fixed array of chan (#18438)

This commit is contained in:
Felipe Pena 2023-06-16 03:44:53 -03:00 committed by GitHub
parent 75f325b950
commit 06583be9ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

View File

@ -201,6 +201,21 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
elem_type: arr_info.elem_type elem_type: arr_info.elem_type
}) })
} }
} else if elem_sym.kind == .chan {
// fixed array for chan -- [N]chan
info := array_type.unaliased_sym.info as ast.ArrayFixed
chan_info := elem_sym.chan_info()
g.expr(ast.ChanInit{
typ: node.elem_type
elem_type: chan_info.elem_type
})
for _ in 1 .. info.size {
g.write(', ')
g.expr(ast.ChanInit{
typ: node.elem_type
elem_type: chan_info.elem_type
})
}
} else { } else {
g.write('0') g.write('0')
} }

View File

@ -0,0 +1,31 @@
import time
struct Struct {
ch chan int
}
fn listen(ch chan int) {
for {
t := <-ch or { break }
println(t)
}
}
fn sp_array() {
mut array := [3]chan int{}
for a in 0 .. 3 {
spawn listen(array[a])
}
for i, a in array {
a <- i
}
time.sleep(1000 * time.millisecond)
for c in array {
c.close()
}
}
fn test_main() {
sp_array()
assert true
}