Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // errorcheck |
Russ Cox | d33d763 | 2009-05-22 09:53:25 -0700 | [diff] [blame] | 2 | |
| 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 Pike | 3fb5f32 | 2012-02-19 17:44:02 +1100 | [diff] [blame] | 7 | // Test various correct and incorrect permutations of send-only, |
| 8 | // receive-only, and bidirectional channels. |
| 9 | // Does not compile. |
| 10 | |
Russ Cox | d33d763 | 2009-05-22 09:53:25 -0700 | [diff] [blame] | 11 | package main |
| 12 | |
| 13 | var ( |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 14 | cr <-chan int |
| 15 | cs chan<- int |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 16 | c chan int |
Russ Cox | d33d763 | 2009-05-22 09:53:25 -0700 | [diff] [blame] | 17 | ) |
| 18 | |
| 19 | func main() { |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 20 | 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 Cox | d33d763 | 2009-05-22 09:53:25 -0700 | [diff] [blame] | 26 | |
Alberto Donizetti | 1737aef | 2017-04-22 15:28:58 +0200 | [diff] [blame] | 27 | var n int |
| 28 | <-n // ERROR "receive from non-chan" |
| 29 | n <- 2 // ERROR "send to non-chan" |
| 30 | |
Josh Bleecher Snyder | 26a8521 | 2017-05-18 14:53:12 -0700 | [diff] [blame] | 31 | c <- 0 // ok |
| 32 | <-c // ok |
| 33 | x, ok := <-c // ok |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 34 | _, _ = x, ok |
Russ Cox | d33d763 | 2009-05-22 09:53:25 -0700 | [diff] [blame] | 35 | |
Josh Bleecher Snyder | 26a8521 | 2017-05-18 14:53:12 -0700 | [diff] [blame] | 36 | cr <- 0 // ERROR "send" |
| 37 | <-cr // ok |
| 38 | x, ok = <-cr // ok |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 39 | _, _ = x, ok |
Russ Cox | d33d763 | 2009-05-22 09:53:25 -0700 | [diff] [blame] | 40 | |
Josh Bleecher Snyder | 26a8521 | 2017-05-18 14:53:12 -0700 | [diff] [blame] | 41 | cs <- 0 // ok |
| 42 | <-cs // ERROR "receive" |
| 43 | x, ok = <-cs // ERROR "receive" |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 44 | _, _ = x, ok |
Russ Cox | d33d763 | 2009-05-22 09:53:25 -0700 | [diff] [blame] | 45 | |
| 46 | select { |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 47 | case c <- 0: // ok |
| 48 | case x := <-c: // ok |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 49 | _ = x |
Russ Cox | d33d763 | 2009-05-22 09:53:25 -0700 | [diff] [blame] | 50 | |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 51 | case cr <- 0: // ERROR "send" |
| 52 | case x := <-cr: // ok |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 53 | _ = x |
Russ Cox | d33d763 | 2009-05-22 09:53:25 -0700 | [diff] [blame] | 54 | |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 55 | case cs <- 0: // ok |
| 56 | case x := <-cs: // ERROR "receive" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 57 | _ = x |
Russ Cox | d33d763 | 2009-05-22 09:53:25 -0700 | [diff] [blame] | 58 | } |
Luuk van Dijk | ea9e938 | 2011-11-06 22:14:15 +0100 | [diff] [blame] | 59 | |
Josh Bleecher Snyder | 26a8521 | 2017-05-18 14:53:12 -0700 | [diff] [blame] | 60 | for _ = range cs { // ERROR "receive" |
Luuk van Dijk | ea9e938 | 2011-11-06 22:14:15 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Josh Bleecher Snyder | 26a8521 | 2017-05-18 14:53:12 -0700 | [diff] [blame] | 63 | for range cs { // ERROR "receive" |
Russ Cox | 8d504c4 | 2014-07-16 19:27:10 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Russ Cox | f58ed4e | 2011-10-13 16:58:04 -0400 | [diff] [blame] | 66 | close(c) |
| 67 | close(cs) |
Josh Bleecher Snyder | 26a8521 | 2017-05-18 14:53:12 -0700 | [diff] [blame] | 68 | close(cr) // ERROR "receive" |
| 69 | close(n) // ERROR "invalid operation.*non-chan type" |
Russ Cox | d33d763 | 2009-05-22 09:53:25 -0700 | [diff] [blame] | 70 | } |