mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: disallow _ = <- quit
(#18104)
This commit is contained in:
parent
43093311b6
commit
5bcc04e66a
@ -3609,6 +3609,12 @@ fn (mut c Checker) select_expr(mut node ast.SelectExpr) ast.Type {
|
||||
c.error('`<-` receive expression expected', branch.stmt.right[0].pos())
|
||||
}
|
||||
}
|
||||
if mut branch.stmt.left[0] is ast.Ident {
|
||||
ident := branch.stmt.left[0] as ast.Ident
|
||||
if ident.kind == .blank_ident && branch.stmt.op != .decl_assign {
|
||||
c.error('cannot send on `_`, use `_ := <- quit` instead', branch.stmt.left[0].pos())
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if !branch.is_else {
|
||||
|
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/blank_ident_select_branch_send_err.vv:13:4: error: cannot send on `_`, use `_ := <- quit` instead
|
||||
11 | x, y = y, x + y
|
||||
12 | }
|
||||
13 | _ = <-quit {
|
||||
| ^
|
||||
14 | println('quit')
|
||||
15 | return
|
39
vlib/v/checker/tests/blank_ident_select_branch_send_err.vv
Normal file
39
vlib/v/checker/tests/blank_ident_select_branch_send_err.vv
Normal file
@ -0,0 +1,39 @@
|
||||
import time
|
||||
|
||||
fn fibonacci(c chan int, quit chan int) {
|
||||
println('fibonacci')
|
||||
mut x, mut y := 0, 1
|
||||
|
||||
for {
|
||||
select {
|
||||
c <- x {
|
||||
println('send')
|
||||
x, y = y, x + y
|
||||
}
|
||||
_ = <-quit {
|
||||
println('quit')
|
||||
return
|
||||
}
|
||||
2 * time.second {
|
||||
println('timeout')
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
ch := chan int{}
|
||||
quit := chan int{}
|
||||
|
||||
spawn fn [ch, quit] () {
|
||||
for _ in 0 .. 5 {
|
||||
println('pop')
|
||||
println(<-ch)
|
||||
}
|
||||
println('quit')
|
||||
quit <- 0
|
||||
}()
|
||||
|
||||
fibonacci(ch, quit)
|
||||
}
|
Loading…
Reference in New Issue
Block a user