From 29c2240aef309ed0245196207560e6711701a46a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Fri, 8 Jan 2021 21:39:21 +0100 Subject: [PATCH] fmt: enable `or` block for `ch <- x` (#7969) --- vlib/sync/channel_opt_propagate_test.v | 10 ++++---- vlib/sync/channel_try_buf_test.v | 0 vlib/v/fmt/fmt.v | 1 + vlib/v/fmt/tests/chan_or_keep.vv | 33 ++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) mode change 100755 => 100644 vlib/sync/channel_try_buf_test.v create mode 100644 vlib/v/fmt/tests/chan_or_keep.vv diff --git a/vlib/sync/channel_opt_propagate_test.v b/vlib/sync/channel_opt_propagate_test.v index 4bb4138aee..42867deef0 100644 --- a/vlib/sync/channel_opt_propagate_test.v +++ b/vlib/sync/channel_opt_propagate_test.v @@ -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] diff --git a/vlib/sync/channel_try_buf_test.v b/vlib/sync/channel_try_buf_test.v old mode 100755 new mode 100644 diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index b4822bae46..2ce1a3eed5 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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) { diff --git a/vlib/v/fmt/tests/chan_or_keep.vv b/vlib/v/fmt/tests/chan_or_keep.vv new file mode 100644 index 0000000000..a6e7a90625 --- /dev/null +++ b/vlib/v/fmt/tests/chan_or_keep.vv @@ -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 +}