Skip to content

Commit 37586e4

Browse files
committed
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 6d4ccf2 commit 37586e4

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

internal/apidiff/apidiff.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"go/constant"
2020
"go/token"
2121
"go/types"
22+
23+
"golang.org/x/tools/internal/aliases"
2224
)
2325

2426
// 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)
206208
// Since these can change without affecting compatibility, we don't want users to
207209
// be distracted by them, so we remove them.
208210
func removeNamesFromSignature(t types.Type) types.Type {
209-
sig, ok := t.(*types.Signature)
211+
sig, ok := aliases.Unalias(t).(*types.Signature)
210212
if !ok {
211213
return t
212214
}

internal/apidiff/compatibility.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import (
88
"fmt"
99
"go/types"
1010
"reflect"
11+
12+
"golang.org/x/tools/internal/aliases"
1113
)
1214

1315
func (d *differ) checkCompatible(otn *types.TypeName, old, new types.Type) {
16+
old = aliases.Unalias(old)
17+
new = aliases.Unalias(new)
1418
switch old := old.(type) {
1519
case *types.Interface:
1620
if new, ok := new.(*types.Interface); ok {
@@ -268,7 +272,7 @@ func (d *differ) checkCompatibleDefined(otn *types.TypeName, old *types.Named, n
268272
return
269273
}
270274
// Interface method sets are checked in checkCompatibleInterface.
271-
if _, ok := old.Underlying().(*types.Interface); ok {
275+
if types.IsInterface(old) {
272276
return
273277
}
274278

@@ -287,7 +291,7 @@ func (d *differ) checkMethodSet(otn *types.TypeName, oldt, newt types.Type, addc
287291
oldMethodSet := exportedMethods(oldt)
288292
newMethodSet := exportedMethods(newt)
289293
msname := otn.Name()
290-
if _, ok := oldt.(*types.Pointer); ok {
294+
if _, ok := aliases.Unalias(oldt).(*types.Pointer); ok {
291295
msname = "*" + msname
292296
}
293297
for name, oldMethod := range oldMethodSet {
@@ -349,9 +353,9 @@ func receiverType(method types.Object) types.Type {
349353
}
350354

351355
func receiverNamedType(method types.Object) *types.Named {
352-
switch t := receiverType(method).(type) {
356+
switch t := aliases.Unalias(receiverType(method)).(type) {
353357
case *types.Pointer:
354-
return t.Elem().(*types.Named)
358+
return aliases.Unalias(t.Elem()).(*types.Named)
355359
case *types.Named:
356360
return t
357361
default:
@@ -360,6 +364,6 @@ func receiverNamedType(method types.Object) *types.Named {
360364
}
361365

362366
func hasPointerReceiver(method types.Object) bool {
363-
_, ok := receiverType(method).(*types.Pointer)
367+
_, ok := aliases.Unalias(receiverType(method)).(*types.Pointer)
364368
return ok
365369
}

internal/apidiff/correspondence.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package apidiff
77
import (
88
"go/types"
99
"sort"
10+
11+
"golang.org/x/tools/internal/aliases"
1012
)
1113

1214
// 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 {
3133
// Compare this to the implementation of go/types.Identical.
3234
func (d *differ) corr(old, new types.Type, p *ifacePair) bool {
3335
// Structure copied from types.Identical.
36+
old = aliases.Unalias(old)
37+
new = aliases.Unalias(new)
3438
switch old := old.(type) {
3539
case *types.Basic:
3640
return types.Identical(old, new)

0 commit comments

Comments
 (0)