summaryrefslogtreecommitdiff
path: root/test/cmp6.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2011-12-12 22:22:09 -0500
committerRuss Cox <rsc@golang.org>2011-12-12 22:22:09 -0500
commit0203a12ce11f1358c9ce0b75c87e0d5f23980b30 (patch)
tree8896aafad99b6a73ad415620366c9de0b2200997 /test/cmp6.go
parentfa93a5f699b13659baae22b0ae24fd6ae11bd41d (diff)
downloadgo-0203a12ce11f1358c9ce0b75c87e0d5f23980b30.tar.gz
gc: implement == on structs and arrays
To allow these types as map keys, we must fill in equal and hash functions in their algorithm tables. Structs or arrays that are "just memory", like [2]int, can and do continue to use the AMEM algorithm. Structs or arrays that contain special values like strings or interface values use generated functions for both equal and hash. The runtime helper func runtime.equal(t, x, y) bool handles the general equality case for x == y and calls out to the equal implementation in the algorithm table. For short values (<= 4 struct fields or array elements), the sequence of elementwise comparisons is inlined instead of calling runtime.equal. R=ken, mpimenov CC=golang-dev http://codereview.appspot.com/5451105
Diffstat (limited to 'test/cmp6.go')
-rw-r--r--test/cmp6.go12
1 files changed, 11 insertions, 1 deletions
diff --git a/test/cmp6.go b/test/cmp6.go
index 6b13cac23..0113a69dd 100644
--- a/test/cmp6.go
+++ b/test/cmp6.go
@@ -11,7 +11,7 @@ func use(bool) {}
type T1 *int
type T2 *int
-type T3 struct{}
+type T3 struct{ z []int }
var t3 T3
@@ -54,4 +54,14 @@ func main() {
use(x == x) // ERROR "slice can only be compared to nil"
use(f == f) // ERROR "func can only be compared to nil"
use(m == m) // ERROR "map can only be compared to nil"
+
+ // Comparison with interface that cannot return true
+ // (would panic).
+ var i interface{}
+ use(i == x) // ERROR "invalid operation"
+ use(x == i) // ERROR "invalid operation"
+ use(i == f) // ERROR "invalid operation"
+ use(f == i) // ERROR "invalid operation"
+ use(i == m) // ERROR "invalid operation"
+ use(m == i) // ERROR "invalid operation"
}