1
0
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:
Swastik Baranwal 2023-05-04 23:53:34 +05:30 committed by GitHub
parent 43093311b6
commit 5bcc04e66a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 0 deletions

View File

@ -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 {

View File

@ -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

View 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)
}