2012-02-16 21:48:57 -07:00
|
|
|
// errorcheck
|
2009-05-22 10:53:25 -06:00
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-18 23:44:02 -07:00
|
|
|
// Test various correct and incorrect permutations of send-only,
|
|
|
|
// receive-only, and bidirectional channels.
|
|
|
|
// Does not compile.
|
|
|
|
|
2009-05-22 10:53:25 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
var (
|
2010-09-03 18:36:13 -06:00
|
|
|
cr <-chan int
|
|
|
|
cs chan<- int
|
2011-01-31 16:36:28 -07:00
|
|
|
c chan int
|
2009-05-22 10:53:25 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2011-01-31 16:36:28 -07:00
|
|
|
cr = c // ok
|
|
|
|
cs = c // ok
|
|
|
|
c = cr // ERROR "illegal types|incompatible|cannot"
|
|
|
|
c = cs // ERROR "illegal types|incompatible|cannot"
|
|
|
|
cr = cs // ERROR "illegal types|incompatible|cannot"
|
|
|
|
cs = cr // ERROR "illegal types|incompatible|cannot"
|
|
|
|
|
2017-04-22 07:28:58 -06:00
|
|
|
var n int
|
test: match gofrontend error messages
These changes match the following gofrontend error messages:
blank1.go:16:1: error: may not define methods on non-local type
chan/perm.go:28:9: error: expected channel
chan/perm.go:29:11: error: left operand of ‘<-’ must be channel
chan/perm.go:69:9: error: argument must be channel
complit1.go:25:16: error: attempt to slice object that is not array, slice, or string
complit1.go:26:16: error: attempt to slice object that is not array, slice, or string
complit1.go:27:17: error: attempt to slice object that is not array, slice, or string
complit1.go:49:41: error: may only omit types within composite literals of slice, array, or map type
complit1.go:50:14: error: expected struct, slice, array, or map type for composite literal
convlit.go:24:9: error: invalid type conversion (cannot use type unsafe.Pointer as type string)
convlit.go:25:9: error: invalid type conversion (cannot use type unsafe.Pointer as type float64)
convlit.go:26:9: error: invalid type conversion (cannot use type unsafe.Pointer as type int)
ddd1.go:63:9: error: invalid use of ‘...’ calling non-variadic function
fixedbugs/bug176.go:12:18: error: index expression is not integer constant
fixedbugs/bug332.go:17:10: error: use of undefined type ‘T’
fixedbugs/issue4232.go:22:16: error: integer constant overflow
fixedbugs/issue4232.go:33:16: error: integer constant overflow
fixedbugs/issue4232.go:44:25: error: integer constant overflow
fixedbugs/issue4232.go:55:16: error: integer constant overflow
fixedbugs/issue4458.go:19:14: error: type has no method ‘foo’
fixedbugs/issue5172.go:24:14: error: too many expressions for struct
init.go:17:9: error: reference to undefined name ‘runtime’
initializerr.go:26:29: error: duplicate value for index 1
interface/explicit.go:60:14: error: type assertion only valid for interface types
label.go:64:9: error: reference to undefined label ‘go2’
label1.go:18:97: error: continue statement not within for
label1.go:22:97: error: continue statement not within for
label1.go:106:89: error: continue statement not within for
label1.go:108:26: error: invalid continue label ‘on’
label1.go:111:118: error: break statement not within for or switch or select
label1.go:113:23: error: invalid break label ‘dance’
map1.go:64:9: error: not enough arguments
map1.go:65:9: error: not enough arguments
map1.go:67:9: error: argument 1 must be a map
method2.go:36:11: error: reference to undefined field or method ‘val’
method2.go:37:11: error: reference to undefined field or method ‘val’
method2.go:41:12: error: method requires pointer (use ‘(*T).g’)
syntax/chan1.go:13:19: error: send statement used as value; use select for non-blocking send
syntax/chan1.go:17:11: error: send statement used as value; use select for non-blocking send
Change-Id: I98047b60a376e3d2788836300f7fcac3f2c285cb
Reviewed-on: https://go-review.googlesource.com/c/go/+/273527
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
2020-11-26 13:26:02 -07:00
|
|
|
<-n // ERROR "receive from non-chan|expected channel"
|
|
|
|
n <- 2 // ERROR "send to non-chan|must be channel"
|
2017-04-22 07:28:58 -06:00
|
|
|
|
2017-05-18 15:53:12 -06:00
|
|
|
c <- 0 // ok
|
|
|
|
<-c // ok
|
|
|
|
x, ok := <-c // ok
|
2011-03-11 12:47:44 -07:00
|
|
|
_, _ = x, ok
|
2011-01-31 16:36:28 -07:00
|
|
|
|
2017-05-18 15:53:12 -06:00
|
|
|
cr <- 0 // ERROR "send"
|
|
|
|
<-cr // ok
|
|
|
|
x, ok = <-cr // ok
|
2011-03-11 12:47:44 -07:00
|
|
|
_, _ = x, ok
|
2011-01-31 16:36:28 -07:00
|
|
|
|
2017-05-18 15:53:12 -06:00
|
|
|
cs <- 0 // ok
|
|
|
|
<-cs // ERROR "receive"
|
|
|
|
x, ok = <-cs // ERROR "receive"
|
2011-03-11 12:47:44 -07:00
|
|
|
_, _ = x, ok
|
2009-05-22 10:53:25 -06:00
|
|
|
|
|
|
|
select {
|
2011-01-31 16:36:28 -07:00
|
|
|
case c <- 0: // ok
|
|
|
|
case x := <-c: // ok
|
2010-09-03 18:36:13 -06:00
|
|
|
_ = x
|
2009-05-22 10:53:25 -06:00
|
|
|
|
2011-01-31 16:36:28 -07:00
|
|
|
case cr <- 0: // ERROR "send"
|
|
|
|
case x := <-cr: // ok
|
2010-09-03 18:36:13 -06:00
|
|
|
_ = x
|
2009-05-22 10:53:25 -06:00
|
|
|
|
2011-01-31 16:36:28 -07:00
|
|
|
case cs <- 0: // ok
|
|
|
|
case x := <-cs: // ERROR "receive"
|
2010-09-03 18:36:13 -06:00
|
|
|
_ = x
|
2009-05-22 10:53:25 -06:00
|
|
|
}
|
2011-11-06 14:14:15 -07:00
|
|
|
|
2017-05-18 15:53:12 -06:00
|
|
|
for _ = range cs { // ERROR "receive"
|
2011-11-06 14:14:15 -07:00
|
|
|
}
|
|
|
|
|
2017-05-18 15:53:12 -06:00
|
|
|
for range cs { // ERROR "receive"
|
2014-07-16 17:27:10 -06:00
|
|
|
}
|
|
|
|
|
2011-10-13 14:58:04 -06:00
|
|
|
close(c)
|
|
|
|
close(cs)
|
2017-05-18 15:53:12 -06:00
|
|
|
close(cr) // ERROR "receive"
|
2021-07-09 19:26:57 -06:00
|
|
|
close(n) // ERROR "invalid operation.*non-chan type|must be channel|non-channel"
|
2009-05-22 10:53:25 -06:00
|
|
|
}
|