1
0
mirror of https://github.com/golang/go synced 2024-11-18 17:34:51 -07:00

go.tools/go/types: simplify LookupFieldOrMethod

Remove a 2nd lookup in some cases.

LGTM=adonovan
R=adonovan
CC=golang-codereviews
https://golang.org/cl/136190043
This commit is contained in:
Robert Griesemer 2014-09-04 10:13:49 -07:00
parent 710872e5c8
commit de5d818681
2 changed files with 14 additions and 32 deletions

View File

@ -323,7 +323,7 @@ func (init *Initializer) String() string {
// Check type-checks a package and returns the resulting package object, // Check type-checks a package and returns the resulting package object,
// the first error if any, and if info != nil, additional type information. // the first error if any, and if info != nil, additional type information.
// The package is marked as complete if no errors occurred, otherwise it is // The package is marked as complete if no errors occurred, otherwise it is
// incomplete. See Config.Error for controlling behavior in the presense of // incomplete. See Config.Error for controlling behavior in the presence of
// errors. // errors.
// //
// The package is specified by a list of *ast.Files and corresponding // The package is specified by a list of *ast.Files and corresponding

View File

@ -33,42 +33,24 @@ package types
// the method's formal receiver base type, nor was the receiver addressable. // the method's formal receiver base type, nor was the receiver addressable.
// //
func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) { func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
obj, index, indirect = lookupFieldOrMethod(T, addressable, pkg, name) // Methods cannot be associated to a named pointer type
if obj != nil { // (spec: "The type denoted by T is called the receiver base type;
return // it must not be a pointer or interface type and it must be declared
} // in the same package as the method.").
// Thus, if we have a named pointer type, proceed with the underlying
// TODO(gri) The code below is not needed if we are looking for methods only, // pointer type but discard the result if it is a method since we would
// and it can be done always if we look for fields only. Consider // not have found it for T (see also issue 8590).
// providing LookupField and LookupMethod as well.
// If we didn't find anything, we still might have a field p.x as in:
//
// type S struct{ x int }
// func (*S) m() {}
// type P *S
// var p P
//
// which requires that we start the search with the underlying type
// of P (i.e., *S). We cannot do this always because we might find
// methods that don't exist for P but for S (e.g., m). Thus, if the
// result is a method we need to discard it.
// (See also issue 8590).
if t, _ := T.(*Named); t != nil { if t, _ := T.(*Named); t != nil {
u := t.underlying if p, _ := t.underlying.(*Pointer); p != nil {
if _, ok := u.(*Pointer); ok { obj, index, indirect = lookupFieldOrMethod(p, false, pkg, name)
// typ is a named type with an underlying type of the form *T, if _, ok := obj.(*Func); ok {
// start the search with the underlying type *T return nil, nil, false
if obj2, index2, indirect2 := lookupFieldOrMethod(u, false, pkg, name); obj2 != nil {
// only if the result is a field can we keep it
if _, ok := obj2.(*Var); ok {
return obj2, index2, indirect2
}
} }
return
} }
} }
return return lookupFieldOrMethod(T, addressable, pkg, name)
} }
// TODO(gri) The named type consolidation and seen maps below must be // TODO(gri) The named type consolidation and seen maps below must be