1// errorcheck
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// Verify compiler messages about erroneous static interface conversions.
8// Does not compile.
9
10package main
11
12type T struct {
13	a int
14}
15
16var t *T
17
18type X int
19
20func (x *X) M() {}
21
22type I interface {
23	M()
24}
25
26var i I
27
28type I2 interface {
29	M()
30	N()
31}
32
33var i2 I2
34
35type E interface{}
36
37var e E
38
39func main() {
40	e = t // ok
41	t = e // ERROR "need explicit|need type assertion"
42
43	// neither of these can work,
44	// because i has an extra method
45	// that t does not, so i cannot contain a t.
46	i = t // ERROR "incompatible|missing method M"
47	t = i // ERROR "incompatible|assignment$"
48
49	i = i2 // ok
50	i2 = i // ERROR "incompatible|missing method N"
51
52	i = I(i2)  // ok
53	i2 = I2(i) // ERROR "invalid|missing N method|cannot convert"
54
55	e = E(t) // ok
56	t = T(e) // ERROR "need explicit|need type assertion|incompatible|cannot convert"
57
58	// cannot type-assert non-interfaces
59	f := 2.0
60	_ = f.(int) // ERROR "non-interface type|only valid for interface types|not an interface"
61
62}
63
64type M interface {
65	M()
66}
67
68var m M
69
70var _ = m.(int) // ERROR "impossible type assertion"
71
72type Int int
73
74func (Int) M(float64) {}
75
76var _ = m.(Int) // ERROR "impossible type assertion"
77
78var _ = m.(X) // ERROR "pointer receiver"
79
80var ii int
81var jj Int
82
83var m1 M = ii // ERROR "incompatible|missing"
84var m2 M = jj // ERROR "incompatible|wrong type for method M"
85
86var m3 = M(ii) // ERROR "invalid|missing|cannot convert"
87var m4 = M(jj) // ERROR "invalid|wrong type for M method|cannot convert"
88
89type B1 interface {
90	_() // ERROR "methods must have a unique non-blank name"
91}
92
93type B2 interface {
94	M()
95	_() // ERROR "methods must have a unique non-blank name"
96}
97
98type T2 struct{}
99
100func (t *T2) M() {}
101func (t *T2) _() {}
102
103// Already reported about the invalid blank interface method above;
104// no need to report about not implementing it.
105var b1 B1 = &T2{}
106var b2 B2 = &T2{}
107