aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/types2/testdata/check/issues.src
blob: 692ed37ef4b9e881f788155fbd1f030201952564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package go1_17 // don't permit non-interface elements in interfaces

import (
	"fmt"
	syn "regexp/syntax"
	t1 "text/template"
	t2 "html/template"
)

func issue7035() {
	type T struct{ X int }
	_ = func() {
		fmt.Println() // must refer to imported fmt rather than the fmt below
	}
	fmt := new(T)
	_ = fmt.X
}

func issue8066() {
	const (
		_ = float32(340282356779733661637539395458142568447)
		_ = float32(340282356779733661637539395458142568448 /* ERROR cannot convert */ )
	)
}

// Check that a missing identifier doesn't lead to a spurious error cascade.
func issue8799a() {
	x, ok := missing /* ERROR undeclared */ ()
	_ = !ok
	_ = x
}

func issue8799b(x int, ok bool) {
	x, ok = missing /* ERROR undeclared */ ()
	_ = !ok
	_ = x
}

func issue9182() {
	type Point C /* ERROR undeclared */ .Point
	// no error for composite literal based on unknown type
	_ = Point{x: 1, y: 2}
}

func f0() (a []int)         { return }
func f1() (a []int, b int)  { return }
func f2() (a, b []int)      { return }

func append_([]int, ...int) {}

func issue9473(a []int, b ...int) {
	// variadic builtin function
	_ = append(f0())
	_ = append(f0(), f0()...)
	_ = append(f1())
	_ = append(f2 /* ERROR cannot use .* in argument */ ())
	_ = append(f2()... /* ERROR cannot use ... */ )
	_ = append(f0(), f1 /* ERROR 2-valued f1 */ ())
	_ = append(f0(), f2 /* ERROR 2-valued f2 */ ())
	_ = append(f0(), f1 /* ERROR 2-valued f1 */ ()...)
	_ = append(f0(), f2 /* ERROR 2-valued f2 */ ()...)

	// variadic user-defined function
	append_(f0())
	append_(f0(), f0()...)
	append_(f1())
	append_(f2 /* ERROR cannot use .* in argument */ ())
	append_(f2()... /* ERROR cannot use ... */ )
	append_(f0(), f1 /* ERROR 2-valued f1 */ ())
	append_(f0(), f2 /* ERROR 2-valued f2 */ ())
	append_(f0(), f1 /* ERROR 2-valued f1 */ ()...)
	append_(f0(), f2 /* ERROR 2-valued f2 */ ()...)
}

// Check that embedding a non-interface type in an interface results in a good error message.
func issue10979() {
	type _ interface {
		int /* ERROR int is not an interface */
	}
	type T struct{}
	type _ interface {
		T /* ERROR T is not an interface */
	}
	type _ interface {
		nosuchtype /* ERROR undeclared name: nosuchtype */
	}
	type _ interface {
		fmt.Nosuchtype /* ERROR Nosuchtype not declared by package fmt */
	}
	type _ interface {
		nosuchpkg /* ERROR undeclared name: nosuchpkg */ .Nosuchtype
	}
	type I interface {
		I.m /* ERROR no field or method m */
		m()
	}
}

// issue11347
// These should not crash.
var a1, b1 /* ERROR cycle */ , c1 /* ERROR cycle */ b1 = 0 > 0<<""[""[c1]]>c1
var a2, b2 /* ERROR cycle */ = 0 /* ERROR cannot initialize */ /* ERROR cannot initialize */ > 0<<""[b2]
var a3, b3 /* ERROR cycle */ = int /* ERROR cannot initialize */ /* ERROR cannot initialize */ (1<<""[b3])

// issue10260
// Check that error messages explain reason for interface assignment failures.
type (
	I0 interface{}
	I1 interface{ foo() }
	I2 interface{ foo(x int) }
	T0 struct{}
	T1 struct{}
	T2 struct{}
)

func (*T1) foo() {}
func (*T2) foo(x int) {}

