github.com/golangci/go-tools@v0.0.0-20190318060251-af6baa5dc196/unused/implements.go (about)

     1  package unused
     2  
     3  import "go/types"
     4  
     5  // lookupMethod returns the index of and method with matching package and name, or (-1, nil).
     6  func lookupMethod(T *types.Interface, pkg *types.Package, name string) (int, *types.Func) {
     7  	if name != "_" {
     8  		for i := 0; i < T.NumMethods(); i++ {
     9  			m := T.Method(i)
    10  			if sameId(m, pkg, name) {
    11  				return i, m
    12  			}
    13  		}
    14  	}
    15  	return -1, nil
    16  }
    17  
    18  func sameId(obj types.Object, pkg *types.Package, name string) bool {
    19  	// spec:
    20  	// "Two identifiers are different if they are spelled differently,
    21  	// or if they appear in different packages and are not exported.
    22  	// Otherwise, they are the same."
    23  	if name != obj.Name() {
    24  		return false
    25  	}
    26  	// obj.Name == name
    27  	if obj.Exported() {
    28  		return true
    29  	}
    30  	// not exported, so packages must be the same (pkg == nil for
    31  	// fields in Universe scope; this can only happen for types
    32  	// introduced via Eval)
    33  	if pkg == nil || obj.Pkg() == nil {
    34  		return pkg == obj.Pkg()
    35  	}
    36  	// pkg != nil && obj.pkg != nil
    37  	return pkg.Path() == obj.Pkg().Path()
    38  }
    39  
    40  func (c *Checker) implements(V types.Type, T *types.Interface) bool {
    41  	// fast path for common case
    42  	if T.Empty() {
    43  		return true
    44  	}
    45  
    46  	if ityp, _ := V.Underlying().(*types.Interface); ityp != nil {
    47  		for i := 0; i < T.NumMethods(); i++ {
    48  			m := T.Method(i)
    49  			_, obj := lookupMethod(ityp, m.Pkg(), m.Name())
    50  			switch {
    51  			case obj == nil:
    52  				return false
    53  			case !types.Identical(obj.Type(), m.Type()):
    54  				return false
    55  			}
    56  		}
    57  		return true
    58  	}
    59  
    60  	// A concrete type implements T if it implements all methods of T.
    61  	ms := c.msCache.MethodSet(V)
    62  	for i := 0; i < T.NumMethods(); i++ {
    63  		m := T.Method(i)
    64  		sel := ms.Lookup(m.Pkg(), m.Name())
    65  		if sel == nil {
    66  			return false
    67  		}
    68  
    69  		f, _ := sel.Obj().(*types.Func)
    70  		if f == nil {
    71  			return false
    72  		}
    73  
    74  		if !types.Identical(f.Type(), m.Type()) {
    75  			return false
    76  		}
    77  	}
    78  	return true
    79  }