blob: 570487db6af707f7d5a0f92e162fe5831651542b [file] [log] [blame]
Russ Coxa7f6d402009-01-26 09:56:42 -08001// $G $D/$F.go && $L $F.$A && ./$A.out
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
7package main
8
9import "unsafe"
10
Rob Pike325cf8e2010-03-24 16:46:53 -070011func use(bool) {}
Russ Coxa7f6d402009-01-26 09:56:42 -080012
Rob Pike325cf8e2010-03-24 16:46:53 -070013func stringptr(s string) uintptr { return *(*uintptr)(unsafe.Pointer(&s)) }
Russ Coxa7f6d402009-01-26 09:56:42 -080014
15func isfalse(b bool) {
Rob Pike325cf8e2010-03-24 16:46:53 -070016 if b {
17 // stack will explain where
18 panic("wanted false, got true")
19 }
Russ Coxa7f6d402009-01-26 09:56:42 -080020}
21
22func istrue(b bool) {
Rob Pike325cf8e2010-03-24 16:46:53 -070023 if !b {
24 // stack will explain where
25 panic("wanted true, got false")
26 }
Russ Coxa7f6d402009-01-26 09:56:42 -080027}
28
Russ Cox23bd2142010-09-13 15:42:47 -040029type T *int
30
Robert Griesemer542099d2009-12-09 19:27:08 -080031func main() {
Rob Pike325cf8e2010-03-24 16:46:53 -070032 var a []int
33 var b map[string]int
Russ Coxa7f6d402009-01-26 09:56:42 -080034
Rob Pike325cf8e2010-03-24 16:46:53 -070035 var c string = "hello"
36 var d string = "hel" // try to get different pointer
37 d = d + "lo"
Russ Coxa7f6d402009-01-26 09:56:42 -080038 if stringptr(c) == stringptr(d) {
39 panic("compiler too smart -- got same string")
40 }
41
Rob Pike325cf8e2010-03-24 16:46:53 -070042 var e = make(chan int)
Russ Coxa7f6d402009-01-26 09:56:42 -080043
Rob Pike325cf8e2010-03-24 16:46:53 -070044 var ia interface{} = a
45 var ib interface{} = b
46 var ic interface{} = c
47 var id interface{} = d
48 var ie interface{} = e
49
Russ Coxa7f6d402009-01-26 09:56:42 -080050 // these comparisons are okay because
51 // string compare is okay and the others
52 // are comparisons where the types differ.
Rob Pike325cf8e2010-03-24 16:46:53 -070053 isfalse(ia == ib)
54 isfalse(ia == ic)
55 isfalse(ia == id)
56 isfalse(ib == ic)
57 isfalse(ib == id)
58 istrue(ic == id)
59 istrue(ie == ie)
Russ Cox23bd2142010-09-13 15:42:47 -040060
61 // these are okay because one side of the
62 // comparison need only be assignable to the other.
63 isfalse(a == ib)
64 isfalse(a == ic)
65 isfalse(a == id)
66 isfalse(b == ic)
67 isfalse(b == id)
68 istrue(c == id)
69 istrue(e == ie)
70
71 isfalse(ia == b)
72 isfalse(ia == c)
73 isfalse(ia == d)
74 isfalse(ib == c)
75 isfalse(ib == d)
76 istrue(ic == d)
77 istrue(ie == e)
Russ Coxa7f6d402009-01-26 09:56:42 -080078
Russ Cox9b6d3852009-01-26 12:36:21 -080079 // 6g used to let this go through as true.
Rob Pike325cf8e2010-03-24 16:46:53 -070080 var g uint64 = 123
81 var h int64 = 123
82 var ig interface{} = g
83 var ih interface{} = h
84 isfalse(ig == ih)
Russ Cox9b6d3852009-01-26 12:36:21 -080085
Russ Coxa7f6d402009-01-26 09:56:42 -080086 // map of interface should use == on interface values,
87 // not memory.
88 // TODO: should m[c], m[d] be valid here?
Rob Pike325cf8e2010-03-24 16:46:53 -070089 var m = make(map[interface{}]int)
90 m[ic] = 1
91 m[id] = 2
Russ Coxa7f6d402009-01-26 09:56:42 -080092 if m[ic] != 2 {
Russ Cox00f9f0c2010-03-30 10:34:57 -070093 println("m[ic] = ", m[ic])
94 panic("bad m[ic]")
Russ Coxa7f6d402009-01-26 09:56:42 -080095 }
Russ Cox23bd2142010-09-13 15:42:47 -040096
97 // non-interface comparisons
98 {
99 c := make(chan int)
100 c1 := (<-chan int)(c)
101 c2 := (chan<- int)(c)
102 istrue(c == c1)
103 istrue(c == c2)
104 istrue(c1 == c)
105 istrue(c2 == c)
106
107 d := make(chan int)
108 isfalse(c == d)
109 isfalse(d == c)
110 isfalse(d == c1)
111 isfalse(d == c2)
112 isfalse(c1 == d)
113 isfalse(c2 == d)
114 }
115
116 // named types vs not
117 {
118 var x = new(int)
119 var y T
120 var z T = x
121
122 isfalse(x == y)
123 istrue(x == z)
124 isfalse(y == z)
125
126 isfalse(y == x)
127 istrue(z == x)
128 isfalse(z == y)
129 }
Russ Cox4bdf1fc2011-09-26 19:35:21 -0400130
131 shouldPanic(p1)
132 shouldPanic(p2)
133 shouldPanic(p3)
134 shouldPanic(p4)
135}
136
137func p1() {
138 var a []int
139 var ia interface{} = a
140 use(ia == ia)
141}
142
143func p2() {
144 var b []int
145 var ib interface{} = b
146 use(ib == ib)
147}
148
149func p3() {
150 var a []int
151 var ia interface{} = a
152 var m = make(map[interface{}] int)
153 m[ia] = 1
154}
155
156func p4() {
157 var b []int
158 var ib interface{} = b
159 var m = make(map[interface{}] int)
160 m[ib] = 1
161}
162
163func shouldPanic(f func()) {
164 defer func() {
165 if recover() == nil {
166 panic("function should panic")
167 }
168 }()
169 f()
Russ Coxa7f6d402009-01-26 09:56:42 -0800170}