internal/apidiff: audit for types.Alias safety

Updates golang/go#65294

Change-Id: I0767c09e277a2225657dcf87e7b41d664c9da1bb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/559935
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Alan Donovan 2024-01-31 16:47:20 -05:00
Родитель 6d4ccf2ad6
Коммит 37586e4e30
3 изменённых файлов: 16 добавлений и 6 удалений

Просмотреть файл

@ -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.
@ -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
}

Просмотреть файл

@ -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 {
@ -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
}
@ -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 {
@ -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:
@ -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
}

Просмотреть файл

@ -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,
@ -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)