Skip to content

Commit

Permalink
Patch apidiff to also capture import path changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxx committed Aug 3, 2022
1 parent a9213ee commit 6126f99
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion apidiff/correspondence.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,40 @@ func (d *differ) establishCorrespondence(old *types.Named, new types.Type) bool
// matching an old one.
if newn, ok := new.(*types.Named); ok {
if old.Obj().Pkg() != d.old || newn.Obj().Pkg() != d.new {
return old.Obj().Id() == newn.Obj().Id()
// check if import paths change.
// if this is not done, this:
// import "a/b/c"
// var Thing c.Type
// changing to this:
// import "x/y/z"
// var Thing z.Type
// is considered "not a change", which is clearly incorrect.
// anything referencing Thing's type (e.g. declaring a variable with the type)
// will fail to compile after that change.
//
// unfortunately this does not go far enough, as the exposed API of
// an exposed third-party type are *also* part of "your" API, so any
// incompatible change they make is something you are forcing on your
// users as a library... so this kind of check needs to be done
// transitively on types exposed by a package, which apidiff as a whole
// does not appear to do at all.
//
// I don't have a fix for that.
// I suspect the whole fundamental approach of this package is flawed, and we'd
// be better off with a transitive sorted API *dumper* that then just gets
// passed through `diff` verbatim, or something.
oldp := old.Obj().Pkg()
newnp := newn.Obj().Pkg()
oldpath, newpath := "", ""
if oldp != nil {
oldpath = oldp.Path()
}
if newnp != nil {
newpath = newnp.Path()
}
return oldpath == newpath && old.Obj().Id() == newn.Obj().Id()
}

// Prior to generics, any two named types could correspond.
// Two named types cannot correspond if their type parameter lists don't match.
if !typeParamListsMatch(old.TypeParams(), newn.TypeParams()) {
Expand Down

0 comments on commit 6126f99

Please sign in to comment.