Skip to content

Commit

Permalink
internal/aliases: remove Alias and Unalias
Browse files Browse the repository at this point in the history
These two declarations can now safely be accessed directly
from go/types.

Also, remove all mention of internal/aliases from gopls/...
We can enable two suppressed tests now that go1.23 is assured.

Updates golang/go#46477

Change-Id: I9ae8536b0d022e3300b285547c18202bed302cf2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/614638
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Commit-Queue: Tim King <taking@google.com>
Reviewed-by: Tim King <taking@google.com>
  • Loading branch information
adonovan committed Sep 20, 2024
1 parent 0b6abe3 commit 1d5e334
Show file tree
Hide file tree
Showing 78 changed files with 168 additions and 321 deletions.
4 changes: 1 addition & 3 deletions cmd/godex/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"go/types"
"io"
"math/big"

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

// TODO(gri) use tabwriter for alignment?
Expand Down Expand Up @@ -58,7 +56,7 @@ func (p *printer) printf(format string, args ...interface{}) {
// denoted by obj is not an interface and has methods. Otherwise it returns
// the zero value.
func methodsFor(obj *types.TypeName) (*types.Named, []*types.Selection) {
named, _ := aliases.Unalias(obj.Type()).(*types.Named)
named, _ := types.Unalias(obj.Type()).(*types.Named)
if named == nil {
// A type name's type can also be the
// exported basic type unsafe.Pointer.
Expand Down
6 changes: 2 additions & 4 deletions cmd/godex/writetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ package main

import (
"go/types"

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

func (p *printer) writeType(this *types.Package, typ types.Type) {
Expand Down Expand Up @@ -177,9 +175,9 @@ func (p *printer) writeTypeInternal(this *types.Package, typ types.Type, visited
p.print(")")
}

case *aliases.Alias:
case *types.Alias:
// TODO(adonovan): display something aliasy.
p.writeTypeInternal(this, aliases.Unalias(t), visited)
p.writeTypeInternal(this, types.Unalias(t), visited)

case *types.Named:
s := "<Named w/o object>"
Expand Down
5 changes: 2 additions & 3 deletions go/analysis/passes/composite/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/typeparams"
)

Expand Down Expand Up @@ -72,7 +71,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
return
}
var structuralTypes []types.Type
switch typ := aliases.Unalias(typ).(type) {
switch typ := types.Unalias(typ).(type) {
case *types.TypeParam:
terms, err := typeparams.StructuralTerms(typ)
if err != nil {
Expand Down Expand Up @@ -146,7 +145,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
// isLocalType reports whether typ belongs to the same package as pass.
// TODO(adonovan): local means "internal to a function"; rename to isSamePackageType.
func isLocalType(pass *analysis.Pass, typ types.Type) bool {
switch x := aliases.Unalias(typ).(type) {
switch x := types.Unalias(typ).(type) {
case *types.Struct:
// struct literals are local types
return true
Expand Down
3 changes: 1 addition & 2 deletions go/analysis/passes/copylock/copylock.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/versions"
)
Expand Down Expand Up @@ -286,7 +285,7 @@ func lockPath(tpkg *types.Package, typ types.Type, seen map[types.Type]bool) typ
}
seen[typ] = true

if tpar, ok := aliases.Unalias(typ).(*types.TypeParam); ok {
if tpar, ok := types.Unalias(typ).(*types.TypeParam); ok {
terms, err := typeparams.StructuralTerms(tpar)
if err != nil {
return nil // invalid type
Expand Down
3 changes: 1 addition & 2 deletions go/analysis/passes/deepequalerrors/deepequalerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/go/types/typeutil"
"golang.org/x/tools/internal/aliases"
)

const Doc = `check for calls of reflect.DeepEqual on error values
Expand Down Expand Up @@ -102,7 +101,7 @@ func containsError(typ types.Type) bool {
return true
}
}
case *types.Named, *aliases.Alias:
case *types.Named, *types.Alias:
return check(t.Underlying())

// We list the remaining valid type kinds for completeness.
Expand Down
3 changes: 1 addition & 2 deletions go/analysis/passes/httpresponse/httpresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/typesinternal"
)

Expand Down Expand Up @@ -137,7 +136,7 @@ func isHTTPFuncOrMethodOnClient(info *types.Info, expr *ast.CallExpr) bool {
if analysisutil.IsNamedType(typ, "net/http", "Client") {
return true // method on http.Client.
}
ptr, ok := aliases.Unalias(typ).(*types.Pointer)
ptr, ok := types.Unalias(typ).(*types.Pointer)
return ok && analysisutil.IsNamedType(ptr.Elem(), "net/http", "Client") // method on *http.Client.
}

Expand Down
3 changes: 1 addition & 2 deletions go/analysis/passes/internal/analysisutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"os"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/analysisinternal"
)

Expand Down Expand Up @@ -121,7 +120,7 @@ func Imports(pkg *types.Package, path string) bool {
// This function avoids allocating the concatenation of "pkg.Name",
// which is important for the performance of syntax matching.
func IsNamedType(t types.Type, pkgPath string, names ...string) bool {
n, ok := aliases.Unalias(t).(*types.Named)
n, ok := types.Unalias(t).(*types.Named)
if !ok {
return false
}
Expand Down
3 changes: 1 addition & 2 deletions go/analysis/passes/nilness/nilness.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"golang.org/x/tools/go/analysis/passes/buildssa"
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/typeparams"
)

Expand Down Expand Up @@ -300,7 +299,7 @@ func nilnessOf(stack []fact, v ssa.Value) nilness {
}
case *ssa.MakeInterface:
// A MakeInterface is non-nil unless its operand is a type parameter.
tparam, ok := aliases.Unalias(v.X.Type()).(*types.TypeParam)
tparam, ok := types.Unalias(v.X.Type()).(*types.TypeParam)
if !ok {
return isnonnil
}
Expand Down
5 changes: 2 additions & 3 deletions go/analysis/passes/printf/printf.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/go/types/typeutil"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/typeparams"
)

Expand Down Expand Up @@ -161,7 +160,7 @@ func maybePrintfWrapper(info *types.Info, decl ast.Decl) *printfWrapper {

// Check final parameter is "args ...interface{}".
args := params.At(nparams - 1)
iface, ok := aliases.Unalias(args.Type().(*types.Slice).Elem()).(*types.Interface)
iface, ok := types.Unalias(args.Type().(*types.Slice).Elem()).(*types.Interface)
if !ok || !iface.Empty() {
return nil
}
Expand Down Expand Up @@ -1013,7 +1012,7 @@ func checkPrint(pass *analysis.Pass, call *ast.CallExpr, fn *types.Func) {

typ := params.At(firstArg).Type()
typ = typ.(*types.Slice).Elem()
it, ok := aliases.Unalias(typ).(*types.Interface)
it, ok := types.Unalias(typ).(*types.Interface)
if !ok || !it.Empty() {
// Skip variadic functions accepting non-interface{} args.
return
Expand Down
5 changes: 2 additions & 3 deletions go/analysis/passes/printf/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"go/types"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/typeparams"
)

Expand Down Expand Up @@ -73,7 +72,7 @@ func (m *argMatcher) match(typ types.Type, topLevel bool) bool {
return true
}

if typ, _ := aliases.Unalias(typ).(*types.TypeParam); typ != nil {
if typ, _ := types.Unalias(typ).(*types.TypeParam); typ != nil {
// Avoid infinite recursion through type parameters.
if m.seen[typ] {
return true
Expand Down Expand Up @@ -276,7 +275,7 @@ func (m *argMatcher) match(typ types.Type, topLevel bool) bool {
}

func isConvertibleToString(typ types.Type) bool {
if bt, ok := aliases.Unalias(typ).(*types.Basic); ok && bt.Kind() == types.UntypedNil {
if bt, ok := types.Unalias(typ).(*types.Basic); ok && bt.Kind() == types.UntypedNil {
// We explicitly don't want untyped nil, which is
// convertible to both of the interfaces below, as it
// would just panic anyway.
Expand Down
3 changes: 1 addition & 2 deletions go/analysis/passes/shift/shift.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/typeparams"
)

Expand Down Expand Up @@ -100,7 +99,7 @@ func checkLongShift(pass *analysis.Pass, node ast.Node, x, y ast.Expr) {
return
}
var structuralTypes []types.Type
switch t := aliases.Unalias(t).(type) {
switch t := types.Unalias(t).(type) {
case *types.TypeParam:
terms, err := typeparams.StructuralTerms(t)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions go/analysis/passes/stringintconv/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/analysisinternal"
"golang.org/x/tools/internal/typeparams"
)
Expand Down Expand Up @@ -248,7 +247,7 @@ func run(pass *analysis.Pass) (interface{}, error) {

func structuralTypes(t types.Type) ([]types.Type, error) {
var structuralTypes []types.Type
if tp, ok := aliases.Unalias(t).(*types.TypeParam); ok {
if tp, ok := types.Unalias(t).(*types.TypeParam); ok {
terms, err := typeparams.StructuralTerms(tp)
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions go/analysis/passes/testinggoroutine/testinggoroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/go/types/typeutil"
"golang.org/x/tools/internal/aliases"
)

//go:embed doc.go
Expand Down Expand Up @@ -270,7 +269,7 @@ func forbiddenMethod(info *types.Info, call *ast.CallExpr) (*types.Var, *types.S
func formatMethod(sel *types.Selection, fn *types.Func) string {
var ptr string
rtype := sel.Recv()
if p, ok := aliases.Unalias(rtype).(*types.Pointer); ok {
if p, ok := types.Unalias(rtype).(*types.Pointer); ok {
ptr = "*"
rtype = p.Elem()
}
Expand Down
3 changes: 1 addition & 2 deletions go/analysis/passes/unsafeptr/unsafeptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/internal/aliases"
)

//go:embed doc.go
Expand Down Expand Up @@ -88,7 +87,7 @@ func isSafeUintptr(info *types.Info, x ast.Expr) bool {
// by the time we get to the conversion at the end.
// For now approximate by saying that *Header is okay
// but Header is not.
pt, ok := aliases.Unalias(info.Types[x.X].Type).(*types.Pointer)
pt, ok := types.Unalias(info.Types[x.X].Type).(*types.Pointer)
if ok && isReflectHeader(pt.Elem()) {
return true
}
Expand Down
3 changes: 1 addition & 2 deletions go/analysis/passes/unusedwrite/unusedwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"golang.org/x/tools/go/analysis/passes/buildssa"
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/typeparams"
)

Expand Down Expand Up @@ -142,7 +141,7 @@ func hasStructOrArrayType(v ssa.Value) bool {
// func (t T) f() { ...}
// the receiver object is of type *T:
// t0 = local T (t) *T
if tp, ok := aliases.Unalias(alloc.Type()).(*types.Pointer); ok {
if tp, ok := types.Unalias(alloc.Type()).(*types.Pointer); ok {
return isStructOrArray(tp.Elem())
}
return false
Expand Down
11 changes: 5 additions & 6 deletions go/callgraph/rta/rta.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import (
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/types/typeutil"
"golang.org/x/tools/internal/aliases"
)

// A Result holds the results of Rapid Type Analysis, which includes the
Expand Down Expand Up @@ -374,7 +373,7 @@ func (r *rta) interfaces(C types.Type) []*types.Interface {
// and update the 'implements' relation.
r.interfaceTypes.Iterate(func(I types.Type, v interface{}) {
iinfo := v.(*interfaceTypeInfo)
if I := aliases.Unalias(I).(*types.Interface); implements(cinfo, iinfo) {
if I := types.Unalias(I).(*types.Interface); implements(cinfo, iinfo) {
iinfo.implementations = append(iinfo.implementations, C)
cinfo.implements = append(cinfo.implements, I)
}
Expand Down Expand Up @@ -417,7 +416,7 @@ func (r *rta) implementations(I *types.Interface) []types.Type {
// Adapted from needMethods in go/ssa/builder.go
func (r *rta) addRuntimeType(T types.Type, skip bool) {
// Never record aliases.
T = aliases.Unalias(T)
T = types.Unalias(T)

if prev, ok := r.result.RuntimeTypes.At(T).(bool); ok {
if skip && !prev {
Expand Down Expand Up @@ -456,11 +455,11 @@ func (r *rta) addRuntimeType(T types.Type, skip bool) {
// Each package maintains its own set of types it has visited.

var n *types.Named
switch T := aliases.Unalias(T).(type) {
switch T := types.Unalias(T).(type) {
case *types.Named:
n = T
case *types.Pointer:
n, _ = aliases.Unalias(T.Elem()).(*types.Named)
n, _ = types.Unalias(T.Elem()).(*types.Named)
}
if n != nil {
owner := n.Obj().Pkg()
Expand All @@ -479,7 +478,7 @@ func (r *rta) addRuntimeType(T types.Type, skip bool) {
}

switch t := T.(type) {
case *aliases.Alias:
case *types.Alias:
panic("unreachable")

case *types.Basic:
Expand Down
3 changes: 1 addition & 2 deletions go/callgraph/rta/rta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/testenv"
"golang.org/x/tools/internal/testfiles"
"golang.org/x/tools/txtar"
Expand Down Expand Up @@ -257,7 +256,7 @@ func check(t *testing.T, f *ast.File, pkg *ssa.Package, res *rta.Result) {
got := make(stringset)
res.RuntimeTypes.Iterate(func(key types.Type, value interface{}) {
if !value.(bool) { // accessible to reflection
typ := types.TypeString(aliases.Unalias(key), types.RelativeTo(pkg.Pkg))
typ := types.TypeString(types.Unalias(key), types.RelativeTo(pkg.Pkg))
got[typ] = true
}
})
Expand Down
5 changes: 2 additions & 3 deletions go/callgraph/vta/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/types/typeutil"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/typeparams"
)

Expand Down Expand Up @@ -676,7 +675,7 @@ func (b *builder) multiconvert(c *ssa.MultiConvert) {
// This is a adaptation of x/exp/typeparams.NormalTerms which x/tools cannot depend on.
var terms []*types.Term
var err error
switch typ := aliases.Unalias(typ).(type) {
switch typ := types.Unalias(typ).(type) {
case *types.TypeParam:
terms, err = typeparams.StructuralTerms(typ)
case *types.Union:
Expand Down Expand Up @@ -745,7 +744,7 @@ func (b *builder) addInFlowEdge(s, d node) {

// Creates const, pointer, global, func, and local nodes based on register instructions.
func (b *builder) nodeFromVal(val ssa.Value) node {
if p, ok := aliases.Unalias(val.Type()).(*types.Pointer); ok && !types.IsInterface(p.Elem()) && !isFunction(p.Elem()) {
if p, ok := types.Unalias(val.Type()).(*types.Pointer); ok && !types.IsInterface(p.Elem()) && !isFunction(p.Elem()) {
// Nested pointer to interfaces are modeled as a special
// nestedPtrInterface node.
if i := interfaceUnderPtr(p.Elem()); i != nil {
Expand Down
3 changes: 1 addition & 2 deletions go/callgraph/vta/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"golang.org/x/tools/go/callgraph/cha"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
"golang.org/x/tools/internal/aliases"
)

func TestNodeInterface(t *testing.T) {
Expand All @@ -38,7 +37,7 @@ func TestNodeInterface(t *testing.T) {
reg := firstRegInstr(main) // t0 := *gl
X := pkg.Type("X").Type()
gl := pkg.Var("gl")
glPtrType, ok := aliases.Unalias(gl.Type()).(*types.Pointer)
glPtrType, ok := types.Unalias(gl.Type()).(*types.Pointer)
if !ok {
t.Fatalf("could not cast gl variable to pointer type")
}
Expand Down
Loading

0 comments on commit 1d5e334

Please sign in to comment.