Skip to content

Commit

Permalink
internal/apidiff: audit for types.Alias safety
Browse files Browse the repository at this point in the history
Updates golang/go#65294

Change-Id: I0767c09e277a2225657dcf87e7b41d664c9da1bb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/559935
Reviewed-by: Jonathan Amsterdam <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
adonovan committed Feb 6, 2024
1 parent 6d4ccf2 commit 37586e4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion internal/apidiff/apidiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"go/constant"
"go/token"
"go/types"

"golang.org/x/tools/internal/aliases"
)

// Changes reports on the differences between the APIs of the old and new packages.
Expand Down Expand Up @@ -206,7 +208,7 @@ func (d *differ) typeChanged(obj types.Object, part string, old, new types.Type)
// Since these can change without affecting compatibility, we don't want users to
// be distracted by them, so we remove them.
func removeNamesFromSignature(t types.Type) types.Type {
sig, ok := t.(*types.Signature)
sig, ok := aliases.Unalias(t).(*types.Signature)
if !ok {
return t
}
Expand Down
14 changes: 9 additions & 5 deletions internal/apidiff/compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import (
"fmt"
"go/types"
"reflect"

"golang.org/x/tools/internal/aliases"
)

func (d *differ) checkCompatible(otn *types.TypeName, old, new types.Type) {
old = aliases.Unalias(old)
new = aliases.Unalias(new)
switch old := old.(type) {
case *types.Interface:
if new, ok := new.(*types.Interface); ok {
Expand Down Expand Up @@ -268,7 +272,7 @@ func (d *differ) checkCompatibleDefined(otn *types.TypeName, old *types.Named, n
return
}
// Interface method sets are checked in checkCompatibleInterface.
if _, ok := old.Underlying().(*types.Interface); ok {
if types.IsInterface(old) {
return
}

Expand All @@ -287,7 +291,7 @@ func (d *differ) checkMethodSet(otn *types.TypeName, oldt, newt types.Type, addc
oldMethodSet := exportedMethods(oldt)
newMethodSet := exportedMethods(newt)
msname := otn.Name()
if _, ok := oldt.(*types.Pointer); ok {
if _, ok := aliases.Unalias(oldt).(*types.Pointer); ok {
msname = "*" + msname
}
for name, oldMethod := range oldMethodSet {
Expand Down Expand Up @@ -349,9 +353,9 @@ func receiverType(method types.Object) types.Type {
}

func receiverNamedType(method types.Object) *types.Named {
switch t := receiverType(method).(type) {
switch t := aliases.Unalias(receiverType(method)).(type) {
case *types.Pointer:
return t.Elem().(*types.Named)
return aliases.Unalias(t.Elem()).(*types.Named)
case *types.Named:
return t
default:
Expand All @@ -360,6 +364,6 @@ func receiverNamedType(method types.Object) *types.Named {
}

func hasPointerReceiver(method types.Object) bool {
_, ok := receiverType(method).(*types.Pointer)
_, ok := aliases.Unalias(receiverType(method)).(*types.Pointer)
return ok
}
4 changes: 4 additions & 0 deletions internal/apidiff/correspondence.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package apidiff
import (
"go/types"
"sort"

"golang.org/x/tools/internal/aliases"
)

// Two types are correspond if they are identical except for defined types,
Expand All @@ -31,6 +33,8 @@ func (d *differ) correspond(old, new types.Type) bool {
// Compare this to the implementation of go/types.Identical.
func (d *differ) corr(old, new types.Type, p *ifacePair) bool {
// Structure copied from types.Identical.
old = aliases.Unalias(old)
new = aliases.Unalias(new)
switch old := old.(type) {
case *types.Basic:
return types.Identical(old, new)
Expand Down

0 comments on commit 37586e4

Please sign in to comment.