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.
|
|
|
|
|
2009-05-12 17:09:47 -06:00
|
|
|
// Static error messages about interface conversions.
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
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"
|
|
|
|
t = i // ERROR "incompatible|need type assertion"
|
|
|
|
|
|
|
|
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
|
2010-09-08 22:03:24 -06:00
|
|
|
i2 = I2(i) // ERROR "invalid|missing N method"
|
2009-07-06 18:20:48 -06:00
|
|
|
|
2010-06-08 19:50:02 -06:00
|
|
|
e = E(t) // ok
|
2011-08-16 09:14:26 -06:00
|
|
|
t = T(e) // ERROR "need explicit|need type assertion|incompatible" "as type [*]T"
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2011-01-19 21:09:00 -07:00
|
|
|
var m3 = M(ii) // ERROR "invalid|missing"
|
|
|
|
var m4 = M(jj) // ERROR "invalid|wrong type for M method"
|