Debian Codegraph @

apidiff/correspondence.go

raw symbols SCIP
1 package apidiff
2
3 import (
4 "fmt"
5 "go/types"
6 "sort"
7 )
8
9 // Two types are correspond if they are identical except for defined types,
10 // which must correspond.
11 //
12 // Two defined types correspond if they can be interchanged in the old and new APIs,
13 // possibly after a renaming.
14 //
15 // This is not a pure function. If we come across named types while traversing,
16 // we establish correspondence.
17 func (d *differ) correspond(old, new types.Type) bool {
18 return d.corr(old, new, nil)
19 }
20
21 // corr determines whether old and new correspond. The argument p is a list of
22 // known interface identities, to avoid infinite recursion.
23 //
24 // corr calls itself recursively as much as possible, to establish more
25 // correspondences and so check more of the API. E.g. if the new function has more
26 // parameters than the old, compare all the old ones before returning false.
27 //
28 // Compare this to the implementation of go/types.Identical.
29 func (d *differ) corr(old, new types.Type, p *ifacePair) bool {
30 old = types.Unalias(old)
31 new = types.Unalias(new)
32 // Structure copied from types.Identical.
33 switch old := old.(type) {
34 case *types.Basic:
35 if new, ok := new.(*types.Basic); ok {
36 return old.Kind() == new.Kind()
37 }
38
39 case *types.Array:
40 if new, ok := new.(*types.Array); ok {
41 return d.corr(old.Elem(), new.Elem(), p) && old.Len() == new.Len()
42 }
43
44 case *types.Slice:
45 if new, ok := new.(*types.Slice); ok {
46 return d.corr(old.Elem(), new.Elem(), p)
47 }
48
49 case *types.Map:
50 if new, ok := new.(*types.Map); ok {
51 return d.corr(old.Key(), new.Key(), p) && d.corr(old.Elem(), new.Elem(), p)
52 }
53
54 case *types.Chan:
55 if new, ok := new.(*types.Chan); ok {
56 return d.corr(old.Elem(), new.Elem(), p) && old.Dir() == new.Dir()
57 }
58
59 case *types.Pointer:
60 if new, ok := new.(*types.Pointer); ok {
61 return d.corr(old.Elem(), new.Elem(), p)
62 }
63
64 case *types.Signature:
65 if new, ok := new.(*types.Signature); ok {
66 pe := d.corr(old.Params(), new.Params(), p)
67 re := d.corr(old.Results(), new.Results(), p)
68 return old.Variadic() == new.Variadic() && pe && re
69 }
70
71 case *types.Tuple:
72 if new, ok := new.(*types.Tuple); ok {
73 for i := 0; i < old.Len(); i++ {
74 if i >= new.Len() || !d.corr(old.At(i).Type(), new.At(i).Type(), p) {
75 return false
76 }
77 }
78 return old.Len() == new.Len()
79 }
80
81 case *types.Struct:
82 if new, ok := new.(*types.Struct); ok {
83 for i := 0; i < old.NumFields(); i++ {
84 if i >= new.NumFields() {
85 return false
86 }
87 of := old.Field(i)
88 nf := new.Field(i)
89 if of.Anonymous() != nf.Anonymous() ||
90 old.Tag(i) != new.Tag(i) ||
91 !d.corr(of.Type(), nf.Type(), p) ||
92 !d.corrFieldNames(of, nf) {
93 return false
94 }
95 }
96 return old.NumFields() == new.NumFields()
97 }
98
99 case *types.Interface:
100 if new, ok := new.(*types.Interface); ok {
101 // Deal with circularity. See the comment in types.Identical.
102 q := &ifacePair{old, new, p}
103 for p != nil {
104 if p.identical(q) {
105 return true // same pair was compared before
106 }
107 p = p.prev
108 }
109 oldms := d.sortedMethods(old)
110 newms := d.sortedMethods(new)
111 for i, om := range oldms {
112 if i >= len(newms) {
113 return false
114 }
115 nm := newms[i]
116 if d.methodID(om) != d.methodID(nm) || !d.corr(om.Type(), nm.Type(), q) {
117 return false
118 }
119 }
120 return old.NumMethods() == new.NumMethods()
121 }
122
123 case *types.Named:
124 return d.establishCorrespondence(old, new)
125
126 case *types.TypeParam:
127 if new, ok := new.(*types.TypeParam); ok {
128 if old.Index() == new.Index() {
129 return true
130 }
131 }
132
133 default:
134 panic(fmt.Sprintf("unknown type kind %T", old))
135 }
136 return false
137 }
138
139 // Compare old and new field names. We are determining correspondence across packages,
140 // so just compare names, not packages. For an unexported, embedded field of named
141 // type (non-named embedded fields are possible with aliases), we check that the type
142 // names correspond. We check the types for correspondence before this is called, so
143 // we've established correspondence.
144 func (d *differ) corrFieldNames(of, nf *types.Var) bool {
145 if of.Anonymous() && nf.Anonymous() && !of.Exported() && !nf.Exported() {
146 if on, ok := of.Type().(*types.Named); ok {
147 nn := nf.Type().(*types.Named)
148 return d.establishCorrespondence(on, nn)
149 }
150 }
151 return of.Name() == nf.Name()
152 }
153
154 // establishCorrespondence records and validates a correspondence between
155 // old and new.
156 //
157 // If this is the first type corresponding to old, it checks that the type
158 // declaration is compatible with old and records its correspondence.
159 // Otherwise, it checks that new is equivalent to the previously recorded
160 // type corresponding to old.
161 func (d *differ) establishCorrespondence(old *types.Named, new types.Type) bool {
162 oldname := old.Obj()
163 // If there already is a corresponding new type for old, check that they
164 // are the same.
165 if c := d.correspondMap.At(old); c != nil {
166 return types.Identical(c.(types.Type), new)
167 }
168 // Attempt to establish a correspondence.
169 // Assume the types don't correspond unless they have the same
170 // ID, or are from the old and new packages, respectively.
171 //
172 // This is too conservative. For instance,
173 // [old] type A = q.B; [new] type A q.C
174 // could be OK if in package q, B is an alias for C.
175 // Or, using p as the name of the current old/new packages:
176 // [old] type A = q.B; [new] type A int
177 // could be OK if in q,
178 // [old] type B int; [new] type B = p.A
179 // In this case, p.A and q.B name the same type in both old and new worlds.
180 // Note that this case doesn't imply circular package imports: it's possible
181 // that in the old world, p imports q, but in the new, q imports p.
182 //
183 // However, if we didn't do something here, then we'd incorrectly allow cases
184 // like the first one above in which q.B is not an alias for q.C
185 //
186 // What we should do is check that the old type, in the new world's package
187 // of the same path, doesn't correspond to something other than the new type.
188 // That is a bit hard, because there is no easy way to find a new package
189 // matching an old one.
190 switch new := new.(type) {
191 case *types.Named:
192 newn := new
193 oobj := old.Obj()
194 nobj := newn.Obj()
195 if oobj.Pkg() != d.old || nobj.Pkg() != d.new {
196 // Compare the fully qualified names of the types.
197 //
198 // TODO(jba): when comparing modules, we should only look at the
199 // paths relative to the module path, because the module paths may differ.
200 // See cmd/gorelease/testdata/internalcompat.
201 var opath, npath string
202 if oobj.Pkg() != nil {
203 opath = oobj.Pkg().Path()
204 }
205 if nobj.Pkg() != nil {
206 npath = nobj.Pkg().Path()
207 }
208 return oobj.Name() == nobj.Name() && opath == npath
209 }
210 // Two generic named types correspond if their type parameter lists correspond.
211 // Since one or the other of those lists will be empty, it doesn't hurt
212 // to check both.
213 if isInstantiated(old) {
214 if !isInstantiated(new) {
215 // old is an instantiated type but new is not; they cannot correspond.
216 return false
217 }
218 // Two instantiated types correspond if their origins correspond and
219 // their type argument lists correspond.
220 if !d.correspond(old.Origin(), new.Origin()) {
221 return false
222 }
223 if !d.typeListsCorrespond(old.TypeArgs(), newn.TypeArgs()) {
224 return false
225 }
226 } else {
227 if !d.typeParamListsCorrespond(old.TypeParams(), newn.TypeParams()) {
228 return false
229 }
230 }
231 case *types.Basic:
232 if old.Obj().Pkg() != d.old {
233 // A named type from a package other than old never corresponds to a basic type.
234 return false
235 }
236 default:
237 // Only named and basic types can correspond.
238 return false
239 }
240 // If there is no correspondence, create one.
241 d.correspondMap.Set(old, new)
242 // Check that the corresponding types are compatible.
243 d.checkCompatibleDefined(oldname, old, new)
244 return true
245 }
246
247 func (d *differ) typeListsCorrespond(tl1, tl2 *types.TypeList) bool {
248 if tl1.Len() != tl2.Len() {
249 return false
250 }
251 for i := 0; i < tl1.Len(); i++ {
252 if !d.correspond(tl1.At(i), tl2.At(i)) {
253 return false
254 }
255 }
256 return true
257 }
258
259 // Two list of type parameters correspond if they are the same length, and
260 // the constraints of corresponding type parameters correspond.
261 func (d *differ) typeParamListsCorrespond(tps1, tps2 *types.TypeParamList) bool {
262 if tps1.Len() != tps2.Len() {
263 return false
264 }
265 for i := 0; i < tps1.Len(); i++ {
266 if !d.correspond(tps1.At(i).Constraint(), tps2.At(i).Constraint()) {
267 return false
268 }
269 }
270 return true
271 }
272
273 func (d *differ) sortedMethods(iface *types.Interface) []*types.Func {
274 ms := make([]*types.Func, iface.NumMethods())
275 for i := 0; i < iface.NumMethods(); i++ {
276 ms[i] = iface.Method(i)
277 }
278 sort.Slice(ms, func(i, j int) bool { return d.methodID(ms[i]) < d.methodID(ms[j]) })
279 return ms
280 }
281
282 func (d *differ) methodID(m *types.Func) string {
283 // If the method belongs to one of the two packages being compared, use
284 // just its name even if it's unexported. That lets us treat unexported names
285 // from the old and new packages as equal.
286 if m.Pkg() == d.old || m.Pkg() == d.new {
287 return m.Name()
288 }
289 return m.Id()
290 }
291
292 // Copied from the go/types package:
293
294 // An ifacePair is a node in a stack of interface type pairs compared for identity.
295 type ifacePair struct {
296 x, y *types.Interface
297 prev *ifacePair
298 }
299
300 func (p *ifacePair) identical(q *ifacePair) bool {
301 return p.x == q.x && p.y == q.y || p.x == q.y && p.y == q.x
302 }
303
304 // isInstantiated reports whether t is instantiated from a generic type.
305 func isInstantiated(t *types.Named) bool {
306 return t.Origin() != t
307 }