blob: 7cf76044ef9b8ebc97baefeb00ccfa31a14fb9ef [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// errorcheck
Russ Cox23bd2142010-09-13 15:42:47 -04002
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 Pikefc0dc042012-02-19 13:19:43 +11007// Verify that incorrect comparisons are detected.
8// Does not compile.
9
Russ Cox23bd2142010-09-13 15:42:47 -040010package main
11
12func use(bool) {}
13
14type T1 *int
15type T2 *int
16
Russ Cox196b6632011-12-12 22:22:09 -050017type T3 struct{ z []int }
Russ Cox7e846662011-01-21 18:15:59 -050018
19var t3 T3
20
Rémy Oudompheng502958ff2014-01-31 00:30:56 +010021type T4 struct {
22 _ []int
23 a float64
24}
Russ Coxc4c92eb2012-02-17 14:45:29 -050025
26var t4 T4
27
Russ Cox23bd2142010-09-13 15:42:47 -040028func 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 Cox5bb54b82011-11-13 22:58:08 -050034 var c1 chan<- int
Russ Cox23bd2142010-09-13 15:42:47 -040035 var c2 <-chan int
36 var c3 chan int
Russ Cox5bb54b82011-11-13 22:58:08 -050037
38 use(c1 == c2) // ERROR "invalid operation|incompatible"
39 use(c2 == c1) // ERROR "invalid operation|incompatible"
Russ Cox23bd2142010-09-13 15:42:47 -040040 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 Cox5bb54b82011-11-13 22:58:08 -050049
50 use(p1 == p2) // ERROR "invalid operation|incompatible"
51 use(p2 == p1) // ERROR "invalid operation|incompatible"
Russ Cox23bd2142010-09-13 15:42:47 -040052 use(p1 == p3)
53 use(p2 == p2)
54 use(p3 == p1)
55 use(p3 == p2)
Russ Cox5bb54b82011-11-13 22:58:08 -050056
Rémy Oudompheng502958ff2014-01-31 00:30:56 +010057 // 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 Cox7e846662011-01-21 18:15:59 -050065 // Comparison of structs should have a good message
Russ Cox5bb54b82011-11-13 22:58:08 -050066 use(t3 == t3) // ERROR "struct|expected"
Rémy Oudompheng428ea682013-07-02 09:08:43 +020067 use(t4 == t4) // ERROR "cannot be compared|non-comparable"
Russ Cox5bb54b82011-11-13 22:58:08 -050068
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 Cox196b6632011-12-12 22:22:09 -050076
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 Cox23bd2142010-09-13 15:42:47 -040086}