Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 1 | // $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 | |
| 7 | package main |
| 8 | |
| 9 | import "unsafe" |
| 10 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 11 | func use(bool) {} |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 12 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 13 | func stringptr(s string) uintptr { return *(*uintptr)(unsafe.Pointer(&s)) } |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 14 | |
| 15 | func isfalse(b bool) { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 16 | if b { |
| 17 | // stack will explain where |
| 18 | panic("wanted false, got true") |
| 19 | } |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | func istrue(b bool) { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 23 | if !b { |
| 24 | // stack will explain where |
| 25 | panic("wanted true, got false") |
| 26 | } |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 27 | } |
| 28 | |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 29 | type T *int |
| 30 | |
Robert Griesemer | 542099d | 2009-12-09 19:27:08 -0800 | [diff] [blame] | 31 | func main() { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 32 | var a []int |
| 33 | var b map[string]int |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 34 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 35 | var c string = "hello" |
| 36 | var d string = "hel" // try to get different pointer |
| 37 | d = d + "lo" |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 38 | if stringptr(c) == stringptr(d) { |
| 39 | panic("compiler too smart -- got same string") |
| 40 | } |
| 41 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 42 | var e = make(chan int) |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 43 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 44 | 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 Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 50 | // these comparisons are okay because |
| 51 | // string compare is okay and the others |
| 52 | // are comparisons where the types differ. |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 53 | 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 Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 60 | |
| 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 Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 78 | |
Russ Cox | 9b6d385 | 2009-01-26 12:36:21 -0800 | [diff] [blame] | 79 | // 6g used to let this go through as true. |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 80 | var g uint64 = 123 |
| 81 | var h int64 = 123 |
| 82 | var ig interface{} = g |
| 83 | var ih interface{} = h |
| 84 | isfalse(ig == ih) |
Russ Cox | 9b6d385 | 2009-01-26 12:36:21 -0800 | [diff] [blame] | 85 | |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 86 | // map of interface should use == on interface values, |
| 87 | // not memory. |
| 88 | // TODO: should m[c], m[d] be valid here? |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 89 | var m = make(map[interface{}]int) |
| 90 | m[ic] = 1 |
| 91 | m[id] = 2 |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 92 | if m[ic] != 2 { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 93 | println("m[ic] = ", m[ic]) |
| 94 | panic("bad m[ic]") |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 95 | } |
Russ Cox | 23bd214 | 2010-09-13 15:42:47 -0400 | [diff] [blame] | 96 | |
| 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 Cox | 4bdf1fc | 2011-09-26 19:35:21 -0400 | [diff] [blame] | 130 | |
| 131 | shouldPanic(p1) |
| 132 | shouldPanic(p2) |
| 133 | shouldPanic(p3) |
| 134 | shouldPanic(p4) |
| 135 | } |
| 136 | |
| 137 | func p1() { |
| 138 | var a []int |
| 139 | var ia interface{} = a |
| 140 | use(ia == ia) |
| 141 | } |
| 142 | |
| 143 | func p2() { |
| 144 | var b []int |
| 145 | var ib interface{} = b |
| 146 | use(ib == ib) |
| 147 | } |
| 148 | |
| 149 | func p3() { |
| 150 | var a []int |
| 151 | var ia interface{} = a |
| 152 | var m = make(map[interface{}] int) |
| 153 | m[ia] = 1 |
| 154 | } |
| 155 | |
| 156 | func p4() { |
| 157 | var b []int |
| 158 | var ib interface{} = b |
| 159 | var m = make(map[interface{}] int) |
| 160 | m[ib] = 1 |
| 161 | } |
| 162 | |
| 163 | func shouldPanic(f func()) { |
| 164 | defer func() { |
| 165 | if recover() == nil { |
| 166 | panic("function should panic") |
| 167 | } |
| 168 | }() |
| 169 | f() |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 170 | } |