1// run
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// Test Implicit methods for embedded types and
8// mixed pointer and non-pointer receivers.
9
10package main
11
12type T int
13
14var nv, np int
15
16func (t T) V() {
17	if t != 42 {
18		panic(t)
19	}
20	nv++
21}
22
23func (t *T) P() {
24	if *t != 42 {
25		println(t, *t)
26		panic("fail")
27	}
28	np++
29}
30
31type V interface {
32	V()
33}
34type P interface {
35	P()
36	V()
37}
38
39type S struct {
40	T
41}
42
43type SP struct {
44	*T
45}
46
47func main() {
48	var t T
49	var v V
50	var p P
51
52	t = 42
53
54	t.P()
55	t.V()
56
57	v = t
58	v.V()
59
60	p = &t
61	p.P()
62	p.V()
63
64	v = &t
65	v.V()
66
67	//	p = t	// ERROR
68	var i interface{} = t
69	if _, ok := i.(P); ok {
70		println("dynamic i.(P) succeeded incorrectly")
71		panic("fail")
72	}
73
74	//	println("--struct--");
75	var s S
76	s.T = 42
77	s.P()
78	s.V()
79
80	v = s
81	s.V()
82
83	p = &s
84	p.P()
85	p.V()
86
87	v = &s
88	v.V()
89
90	//	p = s	// ERROR
91	var j interface{} = s
92	if _, ok := j.(P); ok {
93		println("dynamic j.(P) succeeded incorrectly")
94		panic("fail")
95	}
96
97	//	println("--struct pointer--");
98	var sp SP
99	sp.T = &t
100	sp.P()
101	sp.V()
102
103	v = sp
104	sp.V()
105
106	p = &sp
107	p.P()
108	p.V()
109
110	v = &sp
111	v.V()
112
113	p = sp // not error
114	p.P()
115	p.V()
116
117	if nv != 13 || np != 7 {
118		println("bad count", nv, np)
119		panic("fail")
120	}
121}
122