1 // Copyright 2011 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 declarations
6
7 package go1_17 // don't permit non-interface elements in interfaces
8
9 import "unsafe"
10
11 const pi = 3.1415
12
13 type (
14 N undeclared /* ERROR "undeclared" */
15 B bool
16 I int32
17 A [10]P
18 T struct {
19 x, y P
20 }
21 P *T
22 R (*R)
23 F func(A) I
24 Y interface {
25 f(A) I
26 }
27 S [](((P)))
28 M map[I]F
29 C chan<- I
30
31 // blank types must be typechecked
32 _ pi /* ERROR "not a type" */
33 _ struct{}
34 _ struct{ pi /* ERROR "not a type" */ }
35 )
36
37
38 // declarations of init
39 const _, init /* ERROR "cannot declare init" */ , _ = 0, 1, 2
40 type init /* ERROR "cannot declare init" */ struct{}
41 var _, init /* ERROR "cannot declare init" */ int
42
43 func init() {}
44 func init /* ERROR "missing function body" */ ()
45
46 func _() { const init = 0 }
47 func _() { type init int }
48 func _() { var init int; _ = init }
49
50 // invalid array types
51 type (
52 iA0 [... /* ERROR "invalid use of '...'" */ ]byte
53 // The error message below could be better. At the moment
54 // we believe an integer that is too large is not an integer.
55 // But at least we get an error.
56 iA1 [1 /* ERROR "must be integer" */ <<100]int
57 iA2 [- /* ERROR "invalid array length" */ 1]complex128
58 iA3 ["foo" /* ERROR "must be integer" */ ]string
59 iA4 [float64 /* ERROR "must be integer" */ (0)]int
60 )
61
62
63 type (
64 p1 pi.foo /* ERROR "no field or method foo" */
65 p2 unsafe.Pointer
66 )
67
68
69 type (
70 Pi pi /* ERROR "not a type" */
71
72 a /* ERROR "illegal cycle" */ a
73 a /* ERROR "redeclared" */ int
74
75 b /* ERROR "illegal cycle" */ c
76 c d
77 d e
78 e b
79
80 t *t
81
82 U V
83 V *W
84 W U
85
86 P1 *S2
87 P2 P1
88
89 S0 struct {
90 }
91 S1 struct {
92 a, b, c int
93 u, v, a /* ERROR "redeclared" */ float32
94 }
95 S2 struct {
96 S0 // embedded field
97 S0 /* ERROR "redeclared" */ int
98 }
99 S3 struct {
100 x S2
101 }
102 S4/* ERROR "illegal cycle" */ struct {
103 S4
104 }
105 S5 /* ERROR "illegal cycle" */ struct {
106 S6
107 }
108 S6 struct {
109 field S7
110 }
111 S7 struct {
112 S5
113 }
114
115 L1 []L1
116 L2 []int
117
118 A1 [10.0]int
119 A2 /* ERROR "illegal cycle" */ [10]A2
120 A3 /* ERROR "illegal cycle" */ [10]struct {
121 x A4
122 }
123 A4 [10]A3
124
125 F1 func()
126 F2 func(x, y, z float32)
127 F3 func(x, y, x /* ERROR "redeclared" */ float32)
128 F4 func() (x, y, x /* ERROR "redeclared" */ float32)
129 F5 func(x int) (x /* ERROR "redeclared" */ float32)
130 F6 func(x ...int)
131
132 I1 interface{}
133 I2 interface {
134 m1()
135 }
136 I3 interface {
137 m1()
138 m1 /* ERROR "duplicate method" */ ()
139 }
140 I4 interface {
141 m1(x, y, x /* ERROR "redeclared" */ float32)
142 m2() (x, y, x /* ERROR "redeclared" */ float32)
143 m3(x int) (x /* ERROR "redeclared" */ float32)
144 }
145 I5 interface {
146 m1(I5)
147 }
148 I6 interface {
149 S0 /* ERROR "non-interface type S0" */
150 }
151 I7 interface {
152 I1
153 I1
154 }
155 I8 /* ERROR "illegal cycle" */ interface {
156 I8
157 }
158 I9 /* ERROR "illegal cycle" */ interface {
159 I10
160 }
161 I10 interface {
162 I11
163 }
164 I11 interface {
165 I9
166 }
167
168 C1 chan int
169 C2 <-chan int
170 C3 chan<- C3
171 C4 chan C5
172 C5 chan C6
173 C6 chan C4
174
175 M1 map[Last]string
176 M2 map[string]M2
177
178 Last int
179 )
180
181 // cycles in function/method declarations
182 // (test cases for issues #5217, #25790 and variants)
183 func f1(x f1 /* ERROR "not a type" */ ) {}
184 func f2(x *f2 /* ERROR "not a type" */ ) {}
185 func f3() (x f3 /* ERROR "not a type" */ ) { return }
186 func f4() (x *f4 /* ERROR "not a type" */ ) { return }
187 // TODO(#43215) this should be detected as a cycle error
188 func f5([unsafe.Sizeof(f5)]int) {}
189
190 func (S0) m1 (x S0 /* ERROR illegal cycle in method declaration */ .m1) {}
191 func (S0) m2 (x *S0 /* ERROR illegal cycle in method declaration */ .m2) {}
192 func (S0) m3 () (x S0 /* ERROR illegal cycle in method declaration */ .m3) { return }
193 func (S0) m4 () (x *S0 /* ERROR illegal cycle in method declaration */ .m4) { return }
194
195 // interfaces may not have any blank methods
196 type BlankI interface {
197 _ /* ERROR "invalid method name" */ ()
198 _ /* ERROR "invalid method name" */ (float32) int
199 m()
200 }
201
202 // non-interface types may have multiple blank methods
203 type BlankT struct{}
204
205 func (BlankT) _() {}
206 func (BlankT) _(int) {}
207 func (BlankT) _() int { return 0 }
208 func (BlankT) _(int) int { return 0}
209
View as plain text