1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // TODO: test swap corresponding types (e.g. u1 <-> u2 and u2 <-> u1)
6 // TODO: test exported alias refers to something in another package -- does correspondence work then?
8 // 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)
9 // TODO: if you add an unexported method to an exposed interface, you have to check that
10 // every exposed type that previously implemented the interface still does. Otherwise
11 // an external assignment of the exposed type to the interface type could fail.
12 // TODO: check constant values: large values aren't representable by some types.
13 // TODO: Document all the incompatibilities we don't check for.
24 // Changes reports on the differences between the APIs of the old and new packages.
25 // It classifies each difference as either compatible or incompatible (breaking.) For
26 // a detailed discussion of what constitutes an incompatible change, see the package
28 func Changes(old, new *types.Package) Report {
29 d := newDiffer(old, new)
32 for _, m := range d.incompatibles.collect() {
33 r.Changes = append(r.Changes, Change{Message: m, Compatible: false})
35 for _, m := range d.compatibles.collect() {
36 r.Changes = append(r.Changes, Change{Message: m, Compatible: true})
42 old, new *types.Package
43 // Correspondences between named types.
44 // Even though it is the named types (*types.Named) that correspond, we use
45 // *types.TypeName as a map key because they are canonical.
46 // The values can be either named types or basic types.
47 correspondMap map[*types.TypeName]types.Type
50 incompatibles messageSet
51 compatibles messageSet
54 func newDiffer(old, new *types.Package) *differ {
58 correspondMap: map[*types.TypeName]types.Type{},
59 incompatibles: messageSet{},
60 compatibles: messageSet{},
64 func (d *differ) incompatible(obj types.Object, part, format string, args ...interface{}) {
65 addMessage(d.incompatibles, obj, part, format, args)
68 func (d *differ) compatible(obj types.Object, part, format string, args ...interface{}) {
69 addMessage(d.compatibles, obj, part, format, args)
72 func addMessage(ms messageSet, obj types.Object, part, format string, args []interface{}) {
73 ms.add(obj, part, fmt.Sprintf(format, args...))
76 func (d *differ) checkPackage() {
78 for _, name := range d.old.Scope().Names() {
79 oldobj := d.old.Scope().Lookup(name)
80 if !oldobj.Exported() {
83 newobj := d.new.Scope().Lookup(name)
85 d.incompatible(oldobj, "", "removed")
88 d.checkObjects(oldobj, newobj)
91 for _, name := range d.new.Scope().Names() {
92 newobj := d.new.Scope().Lookup(name)
93 if newobj.Exported() && d.old.Scope().Lookup(name) == nil {
94 d.compatible(newobj, "", "added")
98 // Whole-package satisfaction.
99 // For every old exposed interface oIface and its corresponding new interface nIface...
100 for otn1, nt1 := range d.correspondMap {
101 oIface, ok := otn1.Type().Underlying().(*types.Interface)
105 nIface, ok := nt1.Underlying().(*types.Interface)
107 // If nt1 isn't an interface but otn1 is, then that's an incompatibility that
108 // we've already noticed, so there's no need to do anything here.
111 // For every old type that implements oIface, its corresponding new type must implement
113 for otn2, nt2 := range d.correspondMap {
117 if types.Implements(otn2.Type(), oIface) && !types.Implements(nt2, nIface) {
118 d.incompatible(otn2, "", "no longer implements %s", objectString(otn1))
124 func (d *differ) checkObjects(old, new types.Object) {
125 switch old := old.(type) {
127 if new, ok := new.(*types.Const); ok {
128 d.constChanges(old, new)
132 if new, ok := new.(*types.Var); ok {
133 d.checkCorrespondence(old, "", old.Type(), new.Type())
137 switch new := new.(type) {
139 d.checkCorrespondence(old, "", old.Type(), new.Type())
142 d.compatible(old, "", "changed from func to var")
143 d.checkCorrespondence(old, "", old.Type(), new.Type())
147 case *types.TypeName:
148 if new, ok := new.(*types.TypeName); ok {
149 d.checkCorrespondence(old, "", old.Type(), new.Type())
153 panic("unexpected obj type")
155 // Here if kind of type changed.
156 d.incompatible(old, "", "changed from %s to %s",
157 objectKindString(old), objectKindString(new))
160 // Compare two constants.
161 func (d *differ) constChanges(old, new *types.Const) {
164 // Check for change of type.
165 if !d.correspond(ot, nt) {
166 d.typeChanged(old, "", ot, nt)
169 // Check for change of value.
170 // We know the types are the same, so constant.Compare shouldn't panic.
171 if !constant.Compare(old.Val(), token.EQL, new.Val()) {
172 d.incompatible(old, "", "value changed from %s to %s", old.Val(), new.Val())
176 func objectKindString(obj types.Object) string {
184 case *types.TypeName:
191 func (d *differ) checkCorrespondence(obj types.Object, part string, old, new types.Type) {
192 if !d.correspond(old, new) {
193 d.typeChanged(obj, part, old, new)
197 func (d *differ) typeChanged(obj types.Object, part string, old, new types.Type) {
198 old = removeNamesFromSignature(old)
199 new = removeNamesFromSignature(new)
200 olds := types.TypeString(old, types.RelativeTo(d.old))
201 news := types.TypeString(new, types.RelativeTo(d.new))
202 d.incompatible(obj, part, "changed from %s to %s", olds, news)
205 // go/types always includes the argument and result names when formatting a signature.
206 // Since these can change without affecting compatibility, we don't want users to
207 // be distracted by them, so we remove them.
208 func removeNamesFromSignature(t types.Type) types.Type {
209 sig, ok := t.(*types.Signature)
214 dename := func(p *types.Tuple) *types.Tuple {
215 var vars []*types.Var
216 for i := 0; i < p.Len(); i++ {
218 vars = append(vars, types.NewVar(v.Pos(), v.Pkg(), "", v.Type()))
220 return types.NewTuple(vars...)
223 return types.NewSignature(sig.Recv(), dename(sig.Params()), dename(sig.Results()), sig.Variadic())