2012-02-16 21:50:37 -07:00
|
|
|
// errorcheck
|
2009-01-08 19:06:06 -07: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-18 23:33:41 -07:00
|
|
|
// Verify compiler messages about erroneous static interface conversions.
|
|
|
|
// Does not compile.
|
2009-05-12 17:09:47 -06:00
|
|
|
|
2009-01-08 19:06:06 -07:00
|
|
|
package main
|
|
|
|
|
2010-06-08 19:50:02 -06:00
|
|
|
type T struct {
|
|
|
|
a int
|
|
|
|
}
|
|
|
|
|
2009-01-08 19:06:06 -07:00
|
|
|
var t *T
|
|
|
|
|
2012-09-01 11:52:55 -06:00
|
|
|
type X int
|
|
|
|
|
|
|
|
func (x *X) M() {}
|
|
|
|
|
2010-06-08 19:50:02 -06:00
|
|
|
type I interface {
|
|
|
|
M()
|
|
|
|
}
|
|
|
|
|
2009-01-08 19:06:06 -07:00
|
|
|
var i I
|
|
|
|
|
2010-06-08 19:50:02 -06:00
|
|
|
type I2 interface {
|
|
|
|
M()
|
|
|
|
N()
|
|
|
|
}
|
|
|
|
|
2009-07-06 18:20:48 -06:00
|
|
|
var i2 I2
|
2009-02-11 18:55:16 -07:00
|
|
|
|
2010-06-08 19:50:02 -06:00
|
|
|
type E interface{}
|
|
|
|
|
2009-07-06 18:20:48 -06:00
|
|
|
var e E
|
2009-02-11 18:55:16 -07:00
|
|
|
|
2009-01-08 19:06:06 -07:00
|
|
|
func main() {
|
2010-06-08 19:50:02 -06:00
|
|
|
e = t // ok
|
2021-11-10 09:41:21 -07:00
|
|
|
t = e // ERROR "need explicit|need type assertion"
|
2009-02-11 18:55:16 -07:00
|
|
|
|
2009-01-08 19:06:06 -07:00
|
|
|
// neither of these can work,
|
|
|
|
// because i has an extra method
|
|
|
|
// that t does not, so i cannot contain a t.
|
2010-06-08 19:50:02 -06:00
|
|
|
i = t // ERROR "incompatible|missing M method"
|
2012-08-15 17:53:06 -06:00
|
|
|
t = i // ERROR "incompatible|assignment$"
|
2010-06-08 19:50:02 -06:00
|
|
|
|
|
|
|
i = i2 // ok
|
2010-09-08 22:03:24 -06:00
|
|
|
i2 = i // ERROR "incompatible|missing N method"
|
2009-02-11 18:55:16 -07:00
|
|
|
|
2010-06-08 19:50:02 -06:00
|
|
|
i = I(i2) // ok
|
2020-12-01 18:37:12 -07:00
|
|
|
i2 = I2(i) // ERROR "invalid|missing N method|cannot convert"
|
2009-07-06 18:20:48 -06:00
|
|
|
|
2010-06-08 19:50:02 -06:00
|
|
|
e = E(t) // ok
|
2020-12-01 18:37:12 -07:00
|
|
|
t = T(e) // ERROR "need explicit|need type assertion|incompatible|cannot convert"
|
2017-04-22 07:28:58 -06:00
|
|
|
|
|
|
|
// cannot type-assert non-interfaces
|
|
|
|
f := 2.0
|
2021-11-11 17:32:16 -07:00
|
|
|
_ = f.(int) // ERROR "non-interface type|only valid for interface types|not an interface"
|
2017-04-22 07:28:58 -06:00
|
|
|
|
2009-01-08 19:06:06 -07:00
|
|
|
}
|
2010-06-09 12:00:55 -06:00
|
|
|
|
2011-01-19 21:09:00 -07:00
|
|
|
type M interface {
|
|
|
|
M()
|
|
|
|
}
|
|
|
|
|
2010-06-09 12:00:55 -06:00
|
|
|
var m M
|
|
|
|
|
2011-01-19 21:09:00 -07:00
|
|
|
var _ = m.(int) // ERROR "impossible type assertion"
|
2010-06-09 12:00:55 -06:00
|
|
|
|
|
|
|
type Int int
|
|
|
|
|
2011-01-19 21:09:00 -07:00
|
|
|
func (Int) M(float64) {}
|
|
|
|
|
|
|
|
var _ = m.(Int) // ERROR "impossible type assertion"
|
2010-06-09 12:00:55 -06:00
|
|
|
|
2012-09-01 11:52:55 -06:00
|
|
|
var _ = m.(X) // ERROR "pointer receiver"
|
|
|
|
|
2010-06-09 12:00:55 -06:00
|
|
|
var ii int
|
|
|
|
var jj Int
|
|
|
|
|
2011-01-19 21:09:00 -07:00
|
|
|
var m1 M = ii // ERROR "incompatible|missing"
|
|
|
|
var m2 M = jj // ERROR "incompatible|wrong type for M method"
|
2010-06-09 12:00:55 -06:00
|
|
|
|
2020-12-01 18:37:12 -07: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-18 19:53:34 -06:00
|
|
|
|
|
|
|
type B1 interface {
|
2014-10-15 10:55:13 -06:00
|
|
|
_() // ERROR "methods must have a unique non-blank name"
|
2013-08-18 19:53:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type B2 interface {
|
|
|
|
M()
|
2014-10-15 10:55:13 -06:00
|
|
|
_() // ERROR "methods must have a unique non-blank name"
|
2013-08-18 19:53:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type T2 struct{}
|
|
|
|
|
|
|
|
func (t *T2) M() {}
|
|
|
|
func (t *T2) _() {}
|
|
|
|
|
2020-12-03 15:00:19 -07: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{}
|