1// Copyright 2016 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
5// type aliases
6
7package decls4
8
9type (
10	T0 [10]int
11	T1 []byte
12	T2 struct {
13		x int
14	}
15	T3 interface{
16		m() T2
17	}
18	T4 func(int, T0) chan T2
19)
20
21type (
22	Ai = int
23	A0 = T0
24	A1 = T1
25	A2 = T2
26	A3 = T3
27	A4 = T4
28
29	A10 = [10]int
30	A11 = []byte
31	A12 = struct {
32		x int
33	}
34	A13 = interface{
35		m() A2
36	}
37	A14 = func(int, A0) chan A2
38)
39
40// check assignment compatibility due to equality of types
41var (
42	xi_ int
43	ai Ai = xi_
44
45	x0 T0
46	a0 A0 = x0
47
48	x1 T1
49	a1 A1 = x1
50
51	x2 T2
52	a2 A2 = x2
53
54	x3 T3
55	a3 A3 = x3
56
57	x4 T4
58	a4 A4 = x4
59)
60
61// alias receiver types
62func (Ai /* ERROR "invalid receiver" */) m1() {}
63func (T0) m1() {}
64func (A0) m1 /* ERROR already declared */ () {}
65func (A0) m2 () {}
66func (A3 /* ERROR invalid receiver */ ) m1 () {}
67func (A10 /* ERROR invalid receiver */ ) m1() {}
68
69// x0 has methods m1, m2 declared via receiver type names T0 and A0
70var _ interface{ m1(); m2() } = x0
71
72// alias receiver types (test case for issue #23042)
73type T struct{}
74
75var (
76	_ = T.m
77	_ = T{}.m
78	_ interface{m()} = T{}
79)
80
81var (
82	_ = T.n
83	_ = T{}.n
84	_ interface{m(); n()} = T{}
85)
86
87type U = T
88func (U) m() {}
89
90// alias receiver types (long type declaration chains)
91type (
92	V0 = V1
93	V1 = (V2)
94	V2 = ((V3))
95	V3 = T
96)
97
98func (V0) m /* ERROR already declared */ () {}
99func (V1) n() {}
100
101// alias receiver types (invalid due to cycles)
102type (
103	W0 /* ERROR illegal cycle */ = W1
104	W1 = (W2)
105	W2 = ((W0))
106)
107
108func (W0) m() {} // no error expected (due to above cycle error)
109func (W1) n() {}
110
111// alias receiver types (invalid due to builtin underlying type)
112type (
113	B0 = B1
114	B1 = B2
115	B2 = int
116)
117
118func (B0 /* ERROR invalid receiver */ ) m() {}
119func (B1 /* ERROR invalid receiver */ ) n() {}
120
121// cycles
122type (
123	C2 /* ERROR illegal cycle */ = C2
124	C3 /* ERROR illegal cycle */ = C4
125	C4 = C3
126	C5 struct {
127		f *C6
128	}
129	C6 = C5
130	C7 /* ERROR illegal cycle */  struct {
131		f C8
132	}
133	C8 = C7
134)
135
136// embedded fields
137var (
138	s0 struct { T0 }
139	s1 struct { A0 } = s0 /* ERROR cannot use */ // embedded field names are different
140)
141
142// embedding and lookup of fields and methods
143func _(s struct{A0}) { s.A0 = x0 }
144
145type eX struct{xf int}
146
147func (eX) xm()
148
149type eY = struct{eX} // field/method set of eY includes xf, xm
150
151type eZ = *struct{eX} // field/method set of eZ includes xf, xm
152
153type eA struct {
154	eX // eX contributes xf, xm to eA
155}
156
157type eA2 struct {
158	*eX // *eX contributes xf, xm to eA
159}
160
161type eB struct {
162	eY // eY contributes xf, xm to eB
163}
164
165type eB2 struct {
166	*eY // *eY contributes xf, xm to eB
167}
168
169type eC struct {
170	eZ // eZ contributes xf, xm to eC
171}
172
173var (
174	_ = eA{}.xf
175	_ = eA{}.xm
176	_ = eA2{}.xf
177	_ = eA2{}.xm
178	_ = eB{}.xf
179	_ = eB{}.xm
180	_ = eB2{}.xf
181	_ = eB2{}.xm
182	_ = eC{}.xf
183	_ = eC{}.xm
184)
185
186// ambiguous selectors due to embedding via type aliases
187type eD struct {
188	eY
189	eZ
190}
191
192var (
193	_ = eD{}.xf /* ERROR ambiguous selector */
194	_ = eD{}.xm /* ERROR ambiguous selector */
195)
196
197var (
198	_ interface{ xm() } = eD /* ERROR missing method xm */ {}
199)