go.tools/go/types: don't use nil fields and methods

The change for fields is in anticipation of fields
becoming a *Scope.

Pointed out by adonovan.

R=adonovan
CC=golang-dev
https://golang.org/cl/9874049
This commit is contained in:
Robert Griesemer 2013-05-31 12:02:33 -07:00
parent e5f49b1c9f
commit 710a117e33
2 changed files with 28 additions and 22 deletions

View File

@ -258,6 +258,7 @@ func writeType(buf *bytes.Buffer, typ Type) {
case *Struct:
buf.WriteString("struct{")
if t.fields != nil {
for i, f := range t.fields {
if i > 0 {
buf.WriteString("; ")
@ -271,6 +272,7 @@ func writeType(buf *bytes.Buffer, typ Type) {
fmt.Fprintf(buf, " %q", tag)
}
}
}
buf.WriteByte('}')
case *Pointer:
@ -289,6 +291,7 @@ func writeType(buf *bytes.Buffer, typ Type) {
case *Interface:
buf.WriteString("interface{")
if t.methods != nil {
for i, obj := range t.methods.entries {
if i > 0 {
buf.WriteString("; ")
@ -297,6 +300,7 @@ func writeType(buf *bytes.Buffer, typ Type) {
buf.WriteString(m.name)
writeSignature(buf, m.typ.(*Signature))
}
}
buf.WriteByte('}')
case *Map:

View File

@ -152,6 +152,7 @@ func (check *checker) collectMethods(scope *Scope, list *ast.FieldList) *Scope {
// embedded interface
utyp := typ.Underlying()
if ityp, ok := utyp.(*Interface); ok {
if ityp.methods != nil {
for _, obj := range ityp.methods.entries {
if alt := methods.Insert(obj); alt != nil {
check.errorf(list.Pos(), "multiple methods named %s", obj.Name())
@ -159,6 +160,7 @@ func (check *checker) collectMethods(scope *Scope, list *ast.FieldList) *Scope {
}
check.callImplicitObj(f, obj)
}
}
} else if utyp != Typ[Invalid] {
// if utyp is invalid, don't complain (the root cause was reported before)
check.errorf(f.Type.Pos(), "%s is not an interface type", typ)