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
|
|
|
|
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
|
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
|
2017-04-13 02:42:16 -06:00
|
|
|
t = T(e) // ERROR "need explicit|need type assertion|incompatible"
|
2017-04-22 07:28:58 -06:00
|
|
|
|
|
|
|
// cannot type-assert non-interfaces
|
|
|
|
f := 2.0
|
test: match gofrontend error messages
These changes match the following gofrontend error messages:
blank1.go:16:1: error: may not define methods on non-local type
chan/perm.go:28:9: error: expected channel
chan/perm.go:29:11: error: left operand of ‘<-’ must be channel
chan/perm.go:69:9: error: argument must be channel
complit1.go:25:16: error: attempt to slice object that is not array, slice, or string
complit1.go:26:16: error: attempt to slice object that is not array, slice, or string
complit1.go:27:17: error: attempt to slice object that is not array, slice, or string
complit1.go:49:41: error: may only omit types within composite literals of slice, array, or map type
complit1.go:50:14: error: expected struct, slice, array, or map type for composite literal
convlit.go:24:9: error: invalid type conversion (cannot use type unsafe.Pointer as type string)
convlit.go:25:9: error: invalid type conversion (cannot use type unsafe.Pointer as type float64)
convlit.go:26:9: error: invalid type conversion (cannot use type unsafe.Pointer as type int)
ddd1.go:63:9: error: invalid use of ‘...’ calling non-variadic function
fixedbugs/bug176.go:12:18: error: index expression is not integer constant
fixedbugs/bug332.go:17:10: error: use of undefined type ‘T’
fixedbugs/issue4232.go:22:16: error: integer constant overflow
fixedbugs/issue4232.go:33:16: error: integer constant overflow
fixedbugs/issue4232.go:44:25: error: integer constant overflow
fixedbugs/issue4232.go:55:16: error: integer constant overflow
fixedbugs/issue4458.go:19:14: error: type has no method ‘foo’
fixedbugs/issue5172.go:24:14: error: too many expressions for struct
init.go:17:9: error: reference to undefined name ‘runtime’
initializerr.go:26:29: error: duplicate value for index 1
interface/explicit.go:60:14: error: type assertion only valid for interface types
label.go:64:9: error: reference to undefined label ‘go2’
label1.go:18:97: error: continue statement not within for
label1.go:22:97: error: continue statement not within for
label1.go:106:89: error: continue statement not within for
label1.go:108:26: error: invalid continue label ‘on’
label1.go:111:118: error: break statement not within for or switch or select
label1.go:113:23: error: invalid break label ‘dance’
map1.go:64:9: error: not enough arguments
map1.go:65:9: error: not enough arguments
map1.go:67:9: error: argument 1 must be a map
method2.go:36:11: error: reference to undefined field or method ‘val’
method2.go:37:11: error: reference to undefined field or method ‘val’
method2.go:41:12: error: method requires pointer (use ‘(*T).g’)
syntax/chan1.go:13:19: error: send statement used as value; use select for non-blocking send
syntax/chan1.go:17:11: error: send statement used as value; use select for non-blocking send
Change-Id: I98047b60a376e3d2788836300f7fcac3f2c285cb
Reviewed-on: https://go-review.googlesource.com/c/go/+/273527
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
2020-11-26 13:26:02 -07:00
|
|
|
_ = f.(int) // ERROR "non-interface type|only valid for interface types"
|
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
|
|
|
|
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"
|
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{}
|