blob: db0a486dd339f6f6df3688a02a725bade8d5f77b [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
Robert Griesemer542099d2009-12-09 19:27:08 -080029func main() {
Rob Pike325cf8e2010-03-24 16:46:53 -070030 var a []int
31 var b map[string]int
Russ Coxa7f6d402009-01-26 09:56:42 -080032
Rob Pike325cf8e2010-03-24 16:46:53 -070033 var c string = "hello"
34 var d string = "hel" // try to get different pointer
35 d = d + "lo"
Russ Coxa7f6d402009-01-26 09:56:42 -080036 if stringptr(c) == stringptr(d) {
37 panic("compiler too smart -- got same string")
38 }
39
Rob Pike325cf8e2010-03-24 16:46:53 -070040 var e = make(chan int)
Russ Coxa7f6d402009-01-26 09:56:42 -080041
Rob Pike325cf8e2010-03-24 16:46:53 -070042 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 Coxa7f6d402009-01-26 09:56:42 -080048 // these comparisons are okay because
49 // string compare is okay and the others
50 // are comparisons where the types differ.
Rob Pike325cf8e2010-03-24 16:46:53 -070051 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 Coxa7f6d402009-01-26 09:56:42 -080058
Russ Cox9b6d3852009-01-26 12:36:21 -080059 // 6g used to let this go through as true.
Rob Pike325cf8e2010-03-24 16:46:53 -070060 var g uint64 = 123
61 var h int64 = 123
62 var ig interface{} = g
63 var ih interface{} = h
64 isfalse(ig == ih)
Russ Cox9b6d3852009-01-26 12:36:21 -080065
Russ Coxa7f6d402009-01-26 09:56:42 -080066 // map of interface should use == on interface values,
67 // not memory.
68 // TODO: should m[c], m[d] be valid here?
Rob Pike325cf8e2010-03-24 16:46:53 -070069 var m = make(map[interface{}]int)
70 m[ic] = 1
71 m[id] = 2
Russ Coxa7f6d402009-01-26 09:56:42 -080072 if m[ic] != 2 {
Russ Cox00f9f0c2010-03-30 10:34:57 -070073 println("m[ic] = ", m[ic])
74 panic("bad m[ic]")
Russ Coxa7f6d402009-01-26 09:56:42 -080075 }
76}