fmt: enable `or` block for `ch <- x` (#7969)

pull/7972/head
Uwe Krüger 2021-01-08 21:39:21 +01:00 committed by GitHub
parent 2e02a13227
commit 29c2240aef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 5 deletions

View File

@ -4,13 +4,13 @@ const (
num_iterations = 10000
)
fn get_val_from_chan(ch chan i64) ?i64 {
fn get_val_from_chan(ch chan int) ?int {
r := <-ch ?
return r
}
// this function gets an array of channels for `i64`
fn do_rec_calc_send(chs []chan i64, sem sync.Semaphore) {
// this function gets an array of channels for `int`
fn do_rec_calc_send(chs []chan int, sem sync.Semaphore) {
mut msg := ''
for {
mut s := get_val_from_chan(chs[0]) or {
@ -25,10 +25,10 @@ fn do_rec_calc_send(chs []chan i64, sem sync.Semaphore) {
}
fn test_channel_array_mut() {
mut chs := [chan i64{}, chan i64{cap: 10}]
mut chs := [chan int{}, chan int{cap: 10}]
sem := sync.new_semaphore()
go do_rec_calc_send(chs, sem)
mut t := i64(100)
mut t := int(100)
for _ in 0 .. num_iterations {
chs[0] <- t
t = <-chs[1]

0
vlib/sync/channel_try_buf_test.v 100755 → 100644
View File

View File

@ -1564,6 +1564,7 @@ pub fn (mut f Fmt) infix_expr(node ast.InfixExpr) {
f.penalties = []int{}
f.precedences = []int{}
}
f.or_expr(node.or_block)
}
pub fn (mut f Fmt) if_expr(it ast.IfExpr) {

View File

@ -0,0 +1,33 @@
const n = 1000
fn f(ch chan int) {
mut s := 0
for _ in 0 .. n {
s += <-ch or {
println('Something went wrong:')
println('got $err')
}
}
assert s == n * (n + 1) / 2
ch.close()
}
fn do_send(ch chan int, val int) ?int {
ch <- val ?
return val + 1
}
fn do_send_2(ch chan int, val int) ?int {
ch <- val or { return error('could not send') }
return val + 1
}
fn main() {
ch := chan int{}
go f(ch)
mut s := 1
for {
s = do_send(ch, s) or { break }
}
assert s == n + 1
}