2012-02-17 04:50:37 +00:00
|
|
|
// errorcheck
|
2009-01-09 02:06:06 +00:00
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-19 06:33:41 +00:00
|
|
|
// Verify compiler messages about erroneous static interface conversions.
|
|
|
|
// Does not compile.
|
2009-05-12 23:09:47 +00:00
|
|
|
|
2009-01-09 02:06:06 +00:00
|
|
|
package main
|
|
|
|
|
2010-06-09 01:50:02 +00:00
|
|
|
type T struct {
|
|
|
|
a int
|
|
|
|
}
|
|
|
|
|
2009-01-09 02:06:06 +00:00
|
|
|
var t *T
|
|
|
|
|
2012-09-01 17:52:55 +00:00
|
|
|
type X int
|
|
|
|
|
|
|
|
func (x *X) M() {}
|
|
|
|
|
2010-06-09 01:50:02 +00:00
|
|
|
type I interface {
|
|
|
|
M()
|
|
|
|
}
|
|
|
|
|
2009-01-09 02:06:06 +00:00
|
|
|
var i I
|
|
|
|
|
2010-06-09 01:50:02 +00:00
|
|
|
type I2 interface {
|
|
|
|
M()
|
|
|
|
N()
|
|
|
|
}
|
|
|
|
|
2009-07-07 00:20:48 +00:00
|
|
|
var i2 I2
|
2009-02-12 01:55:16 +00:00
|
|
|
|
2010-06-09 01:50:02 +00:00
|
|
|
type E interface{}
|
|
|
|
|
2009-07-07 00:20:48 +00:00
|
|
|
var e E
|
2009-02-12 01:55:16 +00:00
|
|
|
|
2009-01-09 02:06:06 +00:00
|
|
|
func main() {
|
2010-06-09 01:50:02 +00:00
|
|
|
e = t // ok
|
2021-11-10 16:41:21 +00:00
|
|
|
t = e // ERROR "need explicit|need type assertion"
|
2009-02-12 01:55:16 +00:00
|
|
|
|
2009-01-09 02:06:06 +00:00
|
|
|
// neither of these can work,
|
|
|
|
// because i has an extra method
|
|
|
|
// that t does not, so i cannot contain a t.
|
2022-09-22 04:11:49 +00:00
|
|
|
i = t // ERROR "incompatible|missing method M"
|
2012-08-15 23:53:06 +00:00
|
|
|
t = i // ERROR "incompatible|assignment$"
|
2010-06-09 01:50:02 +00:00
|
|
|
|
|
|
|
i = i2 // ok
|
2022-09-22 04:11:49 +00:00
|
|
|
i2 = i // ERROR "incompatible|missing method N"
|
2009-02-12 01:55:16 +00:00
|
|
|
|
2010-06-09 01:50:02 +00:00
|
|
|
i = I(i2) // ok
|
2020-12-02 01:37:12 +00:00
|
|
|
i2 = I2(i) // ERROR "invalid|missing N method|cannot convert"
|
2009-07-07 00:20:48 +00:00
|
|
|
|
2010-06-09 01:50:02 +00:00
|
|
|
e = E(t) // ok
|
2020-12-02 01:37:12 +00:00
|
|
|
t = T(e) // ERROR "need explicit|need type assertion|incompatible|cannot convert"
|
2017-04-22 13:28:58 +00:00
|
|
|
|
|
|
|
// cannot type-assert non-interfaces
|
|
|
|
f := 2.0
|
2021-11-12 00:32:16 +00:00
|
|
|
_ = f.(int) // ERROR "non-interface type|only valid for interface types|not an interface"
|
2017-04-22 13:28:58 +00:00
|
|
|
|
2009-01-09 02:06:06 +00:00
|
|
|
}
|
2010-06-09 18:00:55 +00:00
|
|
|
|
2011-01-20 04:09:00 +00:00
|
|
|
type M interface {
|
|
|
|
M()
|
|
|
|
}
|
|
|
|
|
2010-06-09 18:00:55 +00:00
|
|
|
var m M
|
|
|
|
|
2011-01-20 04:09:00 +00:00
|
|
|
var _ = m.(int) // ERROR "impossible type assertion"
|
2010-06-09 18:00:55 +00:00
|
|
|
|
|
|
|
type Int int
|
|
|
|
|
2011-01-20 04:09:00 +00:00
|
|
|
func (Int) M(float64) {}
|
|
|
|
|
|
|
|
var _ = m.(Int) // ERROR "impossible type assertion"
|
2010-06-09 18:00:55 +00:00
|
|
|
|
2012-09-01 17:52:55 +00:00
|
|
|
var _ = m.(X) // ERROR "pointer receiver"
|
|
|
|
|
2010-06-09 18:00:55 +00:00
|
|
|
var ii int
|
|
|
|
var jj Int
|
|
|
|
|
2011-01-20 04:09:00 +00:00
|
|
|
var m1 M = ii // ERROR "incompatible|missing"
|
2022-09-22 04:11:49 +00:00
|
|
|
var m2 M = jj // ERROR "incompatible|wrong type for method M"
|
2010-06-09 18:00:55 +00:00
|
|
|
|
2020-12-02 01:37:12 +00:00
|
|
|
var m3 = M(ii) // ERROR "invalid|missing|cannot convert"
|
|
|
|
var m4 = M(jj) // ERROR "invalid|wrong type for M method|cannot convert"
|
2013-08-19 01:53:34 +00:00
|
|
|
|
|
|
|
type B1 interface {
|
2014-10-15 16:55:13 +00:00
|
|
|
_() // ERROR "methods must have a unique non-blank name"
|
2013-08-19 01:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type B2 interface {
|
|
|
|
M()
|
2014-10-15 16:55:13 +00:00
|
|
|
_() // ERROR "methods must have a unique non-blank name"
|
2013-08-19 01:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type T2 struct{}
|
|
|
|
|
|
|
|
func (t *T2) M() {}
|
|
|
|
func (t *T2) _() {}
|
|
|
|
|
2020-12-03 22:00:19 +00:00
|
|
|
// Already reported about the invalid blank interface method above;
|
|
|
|
// no need to report about not implementing it.
|
|
|
|
var b1 B1 = &T2{}
|
|
|
|
var b2 B2 = &T2{}
|