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>
diff --git a/internal/apidiff/apidiff.go b/internal/apidiff/apidiff.go
index 873ee85..087e112 100644
--- a/internal/apidiff/apidiff.go
+++ b/internal/apidiff/apidiff.go
@@ -19,6 +19,8 @@
 	"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 @@
 // 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
 	}
diff --git a/internal/apidiff/compatibility.go b/internal/apidiff/compatibility.go
index 2e32748..0d2d2b3 100644
--- a/internal/apidiff/compatibility.go
+++ b/internal/apidiff/compatibility.go
@@ -8,9 +8,13 @@
 	"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 @@
 		return
 	}
 	// Interface method sets are checked in checkCompatibleInterface.
-	if _, ok := old.Underlying().(*types.Interface); ok {
+	if types.IsInterface(old) {
 		return
 	}
 
@@ -287,7 +291,7 @@
 	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 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 hasPointerReceiver(method types.Object) bool {
-	_, ok := receiverType(method).(*types.Pointer)
+	_, ok := aliases.Unalias(receiverType(method)).(*types.Pointer)
 	return ok
 }
diff --git a/internal/apidiff/correspondence.go b/internal/apidiff/correspondence.go
index 0d7b4c5..dd2f517 100644
--- a/internal/apidiff/correspondence.go
+++ b/internal/apidiff/correspondence.go
@@ -7,6 +7,8 @@
 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 @@
 // 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)