func issue10260() {
	var (
		i0 I0
		i1 I1
		i2 I2
		t0 *T0
		t1 *T1
		t2 *T2
	)

	var x I1
	x = T1 /* ERROR cannot use .*: missing method foo \(foo has pointer receiver\) */ {}
	_ = x /* ERROR .* cannot have dynamic type T1 \(missing method foo \(foo has pointer receiver\)\) */ .(T1)

	T1{}.foo /* ERROR cannot call pointer method foo on T1 */ ()
	x.Foo /* ERROR "x.Foo undefined \(type I1 has no field or method Foo, but does have foo\)" */ ()

	_ = i2 /* ERROR i2 .* cannot have dynamic type \*T1 \(wrong type for method foo \(have func\(\), want func\(x int\)\)\) */ .(*T1)

	i1 = i0 /* ERROR cannot use .* missing method foo */
	i1 = t0 /* ERROR cannot use .* missing method foo */
	i1 = i2 /* ERROR cannot use .* wrong type for method foo */
	i1 = t2 /* ERROR cannot use .* wrong type for method foo */
	i2 = i1 /* ERROR cannot use .* wrong type for method foo */
	i2 = t1 /* ERROR cannot use .* wrong type for method foo */

	_ = func() I1 { return i0 /* ERROR cannot use .* missing method foo */ }
	_ = func() I1 { return t0 /* ERROR cannot use .* missing method foo */ }
	_ = func() I1 { return i2 /* ERROR cannot use .* wrong type for method foo */ }
	_ = func() I1 { return t2 /* ERROR cannot use .* wrong type for method foo */ }
	_ = func() I2 { return i1 /* ERROR cannot use .* wrong type for method foo */ }
	_ = func() I2 { return t1 /* ERROR cannot use .* wrong type for method foo */ }

	// a few more - less exhaustive now

	f := func(I1, I2){}
	f(i0 /* ERROR cannot use .* missing method foo */ , i1 /* ERROR cannot use .* wrong type for method foo \(have func\(\), want func\(x int\)\) */ )

	_ = [...]I1{i0 /* ERROR cannot use .* missing method foo */ }
	_ = [...]I1{i2 /* ERROR cannot use .* wrong type for method foo */ }
	_ = []I1{i0 /* ERROR cannot use .* missing method foo */ }
	_ = []I1{i2 /* ERROR cannot use .* wrong type for method foo */ }
	_ = map[int]I1{0: i0 /* ERROR cannot use .* missing method foo */ }
	_ = map[int]I1{0: i2 /* ERROR cannot use .* wrong type for method foo */ }

	make(chan I1) <- i0 /* ERROR cannot use .* in send: missing method foo */
	make(chan I1) <- i2 /* ERROR cannot use .* in send: wrong type for method foo */
}

// Check that constants representable as integers are in integer form
// before being used in operations that are only defined on integers.
func issue14229() {
	// from the issue
	const _ = int64(-1<<63) % 1e6

	// related
	const (
		a int = 3
		b = 4.0
		_ = a / b
		_ = a % b
		_ = b / a
		_ = b % a
	)
}

// Check that in a n:1 variable declaration with type and initialization
// expression the type is distributed to all variables of the lhs before
// the initialization expression assignment is checked.
func issue15755() {
	// from issue
	var i interface{}
	type b bool
	var x, y b = i.(b)
	_ = x == y

	// related: we should see an error since the result of f1 is ([]int, int)
	var u, v []int = f1 /* ERROR cannot use f1 */ ()
	_ = u
	_ = v
}

// Test that we don't get "declared but not used"
// errors in the context of invalid/C objects.
func issue20358() {
	var F C /* ERROR "undeclared" */ .F
	var A C /* ERROR "undeclared" */ .A
	var S C /* ERROR "undeclared" */ .S
	type T C /* ERROR "undeclared" */ .T
	type P C /* ERROR "undeclared" */ .P

	// these variables must be "used" even though
	// the LHS expressions/types below in which
	// context they are used are unknown/invalid
	var f, a, s1, s2, s3, t, p int

	_ = F(f)
	_ = A[a]
	_ = S[s1:s2:s3]
	_ = T{t}
	_ = P{f: p}
}

