1// Copyright 2010 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file
4
5package main
6
7import (
8	p0 "./bug0"
9	p1 "./bug1"
10
11	"reflect"
12	"strings"
13)
14
15var v0 p0.T
16var v1 p1.T
17
18type I0 interface {
19	M(p0.T)
20}
21
22type I1 interface {
23	M(p1.T)
24}
25
26type t0 int
27
28func (t0) M(p0.T) {}
29
30type t1 float64
31
32func (t1) M(p1.T) {}
33
34var i0 I0 = t0(0) // ok
35var i1 I1 = t1(0) // ok
36
37var p0i p0.I = t0(0) // ok
38var p1i p1.I = t1(0) // ok
39
40func main() {
41	// check that reflect paths are correct,
42	// meaning that reflect data for v0, v1 didn't get confused.
43
44	// path is full (rooted) path name.  check suffix for gc, prefix for gccgo
45	if s := reflect.TypeOf(v0).PkgPath(); !strings.HasSuffix(s, "/bug0") && !strings.HasPrefix(s, "bug0") {
46		println("bad v0 path", len(s), s)
47		panic("fail")
48	}
49	if s := reflect.TypeOf(v1).PkgPath(); !strings.HasSuffix(s, "/bug1") && !strings.HasPrefix(s, "bug1") {
50		println("bad v1 path", s)
51		panic("fail")
52	}
53
54	// check that dynamic interface check doesn't get confused
55	var i interface{} = t0(0)
56	if _, ok := i.(I1); ok {
57		println("used t0 as i1")
58		panic("fail")
59	}
60	if _, ok := i.(p1.I); ok {
61		println("used t0 as p1.I")
62		panic("fail")
63	}
64
65	i = t1(1)
66	if _, ok := i.(I0); ok {
67		println("used t1 as i0")
68		panic("fail")
69	}
70	if _, ok := i.(p0.I); ok {
71		println("used t1 as p0.I")
72		panic("fail")
73	}
74
75	// check that type switch works.
76	// the worry is that if p0.T and p1.T have the same hash,
77	// the binary search will handle one of them incorrectly.
78	for j := 0; j < 3; j++ {
79		switch j {
80		case 0:
81			i = p0.T{}
82		case 1:
83			i = p1.T{}
84		case 2:
85			i = 3.14
86		}
87		switch i.(type) {
88		case p0.T:
89			if j != 0 {
90				println("type switch p0.T")
91				panic("fail")
92			}
93		case p1.T:
94			if j != 1 {
95				println("type switch p1.T")
96				panic("fail")
97			}
98		default:
99			if j != 2 {
100				println("type switch default", j)
101				panic("fail")
102			}
103		}
104	}
105}
106