Debian Codegraph @

unused/implements.go

raw
1 package unused
2
3 import (
4 "go/types"
5 )
6
7 // lookupMethod returns the index of and method with matching package and name, or (-1, nil).
8 func lookupMethod(T *types.Interface, pkg *types.Package, name string) (int, *types.Func) {
9 if name != "_" {
10 for i := 0; i < T.NumMethods(); i++ {
11 m := T.Method(i)
12 if sameId(m, pkg, name) {
13 return i, m
14 }
15 }
16 }
17 return -1, nil
18 }
19
20 func sameId(obj types.Object, pkg *types.Package, name string) bool {
21 // spec:
22 // "Two identifiers are different if they are spelled differently,
23 // or if they appear in different packages and are not exported.
24 // Otherwise, they are the same."
25 if name != obj.Name() {
26 return false
27 }
28 // obj.Name == name
29 if obj.Exported() {
30 return true
31 }
32 // not exported, so packages must be the same (pkg == nil for
33 // fields in Universe scope; this can only happen for types
34 // introduced via Eval)
35 if pkg == nil || obj.Pkg() == nil {
36 return pkg == obj.Pkg()
37 }
38 // pkg != nil && obj.pkg != nil
39 return pkg.Path() == obj.Pkg().Path()
40 }
41
42 func implements(V types.Type, T *types.Interface, msV *types.MethodSet) ([]*types.Selection, bool) {
43 // fast path for common case
44 if T.Empty() {
45 return nil, true
46 }
47
48 if ityp, _ := V.Underlying().(*types.Interface); ityp != nil {
49 // TODO(dh): is this code reachable?
50 for m := range T.Methods() {
51 _, obj := lookupMethod(ityp, m.Pkg(), m.Name())
52 switch {
53 case obj == nil:
54 return nil, false
55 case !types.Identical(obj.Type(), m.Type()):
56 return nil, false
57 }
58 }
59 return nil, true
60 }
61
62 // A concrete type implements T if it implements all methods of T.
63 var sels []*types.Selection
64 var c methodsChecker
65 for m := range T.Methods() {
66 sel := msV.Lookup(m.Pkg(), m.Name())
67 if sel == nil {
68 return nil, false
69 }
70
71 f, _ := sel.Obj().(*types.Func)
72 if f == nil {
73 return nil, false
74 }
75
76 if !c.methodIsCompatible(f, m) {
77 return nil, false
78 }
79
80 sels = append(sels, sel)
81 }
82 return sels, true
83 }
84
85 type methodsChecker struct {
86 typeParams map[*types.TypeParam]types.Type
87 }
88
89 // Currently, this doesn't support methods like `foo(x []T)`.
90 func (c *methodsChecker) methodIsCompatible(implFunc *types.Func, interfaceFunc *types.Func) bool {
91 if types.Identical(implFunc.Type(), interfaceFunc.Type()) {
92 return true
93 }
94 implSig, implOk := implFunc.Type().(*types.Signature)
95 interfaceSig, interfaceOk := interfaceFunc.Type().(*types.Signature)
96 if !implOk || !interfaceOk {
97 // probably not reachable. handle conservatively.
98 return false
99 }
100
101 if !c.typesAreCompatible(implSig.Params(), interfaceSig.Params()) {
102 return false
103 }
104
105 if !c.typesAreCompatible(implSig.Results(), interfaceSig.Results()) {
106 return false
107 }
108
109 return true
110 }
111
112 func (c *methodsChecker) typesAreCompatible(implTypes, interfaceTypes *types.Tuple) bool {
113 if implTypes.Len() != interfaceTypes.Len() {
114 return false
115 }
116 for i := 0; i < implTypes.Len(); i++ {
117 if !c.typeIsCompatible(implTypes.At(i).Type(), interfaceTypes.At(i).Type()) {
118 return false
119 }
120 }
121 return true
122 }
123
124 func (c *methodsChecker) typeIsCompatible(implType, interfaceType types.Type) bool {
125 if types.Identical(implType, interfaceType) {
126 return true
127 }
128 // We only support trivial use of type parameters. This isn't fully compatible with compiler type checking yet.
129 tp, ok := interfaceType.(*types.TypeParam)
130 if !ok {
131 return false
132 }
133 if c.typeParams == nil {
134 c.typeParams = make(map[*types.TypeParam]types.Type)
135 }
136 if c.typeParams[tp] == nil {
137 if !satisfiesConstraint(implType, tp) {
138 return false
139 }
140 c.typeParams[tp] = implType
141 return true
142 }
143 return types.Identical(c.typeParams[tp], implType)
144 }
145
146 func satisfiesConstraint(t types.Type, tp *types.TypeParam) bool {
147 bound := tp.Constraint().Underlying().(*types.Interface)
148 return types.Satisfies(t, bound)
149 }