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 | |
Robert Griesemer | 542099d | 2009-12-09 19:27:08 -0800 | [diff] [blame] | 29 | func main() { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 30 | var a []int |
| 31 | var b map[string]int |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 32 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 33 | var c string = "hello" |
| 34 | var d string = "hel" // try to get different pointer |
| 35 | d = d + "lo" |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 36 | if stringptr(c) == stringptr(d) { |
| 37 | panic("compiler too smart -- got same string") |
| 38 | } |
| 39 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 40 | var e = make(chan int) |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 41 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 42 | var ia interface{} = a |
| 43 | var ib interface{} = b |
| 44 | var ic interface{} = c |
| 45 | var id interface{} = d |
| 46 | var ie interface{} = e |
| 47 | |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 48 | // these comparisons are okay because |
| 49 | // string compare is okay and the others |
| 50 | // are comparisons where the types differ. |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 51 | isfalse(ia == ib) |
| 52 | isfalse(ia == ic) |
| 53 | isfalse(ia == id) |
| 54 | isfalse(ib == ic) |
| 55 | isfalse(ib == id) |
| 56 | istrue(ic == id) |
| 57 | istrue(ie == ie) |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 58 | |
Russ Cox | 9b6d385 | 2009-01-26 12:36:21 -0800 | [diff] [blame] | 59 | // 6g used to let this go through as true. |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 60 | var g uint64 = 123 |
| 61 | var h int64 = 123 |
| 62 | var ig interface{} = g |
| 63 | var ih interface{} = h |
| 64 | isfalse(ig == ih) |
Russ Cox | 9b6d385 | 2009-01-26 12:36:21 -0800 | [diff] [blame] | 65 | |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 66 | // map of interface should use == on interface values, |
| 67 | // not memory. |
| 68 | // TODO: should m[c], m[d] be valid here? |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 69 | var m = make(map[interface{}]int) |
| 70 | m[ic] = 1 |
| 71 | m[id] = 2 |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 72 | if m[ic] != 2 { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 73 | println("m[ic] = ", m[ic]) |
| 74 | panic("bad m[ic]") |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 75 | } |
| 76 | } |