blob: b10d02f2485adb66c1411351ce4909661325ef6d [file] [log] [blame]
Russ Coxd2cc9882012-02-16 23:50:37 -05001// errorcheck
Russ Coxe5124812009-01-08 18:06:06 -08002
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 Pike13514d42012-02-19 17:33:41 +11007// Verify compiler messages about erroneous static interface conversions.
8// Does not compile.
Russ Coxc7d30bc2009-05-12 16:09:47 -07009
Russ Coxe5124812009-01-08 18:06:06 -080010package main
11
Russ Cox565b5dc2010-06-08 18:50:02 -070012type T struct {
13 a int
14}
15
Russ Coxe5124812009-01-08 18:06:06 -080016var t *T
17
Daniel Morsing1c2021c2012-09-01 13:52:55 -040018type X int
19
20func (x *X) M() {}
21
Russ Cox565b5dc2010-06-08 18:50:02 -070022type I interface {
23 M()
24}
25
Russ Coxe5124812009-01-08 18:06:06 -080026var i I
27
Russ Cox565b5dc2010-06-08 18:50:02 -070028type I2 interface {
29 M()
30 N()
31}
32
Russ Coxd436a702009-07-06 17:20:48 -070033var i2 I2
Russ Cox49e20872009-02-11 17:55:16 -080034
Russ Cox565b5dc2010-06-08 18:50:02 -070035type E interface{}
36
Russ Coxd436a702009-07-06 17:20:48 -070037var e E
Russ Cox49e20872009-02-11 17:55:16 -080038
Russ Coxe5124812009-01-08 18:06:06 -080039func main() {
Russ Cox565b5dc2010-06-08 18:50:02 -070040 e = t // ok
41 t = e // ERROR "need explicit|need type assertion"
Russ Cox49e20872009-02-11 17:55:16 -080042
Russ Coxe5124812009-01-08 18:06:06 -080043 // neither of these can work,
44 // because i has an extra method
45 // that t does not, so i cannot contain a t.
Russ Cox565b5dc2010-06-08 18:50:02 -070046 i = t // ERROR "incompatible|missing M method"
Daniel Morsingb04c8902012-08-15 16:53:06 -070047 t = i // ERROR "incompatible|assignment$"
Russ Cox49e20872009-02-11 17:55:16 -080048
Russ Cox565b5dc2010-06-08 18:50:02 -070049 i = i2 // ok
Ian Lance Taylor2d8433a2010-09-08 21:03:24 -070050 i2 = i // ERROR "incompatible|missing N method"
Russ Coxd436a702009-07-06 17:20:48 -070051
Russ Cox565b5dc2010-06-08 18:50:02 -070052 i = I(i2) // ok
Ian Lance Taylor2d8433a2010-09-08 21:03:24 -070053 i2 = I2(i) // ERROR "invalid|missing N method"
Russ Cox565b5dc2010-06-08 18:50:02 -070054
55 e = E(t) // ok
Russ Coxa5d7c1f2011-08-16 11:14:26 -040056 t = T(e) // ERROR "need explicit|need type assertion|incompatible" "as type [*]T"
Russ Coxe5124812009-01-08 18:06:06 -080057}
Russ Coxa2a7d472010-06-09 11:00:55 -070058
Russ Coxf2b5a072011-01-19 23:09:00 -050059type M interface {
60 M()
61}
62
Russ Coxa2a7d472010-06-09 11:00:55 -070063var m M
64
Russ Coxf2b5a072011-01-19 23:09:00 -050065var _ = m.(int) // ERROR "impossible type assertion"
Russ Coxa2a7d472010-06-09 11:00:55 -070066
67type Int int
Russ Coxa2a7d472010-06-09 11:00:55 -070068
Russ Coxf2b5a072011-01-19 23:09:00 -050069func (Int) M(float64) {}
70
71var _ = m.(Int) // ERROR "impossible type assertion"
Russ Coxa2a7d472010-06-09 11:00:55 -070072
Daniel Morsing1c2021c2012-09-01 13:52:55 -040073var _ = m.(X) // ERROR "pointer receiver"
74
Russ Coxa2a7d472010-06-09 11:00:55 -070075var ii int
76var jj Int
77
Russ Coxf2b5a072011-01-19 23:09:00 -050078var m1 M = ii // ERROR "incompatible|missing"
79var m2 M = jj // ERROR "incompatible|wrong type for M method"
Russ Coxa2a7d472010-06-09 11:00:55 -070080
Russ Coxf2b5a072011-01-19 23:09:00 -050081var m3 = M(ii) // ERROR "invalid|missing"
82var m4 = M(jj) // ERROR "invalid|wrong type for M method"
Anthony Martinf316a7e2013-08-19 11:53:34 +100083
84
85type B1 interface {
Chris Manghanedb4dad72014-10-15 09:55:13 -070086 _() // ERROR "methods must have a unique non-blank name"
Anthony Martinf316a7e2013-08-19 11:53:34 +100087}
88
89type B2 interface {
90 M()
Chris Manghanedb4dad72014-10-15 09:55:13 -070091 _() // ERROR "methods must have a unique non-blank name"
Anthony Martinf316a7e2013-08-19 11:53:34 +100092}
93
94type T2 struct{}
95
96func (t *T2) M() {}
97func (t *T2) _() {}
98
99// Check that nothing satisfies an interface with blank methods.
100var b1 B1 = &T2{} // ERROR "incompatible|missing _ method"
101var b2 B2 = &T2{} // ERROR "incompatible|missing _ method"