1 // Copyright 2014 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 package go1_17 // don't permit non-interface elements in interfaces
6
7 import (
8 "fmt"
9 syn "regexp/syntax"
10 t1 "text/template"
11 t2 "html/template"
12 )
13
14 func issue7035() {
15 type T struct{ X int }
16 _ = func() {
17 fmt.Println() // must refer to imported fmt rather than the fmt below
18 }
19 fmt := new(T)
20 _ = fmt.X
21 }
22
23 func issue8066() {
24 const (
25 _ = float32(340282356779733661637539395458142568447)
26 _ = float32(340282356779733661637539395458142568448 /* ERROR cannot convert */ )
27 )
28 }
29
30 // Check that a missing identifier doesn't lead to a spurious error cascade.
31 func issue8799a() {
32 x, ok := missing /* ERROR undeclared */ ()
33 _ = !ok
34 _ = x
35 }
36
37 func issue8799b(x int, ok bool) {
38 x, ok = missing /* ERROR undeclared */ ()
39 _ = !ok
40 _ = x
41 }
42
43 func issue9182() {
44 type Point C /* ERROR undeclared */ .Point
45 // no error for composite literal based on unknown type
46 _ = Point{x: 1, y: 2}
47 }
48
49 func f0() (a []int) { return }
50 func f1() (a []int, b int) { return }
51 func f2() (a, b []int) { return }
52
53 func append_([]int, ...int) {}
54
55 func issue9473(a []int, b ...int) {
56 // variadic builtin function
57 _ = append(f0())
58 _ = append(f0(), f0()...)
59 _ = append(f1())
60 _ = append(f2 /* ERROR cannot use .* in argument */ ())
61 _ = append(f2()... /* ERROR cannot use ... */ )
62 _ = append(f0(), f1 /* ERROR 2-valued f1 */ ())
63 _ = append(f0(), f2 /* ERROR 2-valued f2 */ ())
64 _ = append(f0(), f1 /* ERROR 2-valued f1 */ ()...)
65 _ = append(f0(), f2 /* ERROR 2-valued f2 */ ()...)
66
67 // variadic user-defined function
68 append_(f0())
69 append_(f0(), f0()...)
70 append_(f1())
71 append_(f2 /* ERROR cannot use .* in argument */ ())
72 append_(f2()... /* ERROR cannot use ... */ )
73 append_(f0(), f1 /* ERROR 2-valued f1 */ ())
74 append_(f0(), f2 /* ERROR 2-valued f2 */ ())
75 append_(f0(), f1 /* ERROR 2-valued f1 */ ()...)
76 append_(f0(), f2 /* ERROR 2-valued f2 */ ()...)
77 }
78
79 // Check that embedding a non-interface type in an interface results in a good error message.
80 func issue10979() {
81 type _ interface {
82 int /* ERROR non-interface type int */
83 }
84 type T struct{}
85 type _ interface {
86 T /* ERROR non-interface type T */
87 }
88 type _ interface {
89 nosuchtype /* ERROR undeclared name: nosuchtype */
90 }
91 type _ interface {
92 fmt.Nosuchtype /* ERROR Nosuchtype not declared by package fmt */
93 }
94 type _ interface {
95 nosuchpkg /* ERROR undeclared name: nosuchpkg */ .Nosuchtype
96 }
97 type I interface {
98 I.m /* ERROR no field or method m */
99 m()
100 }
101 }
102
103 // issue11347
104 // These should not crash.
105 var a1, b1 /* ERROR cycle */ , c1 /* ERROR cycle */ b1 = 0 > 0<<""[""[c1]]>c1
106 var a2, b2 /* ERROR cycle */ = 0 /* ERROR cannot initialize */ /* ERROR cannot initialize */ > 0<<""[b2]
107 var a3, b3 /* ERROR cycle */ = int /* ERROR cannot initialize */ /* ERROR cannot initialize */ (1<<""[b3])
108
109 // issue10260
110 // Check that error messages explain reason for interface assignment failures.
111 type (
112 I0 interface{}
113 I1 interface{ foo() }
114 I2 interface{ foo(x int) }
115 T0 struct{}
116 T1 struct{}
117 T2 struct{}
118 )
119
120 func (*T1) foo() {}
121 func (*T2) foo(x int) {}
122
123 func issue10260() {
124 var (
125 i0 I0
126 i1 I1
127 i2 I2
128 t0 *T0
129 t1 *T1
130 t2 *T2
131 )
132
133 var x I1
134 x = T1 /* ERROR cannot use \(T1 literal\) .* as I1 value in assignment: T1 does not implement I1 \(method foo has pointer receiver\) */ {}
135 _ = x /* ERROR impossible type assertion: x\.\(T1\)\n\tT1 does not implement I1 \(method foo has pointer receiver\) */ .(T1)
136
137 T1{}.foo /* ERROR cannot call pointer method foo on T1 */ ()
138 x.Foo /* ERROR "x.Foo undefined \(type I1 has no field or method Foo, but does have foo\)" */ ()
139
140 _ = i2 /* ERROR impossible type assertion: i2\.\(\*T1\)\n\t\*T1 does not implement I2 \(wrong type for method foo\)\n\t\thave foo\(\)\n\t\twant foo\(x int\) */ .(*T1)
141
142 i1 = i0 /* ERROR cannot use i0 .* as I1 value in assignment: I0 does not implement I1 \(missing method foo\) */
143 i1 = t0 /* ERROR .* t0 .* as I1 .*: \*T0 does not implement I1 \(missing method foo\) */
144 i1 = i2 /* ERROR .* i2 .* as I1 .*: I2 does not implement I1 \(wrong type for method foo\)\n\t\thave foo\(x int\)\n\t\twant foo\(\) */
145 i1 = t2 /* ERROR .* t2 .* as I1 .*: \*T2 does not implement I1 \(wrong type for method foo\)\n\t\thave foo\(x int\)\n\t\twant foo\(\) */
146 i2 = i1 /* ERROR .* i1 .* as I2 .*: I1 does not implement I2 \(wrong type for method foo\)\n\t\thave foo\(\)\n\t\twant foo\(x int\) */
147 i2 = t1 /* ERROR .* t1 .* as I2 .*: \*T1 does not implement I2 \(wrong type for method foo\)\n\t\thave foo\(\)\n\t\twant foo\(x int\) */
148
149 _ = func() I1 { return i0 /* ERROR cannot use i0 .* as I1 value in return statement: I0 does not implement I1 \(missing method foo\) */ }
150 _ = func() I1 { return t0 /* ERROR .* t0 .* as I1 .*: \*T0 does not implement I1 \(missing method foo\) */ }
151 _ = func() I1 { return i2 /* ERROR .* i2 .* as I1 .*: I2 does not implement I1 \(wrong type for method foo\)\n\t\thave foo\(x int\)\n\t\twant foo\(\) */ }
152 _ = func() I1 { return t2 /* ERROR .* t2 .* as I1 .*: \*T2 does not implement I1 \(wrong type for method foo\)\n\t\thave foo\(x int\)\n\t\twant foo\(\) */ }
153 _ = func() I2 { return i1 /* ERROR .* i1 .* as I2 .*: I1 does not implement I2 \(wrong type for method foo\)\n\t\thave foo\(\)\n\t\twant foo\(x int\) */ }
154 _ = func() I2 { return t1 /* ERROR .* t1 .* as I2 .*: \*T1 does not implement I2 \(wrong type for method foo\)\n\t\thave foo\(\)\n\t\twant foo\(x int\) */ }
155
156 // a few more - less exhaustive now
157
158 f := func(I1, I2){}
159 f(i0 /* ERROR missing method foo */ , i1 /* ERROR wrong type for method foo */ )
160
161 _ = [...]I1{i0 /* ERROR cannot use i0 .* as I1 value in array or slice literal: I0 does not implement I1 \(missing method foo\) */ }
162 _ = [...]I1{i2 /* ERROR cannot use i2 .* as I1 value in array or slice literal: I2 does not implement I1 \(wrong type for method foo\)\n\t\thave foo\(x int\)\n\t\twant foo\(\) */ }
163 _ = []I1{i0 /* ERROR missing method foo */ }
164 _ = []I1{i2 /* ERROR wrong type for method foo */ }
165 _ = map[int]I1{0: i0 /* ERROR missing method foo */ }
166 _ = map[int]I1{0: i2 /* ERROR wrong type for method foo */ }
167
168 make(chan I1) <- i0 /* ERROR missing method foo */
169 make(chan I1) <- i2 /* ERROR wrong type for method foo */
170 }
171
172 // Check that constants representable as integers are in integer form
173 // before being used in operations that are only defined on integers.
174 func issue14229() {
175 // from the issue
176 const _ = int64(-1<<63) % 1e6
177
178 // related
179 const (
180 a int = 3
181 b = 4.0
182 _ = a / b
183 _ = a % b
184 _ = b / a
185 _ = b % a
186 )
187 }
188
189 // Check that in a n:1 variable declaration with type and initialization
190 // expression the type is distributed to all variables of the lhs before
191 // the initialization expression assignment is checked.
192 func issue15755() {
193 // from issue
194 var i interface{}
195 type b bool
196 var x, y b = i.(b)
197 _ = x == y
198
199 // related: we should see an error since the result of f1 is ([]int, int)
200 var u, v []int = f1 /* ERROR cannot use f1 */ ()
201 _ = u
202 _ = v
203 }
204
205 // Test that we don't get "declared but not used"
206 // errors in the context of invalid/C objects.
207 func issue20358() {
208 var F C /* ERROR "undeclared" */ .F
209 var A C /* ERROR "undeclared" */ .A
210 var S C /* ERROR "undeclared" */ .S
211 type T C /* ERROR "undeclared" */ .T
212 type P C /* ERROR "undeclared" */ .P
213
214 // these variables must be "used" even though
215 // the LHS expressions/types below in which
216 // context they are used are unknown/invalid
217 var f, a, s1, s2, s3, t, p int
218
219 _ = F(f)
220 _ = A[a]
221 _ = S[s1:s2:s3]
222 _ = T{t}
223 _ = P{f: p}
224 }
225
226 // Test that we don't declare lhs variables in short variable
227 // declarations before we type-check function literals on the
228 // rhs.
229 func issue24026() {
230 f := func() int { f(0) /* must refer to outer f */; return 0 }
231 _ = f
232
233 _ = func() {
234 f := func() { _ = f() /* must refer to outer f */ }
235 _ = f
236 }
237
238 // b and c must not be visible inside function literal
239 a := 0
240 a, b, c := func() (int, int, int) {
241 return a, b /* ERROR undeclared */ , c /* ERROR undeclared */
242 }()
243 _, _ = b, c
244 }
245
246 func f(int) {} // for issue24026
247
248 // Test that we don't report a "missing return statement" error
249 // (due to incorrect context when type-checking interfaces).
250 func issue24140(x interface{}) int {
251 switch x.(type) {
252 case interface{}:
253 return 0
254 default:
255 panic(0)
256 }
257 }
258
259 // Test that we don't crash when the 'if' condition is missing.
260 func issue25438() {
261 if { /* ERROR missing condition */ }
262 if x := 0; /* ERROR missing condition */ { _ = x }
263 if
264 { /* ERROR missing condition */ }
265 }
266
267 // Test that we can embed alias type names in interfaces.
268 type issue25301 interface {
269 E
270 }
271
272 type E = interface {
273 m()
274 }
275
276 // Test case from issue.
277 // cmd/compile reports a cycle as well.
278 type issue25301b /* ERROR cycle */ = interface {
279 m() interface{ issue25301b }
280 }
281
282 type issue25301c interface {
283 notE // ERROR non-interface type struct\{\}
284 }
285
286 type notE = struct{}
287
288 // Test that method declarations don't introduce artificial cycles
289 // (issue #26124).
290 const CC TT = 1
291 type TT int
292 func (TT) MM() [CC]TT
293
294 // Reduced test case from issue #26124.
295 const preloadLimit LNumber = 128
296 type LNumber float64
297 func (LNumber) assertFunction() *LFunction
298 type LFunction struct {
299 GFunction LGFunction
300 }
301 type LGFunction func(*LState)
302 type LState struct {
303 reg *registry
304 }
305 type registry struct {
306 alloc *allocator
307 }
308 type allocator struct {
309 _ [int(preloadLimit)]int
310 }
311
312 // Test that we don't crash when type-checking composite literals
313 // containing errors in the type.
314 var issue27346 = [][n /* ERROR undeclared */ ]int{
315 0: {},
316 }
317
318 var issue22467 = map[int][... /* ERROR invalid use of ... */ ]int{0: {}}
319
320 // Test that invalid use of ... in parameter lists is recognized
321 // (issue #28281).
322 func issue28281a(int, int, ...int)
323 func issue28281b(a, b int, c ...int)
324 func issue28281c(a, b, c ... /* ERROR can only use ... with final parameter */ int)
325 func issue28281d(... /* ERROR can only use ... with final parameter */ int, int)
326 func issue28281e(a, b, c ... /* ERROR can only use ... with final parameter */ int, d int)
327 func issue28281f(... /* ERROR can only use ... with final parameter */ int, ... /* ERROR can only use ... with final parameter */ int, int)
328 func (... /* ERROR can only use ... with final parameter */ TT) f()
329 func issue28281g() (... /* ERROR can only use ... with final parameter */ TT)
330
331 // Issue #26234: Make various field/method lookup errors easier to read by matching cmd/compile's output
332 func issue26234a(f *syn.Prog) {
333 // The error message below should refer to the actual package name (syntax)
334 // not the local package name (syn).
335 f.foo /* ERROR f\.foo undefined \(type \*syntax\.Prog has no field or method foo\) */
336 }
337
338 type T struct {
339 x int
340 E1
341 E2
342 }
343
344 type E1 struct{ f int }
345 type E2 struct{ f int }
346
347 func issue26234b(x T) {
348 _ = x.f /* ERROR ambiguous selector x.f */
349 }
350
351 func issue26234c() {
352 T.x /* ERROR T.x undefined \(type T has no method x\) */ ()
353 }
354
355 func issue35895() {
356 // T is defined in this package, don't qualify its name with the package name.
357 var _ T = 0 // ERROR cannot use 0 \(untyped int constant\) as T
358
359 // There is only one package with name syntax imported, only use the (global) package name in error messages.
360 var _ *syn.Prog = 0 // ERROR cannot use 0 \(untyped int constant\) as \*syntax.Prog
361
362 // Because both t1 and t2 have the same global package name (template),
363 // qualify packages with full path name in this case.
364 var _ t1.Template = t2 /* ERROR cannot use .* \(value of type .html/template.\.Template\) as .text/template.\.Template */ .Template{}
365 }
366
367 func issue42989(s uint) {
368 var m map[int]string
369 delete(m, 1<<s)
370 delete(m, 1.<<s)
371 }
372
View as plain text