Russ Cox | d2cc988 | 2012-02-16 23:50:37 -0500 | [diff] [blame] | 1 | // errorcheck |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 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 | |
Rob Pike | 13514d4 | 2012-02-19 17:33:41 +1100 | [diff] [blame] | 7 | // Verify compiler messages about erroneous static interface conversions. |
| 8 | // Does not compile. |
Russ Cox | c7d30bc | 2009-05-12 16:09:47 -0700 | [diff] [blame] | 9 | |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 10 | package main |
| 11 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 12 | type T struct { |
| 13 | a int |
| 14 | } |
| 15 | |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 16 | var t *T |
| 17 | |
Daniel Morsing | 1c2021c | 2012-09-01 13:52:55 -0400 | [diff] [blame] | 18 | type X int |
| 19 | |
| 20 | func (x *X) M() {} |
| 21 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 22 | type I interface { |
| 23 | M() |
| 24 | } |
| 25 | |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 26 | var i I |
| 27 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 28 | type I2 interface { |
| 29 | M() |
| 30 | N() |
| 31 | } |
| 32 | |
Russ Cox | d436a70 | 2009-07-06 17:20:48 -0700 | [diff] [blame] | 33 | var i2 I2 |
Russ Cox | 49e2087 | 2009-02-11 17:55:16 -0800 | [diff] [blame] | 34 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 35 | type E interface{} |
| 36 | |
Russ Cox | d436a70 | 2009-07-06 17:20:48 -0700 | [diff] [blame] | 37 | var e E |
Russ Cox | 49e2087 | 2009-02-11 17:55:16 -0800 | [diff] [blame] | 38 | |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 39 | func main() { |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 40 | e = t // ok |
| 41 | t = e // ERROR "need explicit|need type assertion" |
Russ Cox | 49e2087 | 2009-02-11 17:55:16 -0800 | [diff] [blame] | 42 | |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 43 | // neither of these can work, |
| 44 | // because i has an extra method |
| 45 | // that t does not, so i cannot contain a t. |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 46 | i = t // ERROR "incompatible|missing M method" |
Daniel Morsing | b04c890 | 2012-08-15 16:53:06 -0700 | [diff] [blame] | 47 | t = i // ERROR "incompatible|assignment$" |
Russ Cox | 49e2087 | 2009-02-11 17:55:16 -0800 | [diff] [blame] | 48 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 49 | i = i2 // ok |
Ian Lance Taylor | 2d8433a | 2010-09-08 21:03:24 -0700 | [diff] [blame] | 50 | i2 = i // ERROR "incompatible|missing N method" |
Russ Cox | d436a70 | 2009-07-06 17:20:48 -0700 | [diff] [blame] | 51 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 52 | i = I(i2) // ok |
Ian Lance Taylor | 2d8433a | 2010-09-08 21:03:24 -0700 | [diff] [blame] | 53 | i2 = I2(i) // ERROR "invalid|missing N method" |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 54 | |
| 55 | e = E(t) // ok |
Russ Cox | a5d7c1f | 2011-08-16 11:14:26 -0400 | [diff] [blame] | 56 | t = T(e) // ERROR "need explicit|need type assertion|incompatible" "as type [*]T" |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 57 | } |
Russ Cox | a2a7d47 | 2010-06-09 11:00:55 -0700 | [diff] [blame] | 58 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 59 | type M interface { |
| 60 | M() |
| 61 | } |
| 62 | |
Russ Cox | a2a7d47 | 2010-06-09 11:00:55 -0700 | [diff] [blame] | 63 | var m M |
| 64 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 65 | var _ = m.(int) // ERROR "impossible type assertion" |
Russ Cox | a2a7d47 | 2010-06-09 11:00:55 -0700 | [diff] [blame] | 66 | |
| 67 | type Int int |
Russ Cox | a2a7d47 | 2010-06-09 11:00:55 -0700 | [diff] [blame] | 68 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 69 | func (Int) M(float64) {} |
| 70 | |
| 71 | var _ = m.(Int) // ERROR "impossible type assertion" |
Russ Cox | a2a7d47 | 2010-06-09 11:00:55 -0700 | [diff] [blame] | 72 | |
Daniel Morsing | 1c2021c | 2012-09-01 13:52:55 -0400 | [diff] [blame] | 73 | var _ = m.(X) // ERROR "pointer receiver" |
| 74 | |
Russ Cox | a2a7d47 | 2010-06-09 11:00:55 -0700 | [diff] [blame] | 75 | var ii int |
| 76 | var jj Int |
| 77 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 78 | var m1 M = ii // ERROR "incompatible|missing" |
| 79 | var m2 M = jj // ERROR "incompatible|wrong type for M method" |
Russ Cox | a2a7d47 | 2010-06-09 11:00:55 -0700 | [diff] [blame] | 80 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 81 | var m3 = M(ii) // ERROR "invalid|missing" |
| 82 | var m4 = M(jj) // ERROR "invalid|wrong type for M method" |
Anthony Martin | f316a7e | 2013-08-19 11:53:34 +1000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | type B1 interface { |
Chris Manghane | db4dad7 | 2014-10-15 09:55:13 -0700 | [diff] [blame] | 86 | _() // ERROR "methods must have a unique non-blank name" |
Anthony Martin | f316a7e | 2013-08-19 11:53:34 +1000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | type B2 interface { |
| 90 | M() |
Chris Manghane | db4dad7 | 2014-10-15 09:55:13 -0700 | [diff] [blame] | 91 | _() // ERROR "methods must have a unique non-blank name" |
Anthony Martin | f316a7e | 2013-08-19 11:53:34 +1000 | [diff] [blame] | 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" |