modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/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" 57 58 // cannot type-assert non-interfaces 59 f := 2.0 60 _ = f.(int) // ERROR "non-interface type" 61 62 } 63 64 type M interface { 65 M() 66 } 67 68 var m M 69 70 var _ = m.(int) // ERROR "impossible type assertion" 71 72 type Int int 73 74 func (Int) M(float64) {} 75 76 var _ = m.(Int) // ERROR "impossible type assertion" 77 78 var _ = m.(X) // ERROR "pointer receiver" 79 80 var ii int 81 var jj Int 82 83 var m1 M = ii // ERROR "incompatible|missing" 84 var m2 M = jj // ERROR "incompatible|wrong type for M method" 85 86 var m3 = M(ii) // ERROR "invalid|missing" 87 var m4 = M(jj) // ERROR "invalid|wrong type for M method" 88 89 type B1 interface { 90 _() // ERROR "methods must have a unique non-blank name" 91 } 92 93 type B2 interface { 94 M() 95 _() // ERROR "methods must have a unique non-blank name" 96 } 97 98 type T2 struct{} 99 100 func (t *T2) M() {} 101 func (t *T2) _() {} 102 103 // Check that nothing satisfies an interface with blank methods. 104 var b1 B1 = &T2{} // ERROR "incompatible|missing _ method" 105 var b2 B2 = &T2{} // ERROR "incompatible|missing _ method"