Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // errorcheck |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 2 | |
| 3 | // Copyright 2010 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 | fc0dc04 | 2012-02-19 13:19:43 +1100 | [diff] [blame] | 7 | // Verify that incorrect comparisons are detected. |
| 8 | // Does not compile. |
| 9 | |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 10 | package main |
| 11 | |
| 12 | func use(bool) {} |
| 13 | |
| 14 | type T1 *int |
| 15 | type T2 *int |
| 16 | |
Russ Cox | 196b663 | 2011-12-12 22:22:09 -0500 | [diff] [blame] | 17 | type T3 struct{ z []int } |
Russ Cox | 7e84666 | 2011-01-21 18:15:59 -0500 | [diff] [blame] | 18 | |
| 19 | var t3 T3 |
| 20 | |
Rémy Oudompheng | 502958ff | 2014-01-31 00:30:56 +0100 | [diff] [blame] | 21 | type T4 struct { |
| 22 | _ []int |
| 23 | a float64 |
| 24 | } |
Russ Cox | c4c92eb | 2012-02-17 14:45:29 -0500 | [diff] [blame] | 25 | |
| 26 | var t4 T4 |
| 27 | |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 28 | func main() { |
| 29 | // Arguments to comparison must be |
| 30 | // assignable one to the other (or vice versa) |
| 31 | // so chan int can be compared against |
| 32 | // directional channels but channel of different |
| 33 | // direction cannot be compared against each other. |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 34 | var c1 chan<- int |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 35 | var c2 <-chan int |
| 36 | var c3 chan int |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 37 | |
| 38 | use(c1 == c2) // ERROR "invalid operation|incompatible" |
| 39 | use(c2 == c1) // ERROR "invalid operation|incompatible" |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 40 | use(c1 == c3) |
| 41 | use(c2 == c2) |
| 42 | use(c3 == c1) |
| 43 | use(c3 == c2) |
| 44 | |
| 45 | // Same applies to named types. |
| 46 | var p1 T1 |
| 47 | var p2 T2 |
| 48 | var p3 *int |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 49 | |
| 50 | use(p1 == p2) // ERROR "invalid operation|incompatible" |
| 51 | use(p2 == p1) // ERROR "invalid operation|incompatible" |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 52 | use(p1 == p3) |
| 53 | use(p2 == p2) |
| 54 | use(p3 == p1) |
| 55 | use(p3 == p2) |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 56 | |
Rémy Oudompheng | 502958ff | 2014-01-31 00:30:56 +0100 | [diff] [blame] | 57 | // Arrays are comparable if and only if their element type is comparable. |
| 58 | var a1 [1]int |
| 59 | var a2 [1]func() |
| 60 | var a3 [0]func() |
| 61 | use(a1 == a1) |
| 62 | use(a2 == a2) // ERROR "invalid operation|invalid comparison" |
| 63 | use(a3 == a3) // ERROR "invalid operation|invalid comparison" |
| 64 | |
Russ Cox | 7e84666 | 2011-01-21 18:15:59 -0500 | [diff] [blame] | 65 | // Comparison of structs should have a good message |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 66 | use(t3 == t3) // ERROR "struct|expected" |
Rémy Oudompheng | 428ea68 | 2013-07-02 09:08:43 +0200 | [diff] [blame] | 67 | use(t4 == t4) // ERROR "cannot be compared|non-comparable" |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 68 | |
| 69 | // Slices, functions, and maps too. |
| 70 | var x []int |
| 71 | var f func() |
| 72 | var m map[int]int |
| 73 | use(x == x) // ERROR "slice can only be compared to nil" |
| 74 | use(f == f) // ERROR "func can only be compared to nil" |
| 75 | use(m == m) // ERROR "map can only be compared to nil" |
Russ Cox | 196b663 | 2011-12-12 22:22:09 -0500 | [diff] [blame] | 76 | |
| 77 | // Comparison with interface that cannot return true |
| 78 | // (would panic). |
| 79 | var i interface{} |
| 80 | use(i == x) // ERROR "invalid operation" |
| 81 | use(x == i) // ERROR "invalid operation" |
| 82 | use(i == f) // ERROR "invalid operation" |
| 83 | use(f == i) // ERROR "invalid operation" |
| 84 | use(i == m) // ERROR "invalid operation" |
| 85 | use(m == i) // ERROR "invalid operation" |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 86 | } |