Skip to content
Permalink
Browse files

go/types: unexport Checker.LookupFieldOrMethod

Implementation changes in go/types for #6977 required that internal
LookupFieldOrMethod calls had access to the current *Checker. In
order to make quick progress, I added a *Checker receiver to the
function LookupFieldOrMethod (thus making it a method), and added
a new function LookupFieldOrMethod. The plan was always to rename
that function (Checker.LookupFieldOrMethod) such that it wouldn't
be exported; with the obvious name being Checker.lookupFieldOrMethod.
But that name was already in use which is why I postponed the rename.
Eventually I forgot to clean it up. This CL fixes that with the
following renames:

Checker.lookupFieldOrMethod => Checker.rawLookupFieldOrMethod
Checker.LookupFieldOrMethod => Checker.lookupFieldOrMethod

Updates #6977.
Fixes #36916.

Change-Id: Icfafd0de9a19841ba5bd87142730fe7323204491
Reviewed-on: https://go-review.googlesource.com/c/go/+/217134
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
griesemer authored and ianlancetaylor committed Jan 31, 2020
1 parent b7689f5 commit 6e592c2b6d3d32f0eb1211a3795e852627c7a086
Showing with 12 additions and 13 deletions.
  1. +0 −1 api/go1.14.txt
  2. +1 −1 src/go/types/builtins.go
  3. +1 −1 src/go/types/call.go
  4. +10 −10 src/go/types/lookup.go
@@ -150,7 +150,6 @@ pkg go/doc, type Example struct, Suffix string
pkg go/doc, type Func struct, Examples []*Example
pkg go/doc, type Package struct, Examples []*Example
pkg go/doc, type Type struct, Examples []*Example
pkg go/types, method (*Checker) LookupFieldOrMethod(Type, bool, *Package, string) (Object, []int, bool)
pkg hash/maphash, func MakeSeed() Seed
pkg hash/maphash, method (*Hash) BlockSize() int
pkg hash/maphash, method (*Hash) Reset()
@@ -559,7 +559,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b

base := derefStructPtr(x.typ)
sel := selx.Sel.Name
obj, index, indirect := check.LookupFieldOrMethod(base, false, check.pkg, sel)
obj, index, indirect := check.lookupFieldOrMethod(base, false, check.pkg, sel)
switch obj.(type) {
case nil:
check.invalidArg(x.pos(), "%s has no single field %s", base, sel)
@@ -370,7 +370,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
goto Error
}

obj, index, indirect = check.LookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel)
obj, index, indirect = check.lookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel)
if obj == nil {
switch {
case index != nil:
@@ -33,19 +33,19 @@ package types
// 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) {
return (*Checker)(nil).LookupFieldOrMethod(T, addressable, pkg, name)
return (*Checker)(nil).lookupFieldOrMethod(T, addressable, pkg, name)
}

// Internal use of Checker.LookupFieldOrMethod: If the obj result is a method
// Internal use of Checker.lookupFieldOrMethod: If the obj result is a method
// associated with a concrete (non-interface) type, the method's signature
// may not be fully set up. Call Checker.objDecl(obj, nil) before accessing
// the method's type.
// TODO(gri) Now that we provide the *Checker, we can probably remove this
// caveat by calling Checker.objDecl from LookupFieldOrMethod. Investigate.
// caveat by calling Checker.objDecl from lookupFieldOrMethod. Investigate.

// LookupFieldOrMethod is like the external version but completes interfaces
// lookupFieldOrMethod is like the external version but completes interfaces
// as necessary.
func (check *Checker) LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
func (check *Checker) lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
// Methods cannot be associated to a named pointer type
// (spec: "The type denoted by T is called the receiver base type;
// it must not be a pointer or interface type and it must be declared
@@ -55,24 +55,24 @@ func (check *Checker) LookupFieldOrMethod(T Type, addressable bool, pkg *Package
// not have found it for T (see also issue 8590).
if t, _ := T.(*Named); t != nil {
if p, _ := t.underlying.(*Pointer); p != nil {
obj, index, indirect = check.lookupFieldOrMethod(p, false, pkg, name)
obj, index, indirect = check.rawLookupFieldOrMethod(p, false, pkg, name)
if _, ok := obj.(*Func); ok {
return nil, nil, false
}
return
}
}

return check.lookupFieldOrMethod(T, addressable, pkg, name)
return check.rawLookupFieldOrMethod(T, addressable, pkg, name)
}

// TODO(gri) The named type consolidation and seen maps below must be
// indexed by unique keys for a given type. Verify that named
// types always have only one representation (even when imported
// indirectly via different packages.)

// lookupFieldOrMethod should only be called by LookupFieldOrMethod and missingMethod.
func (check *Checker) lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
// rawLookupFieldOrMethod should only be called by lookupFieldOrMethod and missingMethod.
func (check *Checker) rawLookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
// WARNING: The code in this function is extremely subtle - do not modify casually!
// This function and NewMethodSet should be kept in sync.

@@ -297,7 +297,7 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method *

// A concrete type implements T if it implements all methods of T.
for _, m := range T.allMethods {
obj, _, _ := check.lookupFieldOrMethod(V, false, m.pkg, m.name)
obj, _, _ := check.rawLookupFieldOrMethod(V, false, m.pkg, m.name)

// we must have a method (not a field of matching function type)
f, _ := obj.(*Func)

0 comments on commit 6e592c2

Please sign in to comment.
You can’t perform that action at this time.