// Test that we don't declare lhs variables in short variable
// declarations before we type-check function literals on the
// rhs.
func issue24026() {
	f := func() int { f(0) /* must refer to outer f */; return 0 }
	_ = f

	_ = func() {
		f := func() { _ = f() /* must refer to outer f */ }
		_ = f
	}

	// b and c must not be visible inside function literal
	a := 0
	a, b, c := func() (int, int, int) {
		return a, b /* ERROR undeclared */ , c /* ERROR undeclared */
	}()
	_, _ = b, c
}

func f(int) {} // for issue24026

// Test that we don't report a "missing return statement" error
// (due to incorrect context when type-checking interfaces).
func issue24140(x interface{}) int {
        switch x.(type) {
        case interface{}:
                return 0
        default:
                panic(0)
        }
}

// Test that we don't crash when the 'if' condition is missing.
func issue25438() {
	if { /* ERROR missing condition */ }
	if x := 0; /* ERROR missing condition */ { _ = x }
	if
	{ /* ERROR missing condition */ }
}

// Test that we can embed alias type names in interfaces.
type issue25301 interface {
	E
}

type E = interface {
	m()
}

// Test case from issue.
// cmd/compile reports a cycle as well.
type issue25301b /* ERROR cycle */ = interface {
	m() interface{ issue25301b }
}

type issue25301c interface {
	notE // ERROR struct\{\} is not an interface
}

type notE = struct{}

// Test that method declarations don't introduce artificial cycles
// (issue #26124).
const CC TT = 1
type TT int
func (TT) MM() [CC]TT

// Reduced test case from issue #26124.
const preloadLimit LNumber = 128
type LNumber float64
func (LNumber) assertFunction() *LFunction
type LFunction struct {
	GFunction LGFunction
}
type LGFunction func(*LState)
type LState struct {
	reg *registry
}
type registry struct {
	alloc *allocator
}
type allocator struct {
	_ [int(preloadLimit)]int
}

// Test that we don't crash when type-checking composite literals
// containing errors in the type.
var issue27346 = [][n /* ERROR undeclared */ ]int{
	0: {},
}

var issue22467 = map[int][... /* ERROR invalid use of ... */ ]int{0: {}}

// Test that invalid use of ... in parameter lists is recognized
// (issue #28281).
func issue28281a(int, int, ...int)
func issue28281b(a, b int, c ...int)
func issue28281c(a, b, c ... /* ERROR can only use ... with final parameter */ int)
func issue28281d(... /* ERROR can only use ... with final parameter */ int, int)
func issue28281e(a, b, c  ... /* ERROR can only use ... with final parameter */ int, d int)
func issue28281f(... /* ERROR can only use ... with final parameter */ int, ... /* ERROR can only use ... with final parameter */ int, int)
func (... /* ERROR can only use ... with final parameter in list */ TT) f()
func issue28281g() (... /* ERROR can only use ... with final parameter in list */ TT)

// Issue #26234: Make various field/method lookup errors easier to read by matching cmd/compile's output
func issue26234a(f *syn.Prog) {
	// The error message below should refer to the actual package name (syntax)
	// not the local package name (syn).
	f.foo /* ERROR f\.foo undefined \(type \*syntax\.Prog has no field or method foo\) */
}

type T struct {
	x int
	E1
	E2
}

type E1 struct{ f int }
type E2 struct{ f int }

func issue26234b(x T) {
	_ = x.f /* ERROR ambiguous selector x.f */
}

func issue26234c() {
	T.x /* ERROR T.x undefined \(type T has no method x\) */ ()
}

func issue35895() {
	// T is defined in this package, don't qualify its name with the package name.
	var _ T = 0 // ERROR cannot use 0 \(untyped int constant\) as T

	// There is only one package with name syntax imported, only use the (global) package name in error messages.
	var _ *syn.Prog = 0 // ERROR cannot use 0 \(untyped int constant\) as \*syntax.Prog

	// Because both t1 and t2 have the same global package name (template),
	// qualify packages with full path name in this case.
	var _ t1.Template = t2 /* ERROR cannot use .* \(value of type .html/template.\.Template\) as .text/template.\.Template */ .Template{}
}

func issue42989(s uint) {
	var m map[int]string
	delete(m, 1<<s)
	delete(m, 1.<<s)
}