Source file test/chan/perm.go

     1  // errorcheck
     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  
     7  // Test various correct and incorrect permutations of send-only,
     8  // receive-only, and bidirectional channels.
     9  // Does not compile.
    10  
    11  package main
    12  
    13  var (
    14  	cr <-chan int
    15  	cs chan<- int
    16  	c  chan int
    17  )
    18  
    19  func main() {
    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"
    26  
    27  	var n int
    28  	<-n    // ERROR "receive from non-chan|expected channel"
    29  	n <- 2 // ERROR "send to non-chan|must be channel"
    30  
    31  	c <- 0       // ok
    32  	<-c          // ok
    33  	x, ok := <-c // ok
    34  	_, _ = x, ok
    35  
    36  	cr <- 0      // ERROR "send"
    37  	<-cr         // ok
    38  	x, ok = <-cr // ok
    39  	_, _ = x, ok
    40  
    41  	cs <- 0      // ok
    42  	<-cs         // ERROR "receive"
    43  	x, ok = <-cs // ERROR "receive"
    44  	_, _ = x, ok
    45  
    46  	select {
    47  	case c <- 0: // ok
    48  	case x := <-c: // ok
    49  		_ = x
    50  
    51  	case cr <- 0: // ERROR "send"
    52  	case x := <-cr: // ok
    53  		_ = x
    54  
    55  	case cs <- 0: // ok
    56  	case x := <-cs: // ERROR "receive"
    57  		_ = x
    58  	}
    59  
    60  	for _ = range cs { // ERROR "receive"
    61  	}
    62  
    63  	for range cs { // ERROR "receive"
    64  	}
    65  
    66  	close(c)
    67  	close(cs)
    68  	close(cr) // ERROR "receive"
    69  	close(n)  // ERROR "invalid operation.*non-chan type|must be channel|non-channel"
    70  }
    71  

View as plain text