Debian Codegraph @

apidiff/apidiff.go

raw symbols SCIP
1 // TODO: test swap corresponding types (e.g. u1 <-> u2 and u2 <-> u1)
2 // TODO: test exported alias refers to something in another package -- does correspondence work then?
3 // TODO: CODE COVERAGE
4 // TODO: note that we may miss correspondences because we bail early when we compare a signature (e.g. when lengths differ; we could do up to the shorter)
5 // TODO: if you add an unexported method to an exposed interface, you have to check that
6 // every exposed type that previously implemented the interface still does. Otherwise
7 // an external assignment of the exposed type to the interface type could fail.
8 // TODO: check constant values: large values aren't representable by some types.
9 // TODO: Document all the incompatibilities we don't check for.
10
11 package apidiff
12
13 import (
14 "fmt"
15 "go/constant"
16 "go/token"
17 "go/types"
18 "strings"
19
20 "golang.org/x/tools/go/types/typeutil"
21 )
22
23 // Changes reports on the differences between the APIs of the old and new packages.
24 // It classifies each difference as either compatible or incompatible (breaking.) For
25 // a detailed discussion of what constitutes an incompatible change, see the README.
26 func Changes(old, new *types.Package) Report {
27 return changesInternal(old, new, old.Path(), new.Path())
28 }
29
30 // changesInternal contains the core logic for comparing a single package, shared
31 // between Changes and ModuleChanges. The root package path arguments refer to the
32 // context of this apidiff invocation - when diffing a single package, they will be
33 // that package, but when diffing a whole module, they will be the root path of the
34 // module. This is used to give change messages appropriate context for object names.
35 // The old and new root must be tracked independently, since each side of the diff
36 // operation may be a different path.
37 func changesInternal(old, new *types.Package, oldRootPackagePath, newRootPackagePath string) Report {
38 d := newDiffer(old, new)
39 d.checkPackage(oldRootPackagePath)
40 r := Report{}
41 for _, m := range d.incompatibles.collect(oldRootPackagePath, newRootPackagePath) {
42 r.Changes = append(r.Changes, Change{Message: m, Compatible: false})
43 }
44 for _, m := range d.compatibles.collect(oldRootPackagePath, newRootPackagePath) {
45 r.Changes = append(r.Changes, Change{Message: m, Compatible: true})
46 }
47 return r
48 }
49
50 // ModuleChanges reports on the differences between the APIs of the old and new
51 // modules. It classifies each difference as either compatible or incompatible
52 // (breaking). This includes the addition and removal of entire packages. For a
53 // detailed discussion of what constitutes an incompatible change, see the README.
54 func ModuleChanges(old, new *Module) Report {
55 var r Report
56
57 oldPkgs := make(map[string]*types.Package)
58 for _, p := range old.Packages {
59 oldPkgs[old.relativePath(p)] = p
60 }
61
62 newPkgs := make(map[string]*types.Package)
63 for _, p := range new.Packages {
64 newPkgs[new.relativePath(p)] = p
65 }
66
67 for n, op := range oldPkgs {
68 if np, ok := newPkgs[n]; ok {
69 // shared package, compare surfaces
70 rr := changesInternal(op, np, old.Path, new.Path)
71 r.Changes = append(r.Changes, rr.Changes...)
72 } else {
73 // old package was removed
74 r.Changes = append(r.Changes, packageChange(op, "removed", false))
75 }
76 }
77
78 for n, np := range newPkgs {
79 if _, ok := oldPkgs[n]; !ok {
80 // new package was added
81 r.Changes = append(r.Changes, packageChange(np, "added", true))
82 }
83 }
84
85 return r
86 }
87
88 func packageChange(p *types.Package, change string, compatible bool) Change {
89 return Change{
90 Message: fmt.Sprintf("package %s: %s", p.Path(), change),
91 Compatible: compatible,
92 }
93 }
94
95 // Module is a convenience type for representing a Go module with a path and a
96 // slice of Packages contained within.
97 type Module struct {
98 Path string
99 Packages []*types.Package
100 }
101
102 // relativePath computes the module-relative package path of the given Package.
103 func (m *Module) relativePath(p *types.Package) string {
104 return strings.TrimPrefix(p.Path(), m.Path)
105 }
106
107 type differ struct {
108 old, new *types.Package
109 // Correspondences between named types.
110 // Even though it is the named types (*types.Named) that correspond, we use
111 // *types.TypeName as a map key because they are canonical.
112 // The values can be either named types or basic types.
113 correspondMap typeutil.Map
114
115 // Messages.
116 incompatibles messageSet
117 compatibles messageSet
118 }
119
120 func newDiffer(old, new *types.Package) *differ {
121 return &differ{
122 old: old,
123 new: new,
124 incompatibles: messageSet{},
125 compatibles: messageSet{},
126 }
127 }
128
129 func (d *differ) incompatible(obj objectWithSide, part, format string, args ...interface{}) {
130 addMessage(d.incompatibles, obj, part, format, args)
131 }
132
133 func (d *differ) compatible(obj objectWithSide, part, format string, args ...interface{}) {
134 addMessage(d.compatibles, obj, part, format, args)
135 }
136
137 func addMessage(ms messageSet, obj objectWithSide, part, format string, args []interface{}) {
138 ms.add(obj, part, fmt.Sprintf(format, args...))
139 }
140
141 func (d *differ) checkPackage(oldRootPackagePath string) {
142 // Determine what has changed between old and new.
143
144 // First, establish correspondences between types with the same name, before
145 // looking at aliases. This will avoid confusing messages like "T: changed
146 // from T to T", which can happen if a correspondence between an alias
147 // and a named type is established first.
148 // See testdata/order.go.
149 // Example of what this loop looks for:
150 // type A = B // old
151 // type B ... // new
152 //
153 // We want to ensure that old B and new B correspond.
154 //
155 // This loop is unnecessary for correctness; skipping symbols will not introduce bugs.
156 for _, name := range d.old.Scope().Names() {
157 oldobj := d.old.Scope().Lookup(name)
158 if tn, ok := oldobj.(*types.TypeName); ok {
159 if oldn, ok := tn.Type().(*types.Named); ok {
160 if !oldn.Obj().Exported() {
161 continue
162 }
163 // Skip aliases to instantiated generic types. They end up getting
164 // compared to the origin type, which will fail.
165 // For example, we don't want to try to make A[int] correspond with
166 // A[T any].
167 if tn.IsAlias() && isInstantiated(oldn) {
168 continue
169 }
170 // Does new have a named type of the same name? Look up using
171 // the old named type's name, oldn.Obj().Name(), not the
172 // TypeName tn, which may be an alias.
173 newobj := d.new.Scope().Lookup(oldn.Obj().Name())
174 if newobj != nil {
175 d.checkObjects(oldobj, newobj)
176 }
177 }
178 }
179 }
180
181 // Next, look at all exported symbols in the old world and compare them
182 // with the same-named symbols in the new world.
183 for _, name := range d.old.Scope().Names() {
184 oldobj := d.old.Scope().Lookup(name)
185 if !oldobj.Exported() {
186 continue
187 }
188 newobj := d.new.Scope().Lookup(name)
189 if newobj == nil {
190 d.incompatible(objectWithSide{oldobj, false}, "", "removed")
191 continue
192 }
193 d.checkObjects(oldobj, newobj)
194 }
195
196 // Now look at what has been added in the new package.
197 for _, name := range d.new.Scope().Names() {
198 newobj := d.new.Scope().Lookup(name)
199 if newobj.Exported() && d.old.Scope().Lookup(name) == nil {
200 d.compatible(objectWithSide{newobj, true}, "", "added")
201 }
202 }
203
204 // Whole-package satisfaction.
205 // For every old exposed interface oIface and its corresponding new interface nIface...
206 d.correspondMap.Iterate(func(k1 types.Type, v1 any) {
207 ot1 := k1.(*types.Named)
208 otn1 := ot1.Obj()
209 nt1 := v1.(types.Type)
210 oIface, ok := otn1.Type().Underlying().(*types.Interface)
211 if !ok {
212 return
213 }
214 nIface, ok := nt1.Underlying().(*types.Interface)
215 if !ok {
216 // If nt1 isn't an interface but otn1 is, then that's an incompatibility that
217 // we've already noticed, so there's no need to do anything here.
218 return
219 }
220 // For every old type that implements oIface, its corresponding new type must implement
221 // nIface.
222 d.correspondMap.Iterate(func(k2 types.Type, v2 any) {
223 ot2 := k2.(*types.Named)
224 otn2 := ot2.Obj()
225 nt2 := v2.(types.Type)
226 if otn1 == otn2 {
227 return
228 }
229 if types.Implements(otn2.Type(), oIface) && !types.Implements(nt2, nIface) {
230 // TODO(jba): the type name is not sufficient information here; we need the type args
231 // if this is an instantiated generic type.
232 d.incompatible(objectWithSide{otn2, false}, "", "no longer implements %s", objectString(otn1, oldRootPackagePath))
233 }
234 })
235 })
236 }
237
238 func (d *differ) checkObjects(old, new types.Object) {
239 switch old := old.(type) {
240 case *types.Const:
241 if new, ok := new.(*types.Const); ok {
242 d.constChanges(old, new)
243 return
244 }
245 case *types.Var:
246 if new, ok := new.(*types.Var); ok {
247 d.checkCorrespondence(objectWithSide{old, false}, "", old.Type(), new.Type())
248 return
249 }
250 case *types.Func:
251 switch new := new.(type) {
252 case *types.Func:
253 d.checkCorrespondence(objectWithSide{old, false}, "", old.Type(), new.Type())
254 return
255 case *types.Var:
256 d.compatible(objectWithSide{old, false}, "", "changed from func to var")
257 d.checkCorrespondence(objectWithSide{old, false}, "", old.Type(), new.Type())
258 return
259
260 }
261 case *types.TypeName:
262 if new, ok := new.(*types.TypeName); ok {
263 d.checkCorrespondence(objectWithSide{old, false}, "", old.Type(), new.Type())
264 return
265 }
266 default:
267 panic("unexpected obj type")
268 }
269 // Here if kind of type changed.
270 d.incompatible(objectWithSide{old, false}, "", "changed from %s to %s",
271 objectKindString(old), objectKindString(new))
272 }
273
274 // Compare two constants.
275 func (d *differ) constChanges(old, new *types.Const) {
276 ot := old.Type()
277 nt := new.Type()
278 // Check for change of type.
279 if !d.correspond(ot, nt) {
280 d.typeChanged(objectWithSide{old, false}, "", ot, nt)
281 return
282 }
283 // Check for change of value.
284 // We know the types are the same, so constant.Compare shouldn't panic.
285 if !constant.Compare(old.Val(), token.EQL, new.Val()) {
286 d.incompatible(objectWithSide{old, false}, "", "value changed from %s to %s", old.Val(), new.Val())
287 }
288 }
289
290 func objectKindString(obj types.Object) string {
291 switch obj.(type) {
292 case *types.Const:
293 return "const"
294 case *types.Var:
295 return "var"
296 case *types.Func:
297 return "func"
298 case *types.TypeName:
299 return "type"
300 default:
301 return "???"
302 }
303 }
304
305 func (d *differ) checkCorrespondence(obj objectWithSide, part string, old, new types.Type) {
306 if !d.correspond(old, new) {
307 d.typeChanged(obj, part, old, new)
308 }
309 }
310
311 func (d *differ) typeChanged(obj objectWithSide, part string, old, new types.Type) {
312 old = types.Unalias(old)
313 new = types.Unalias(new)
314 old = removeNamesFromSignature(old)
315 new = removeNamesFromSignature(new)
316 olds := types.TypeString(old, types.RelativeTo(d.old))
317 news := types.TypeString(new, types.RelativeTo(d.new))
318 d.incompatible(obj, part, "changed from %s to %s", olds, news)
319 }
320
321 // go/types always includes the argument and result names when formatting a signature.
322 // Since these can change without affecting compatibility, we don't want users to
323 // be distracted by them, so we remove them.
324 func removeNamesFromSignature(t types.Type) types.Type {
325 sig, ok := t.(*types.Signature)
326 if !ok {
327 return t
328 }
329
330 dename := func(p *types.Tuple) *types.Tuple {
331 var vars []*types.Var
332 for i := 0; i < p.Len(); i++ {
333 v := p.At(i)
334 vars = append(vars, types.NewVar(v.Pos(), v.Pkg(), "", v.Type()))
335 }
336 return types.NewTuple(vars...)
337 }
338
339 return types.NewSignature(sig.Recv(), dename(sig.Params()), dename(sig.Results()), sig.Variadic())
340 }