github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/test/interface/explicit.go (about) 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 10 package main 11 12 type T struct { 13 a int 14 } 15 16 var t *T 17 18 type X int 19 20 func (x *X) M() {} 21 22 type I interface { 23 M() 24 } 25 26 var i I 27 28 type I2 interface { 29 M() 30 N() 31 } 32 33 var i2 I2 34 35 type E interface{} 36 37 var e E 38 39 func 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 M method" 47 t = i // ERROR "incompatible|assignment$" 48 49 i = i2 // ok 50 i2 = i // ERROR "incompatible|missing N method" 51 52 i = I(i2) // ok 53 i2 = I2(i) // ERROR "invalid|missing N method" 54 55 e = E(t) // ok 56 t = T(e) // ERROR "need explicit|need type assertion|incompatible" "as type [*]T" 57 } 58 59 type M interface { 60 M() 61 } 62 63 var m M 64 65 var _ = m.(int) // ERROR "impossible type assertion" 66 67 type Int int 68 69 func (Int) M(float64) {} 70 71 var _ = m.(Int) // ERROR "impossible type assertion" 72 73 var _ = m.(X) // ERROR "pointer receiver" 74 75 var ii int 76 var jj Int 77 78 var m1 M = ii // ERROR "incompatible|missing" 79 var m2 M = jj // ERROR "incompatible|wrong type for M method" 80 81 var m3 = M(ii) // ERROR "invalid|missing" 82 var m4 = M(jj) // ERROR "invalid|wrong type for M method" 83 84 85 type B1 interface { 86 _() // ERROR "methods must have a unique non-blank name" 87 } 88 89 type B2 interface { 90 M() 91 _() // ERROR "methods must have a unique non-blank name" 92 } 93 94 type T2 struct{} 95 96 func (t *T2) M() {} 97 func (t *T2) _() {} 98 99 // Check that nothing satisfies an interface with blank methods. 100 var b1 B1 = &T2{} // ERROR "incompatible|missing _ method" 101 var b2 B2 = &T2{} // ERROR "incompatible|missing _ method"