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,17 +258,19 @@ func writeType(buf *bytes.Buffer, typ Type) {
case *Struct:
buf.WriteString("struct{")
for i, f := range t.fields {
if i > 0 {
buf.WriteString("; ")
}
if !f.IsAnonymous {
buf.WriteString(f.Name)
buf.WriteByte(' ')
}
writeType(buf, f.Type)
if tag := t.Tag(i); tag != "" {
fmt.Fprintf(buf, " %q", tag)
if t.fields != nil {
for i, f := range t.fields {
if i > 0 {
buf.WriteString("; ")
}
if !f.IsAnonymous {
buf.WriteString(f.Name)
buf.WriteByte(' ')
}
writeType(buf, f.Type)
if tag := t.Tag(i); tag != "" {
fmt.Fprintf(buf, " %q", tag)
}
}
}
buf.WriteByte('}')
@ -289,13 +291,15 @@ func writeType(buf *bytes.Buffer, typ Type) {
case *Interface:
buf.WriteString("interface{")
for i, obj := range t.methods.entries {
if i > 0 {
buf.WriteString("; ")
if t.methods != nil {
for i, obj := range t.methods.entries {
if i > 0 {
buf.WriteString("; ")
}
m := obj.(*Func)
buf.WriteString(m.name)
writeSignature(buf, m.typ.(*Signature))
}
m := obj.(*Func)
buf.WriteString(m.name)
writeSignature(buf, m.typ.(*Signature))
}
buf.WriteByte('}')

View File

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