blob: 7da88bdae8a9c737903f607d2fd31d689f9ae52e [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// errorcheck
Russ Coxd33d7632009-05-22 09:53:25 -07002
3// Copyright 2009 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
Rob Pike3fb5f322012-02-19 17:44:02 +11007// Test various correct and incorrect permutations of send-only,
8// receive-only, and bidirectional channels.
9// Does not compile.
10
Russ Coxd33d7632009-05-22 09:53:25 -070011package main
12
13var (
Rob Pike4f61fc92010-09-04 10:36:13 +100014 cr <-chan int
15 cs chan<- int
Russ Coxf4e76d82011-01-31 18:36:28 -050016 c chan int
Russ Coxd33d7632009-05-22 09:53:25 -070017)
18
19func main() {
Russ Coxf4e76d82011-01-31 18:36:28 -050020 cr = c // ok
21 cs = c // ok
22 c = cr // ERROR "illegal types|incompatible|cannot"
23 c = cs // ERROR "illegal types|incompatible|cannot"
24 cr = cs // ERROR "illegal types|incompatible|cannot"
25 cs = cr // ERROR "illegal types|incompatible|cannot"
Russ Coxd33d7632009-05-22 09:53:25 -070026
Alberto Donizetti1737aef2017-04-22 15:28:58 +020027 var n int
28 <-n // ERROR "receive from non-chan"
29 n <- 2 // ERROR "send to non-chan"
30
Josh Bleecher Snyder26a85212017-05-18 14:53:12 -070031 c <- 0 // ok
32 <-c // ok
33 x, ok := <-c // ok
Russ Cox3f915f52011-03-11 14:47:44 -050034 _, _ = x, ok
Russ Coxd33d7632009-05-22 09:53:25 -070035
Josh Bleecher Snyder26a85212017-05-18 14:53:12 -070036 cr <- 0 // ERROR "send"
37 <-cr // ok
38 x, ok = <-cr // ok
Russ Cox3f915f52011-03-11 14:47:44 -050039 _, _ = x, ok
Russ Coxd33d7632009-05-22 09:53:25 -070040
Josh Bleecher Snyder26a85212017-05-18 14:53:12 -070041 cs <- 0 // ok
42 <-cs // ERROR "receive"
43 x, ok = <-cs // ERROR "receive"
Russ Cox3f915f52011-03-11 14:47:44 -050044 _, _ = x, ok
Russ Coxd33d7632009-05-22 09:53:25 -070045
46 select {
Russ Coxf4e76d82011-01-31 18:36:28 -050047 case c <- 0: // ok
48 case x := <-c: // ok
Rob Pike4f61fc92010-09-04 10:36:13 +100049 _ = x
Russ Coxd33d7632009-05-22 09:53:25 -070050
Russ Coxf4e76d82011-01-31 18:36:28 -050051 case cr <- 0: // ERROR "send"
52 case x := <-cr: // ok
Rob Pike4f61fc92010-09-04 10:36:13 +100053 _ = x
Russ Coxd33d7632009-05-22 09:53:25 -070054
Russ Coxf4e76d82011-01-31 18:36:28 -050055 case cs <- 0: // ok
56 case x := <-cs: // ERROR "receive"
Rob Pike4f61fc92010-09-04 10:36:13 +100057 _ = x
Russ Coxd33d7632009-05-22 09:53:25 -070058 }
Luuk van Dijkea9e9382011-11-06 22:14:15 +010059
Josh Bleecher Snyder26a85212017-05-18 14:53:12 -070060 for _ = range cs { // ERROR "receive"
Luuk van Dijkea9e9382011-11-06 22:14:15 +010061 }
62
Josh Bleecher Snyder26a85212017-05-18 14:53:12 -070063 for range cs { // ERROR "receive"
Russ Cox8d504c42014-07-16 19:27:10 -040064 }
65
Russ Coxf58ed4e2011-10-13 16:58:04 -040066 close(c)
67 close(cs)
Josh Bleecher Snyder26a85212017-05-18 14:53:12 -070068 close(cr) // ERROR "receive"
69 close(n) // ERROR "invalid operation.*non-chan type"
Russ Coxd33d7632009-05-22 09:53:25 -070070}