1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:44:42 -07:00

go.tools/go/types: interfaces may not have blank methods

Fixes golang/go#8050.

LGTM=adonovan
R=adonovan
CC=golang-codereviews
https://golang.org/cl/96520047
This commit is contained in:
Robert Griesemer 2014-05-22 13:10:53 -07:00
parent 2fcf90cfac
commit 2afc128b30
2 changed files with 10 additions and 24 deletions

View File

@ -190,10 +190,11 @@ func (S0) m2(x *S0 /* ERROR "field or method" */ .m2) {}
func (S0) m3() (x S0 /* ERROR "field or method" */ .m3) { return }
func (S0) m4() (x *S0 /* ERROR "field or method" */ .m4) { return }
// interfaces may have at most one blank method
// interfaces may not have any blank methods
type BlankI interface {
_()
_ /* ERROR redeclared */ ()
_ /* ERROR "invalid method name" */ ()
_ /* ERROR "invalid method name" */ (float32) int
m()
}
// non-interface types may have multiple blank methods
@ -203,20 +204,3 @@ func (BlankT) _() {}
func (BlankT) _(int) {}
func (BlankT) _() int { return 0 }
func (BlankT) _(int) int { return 0}
// no type can ever satisfy an interface with a _ method
func _() {
var i BlankI
var x BlankT
i = x /* ERROR "cannot assign" */
_ = i
}
// assignability of interfaces with blank methods
func _() {
var x1, x1b interface { _() }
var x2 interface { _() }
x1 = x2
_ = x1
x1 = x1b
}

View File

@ -472,6 +472,12 @@ func (check *checker) interfaceType(iface *Interface, ityp *ast.InterfaceType, d
// and we don't care if a constructed AST has more.
name := f.Names[0]
pos := name.Pos()
// spec: "As with all method sets, in an interface type,
// each method must have a unique non-blank name."
if name.Name == "_" {
check.errorf(pos, "invalid method name _")
continue
}
// Don't type-check signature yet - use an
// empty signature now and update it later.
// Since we know the receiver, set it up now
@ -483,10 +489,6 @@ func (check *checker) interfaceType(iface *Interface, ityp *ast.InterfaceType, d
sig := new(Signature)
sig.recv = NewVar(pos, check.pkg, "", recvTyp)
m := NewFunc(pos, check.pkg, name.Name, sig)
// spec: "As with all method sets, in an interface type,
// each method must have a unique name."
// (The spec does not exclude blank _ identifiers for
// interface methods.)
if check.declareInSet(&mset, pos, m) {
iface.methods = append(iface.methods, m)
iface.allMethods = append(iface.allMethods, m)