Skip to content

Commit

Permalink
internal/typeparams: eliminate type aliases
Browse files Browse the repository at this point in the history
Change-Id: I660520bbb1dae855e52bf92492045bb3b16eff8d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/549119
Reviewed-by: Robert Findley <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
adonovan committed Dec 12, 2023
1 parent 23c86e8 commit 67611a1
Show file tree
Hide file tree
Showing 52 changed files with 157 additions and 196 deletions.
4 changes: 2 additions & 2 deletions go/analysis/passes/composite/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
}
var structuralTypes []types.Type
switch typ := typ.(type) {
case *typeparams.TypeParam:
case *types.TypeParam:
terms, err := typeparams.StructuralTerms(typ)
if err != nil {
return // invalid type
Expand Down Expand Up @@ -163,7 +163,7 @@ func isLocalType(pass *analysis.Pass, typ types.Type) bool {
case *types.Named:
// names in package foo are local to foo_test too
return strings.TrimSuffix(x.Obj().Pkg().Path(), "_test") == strings.TrimSuffix(pass.Pkg.Path(), "_test")
case *typeparams.TypeParam:
case *types.TypeParam:
return strings.TrimSuffix(x.Obj().Pkg().Path(), "_test") == strings.TrimSuffix(pass.Pkg.Path(), "_test")
}
return false
Expand Down
2 changes: 1 addition & 1 deletion go/analysis/passes/copylock/copylock.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func lockPath(tpkg *types.Package, typ types.Type, seen map[types.Type]bool) typ
}
seen[typ] = true

if tpar, ok := typ.(*typeparams.TypeParam); ok {
if tpar, ok := typ.(*types.TypeParam); ok {
terms, err := typeparams.StructuralTerms(tpar)
if err != nil {
return nil // invalid type
Expand Down
2 changes: 1 addition & 1 deletion go/analysis/passes/ifaceassert/parameterized.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (w *tpWalker) isParameterized(typ types.Type) (res bool) {
}
}

case *typeparams.TypeParam:
case *types.TypeParam:
return true

default:
Expand Down
2 changes: 1 addition & 1 deletion go/analysis/passes/nilfunc/nilfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
obj = pass.TypesInfo.Uses[v]
case *ast.SelectorExpr:
obj = pass.TypesInfo.Uses[v.Sel]
case *ast.IndexExpr, *typeparams.IndexListExpr:
case *ast.IndexExpr, *ast.IndexListExpr:
// Check generic functions such as "f[T1,T2]".
x, _, _, _ := typeparams.UnpackIndexExpr(v)
if id, ok := x.(*ast.Ident); ok {
Expand Down
2 changes: 1 addition & 1 deletion go/analysis/passes/printf/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (m *argMatcher) match(typ types.Type, topLevel bool) bool {
return true
}

if typ, _ := typ.(*typeparams.TypeParam); typ != nil {
if typ, _ := typ.(*types.TypeParam); typ != nil {
// Avoid infinite recursion through type parameters.
if m.seen[typ] {
return true
Expand Down
2 changes: 1 addition & 1 deletion go/analysis/passes/shift/shift.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func checkLongShift(pass *analysis.Pass, node ast.Node, x, y ast.Expr) {
}
var structuralTypes []types.Type
switch t := t.(type) {
case *typeparams.TypeParam:
case *types.TypeParam:
terms, err := typeparams.StructuralTerms(t)
if err != nil {
return // invalid type
Expand Down
2 changes: 1 addition & 1 deletion go/analysis/passes/stringintconv/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
func structuralTypes(t types.Type) ([]types.Type, error) {
var structuralTypes []types.Type
switch t := t.(type) {
case *typeparams.TypeParam:
case *types.TypeParam:
terms, err := typeparams.StructuralTerms(t)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion go/analysis/passes/testinggoroutine/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func isMethodNamed(f *types.Func, pkgPath string, names ...string) bool {

func funcIdent(fun ast.Expr) *ast.Ident {
switch fun := astutil.Unparen(fun).(type) {
case *ast.IndexExpr, *typeparams.IndexListExpr:
case *ast.IndexExpr, *ast.IndexListExpr:
x, _, _, _ := typeparams.UnpackIndexExpr(fun) // necessary?
id, _ := x.(*ast.Ident)
return id
Expand Down
3 changes: 1 addition & 2 deletions go/analysis/passes/unmarshal/unmarshal.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/internal/analysisutil"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/go/types/typeutil"
"golang.org/x/tools/internal/typeparams"
)

//go:embed doc.go
Expand Down Expand Up @@ -92,7 +91,7 @@ func run(pass *analysis.Pass) (interface{}, error) {

t := pass.TypesInfo.Types[call.Args[argidx]].Type
switch t.Underlying().(type) {
case *types.Pointer, *types.Interface, *typeparams.TypeParam:
case *types.Pointer, *types.Interface, *types.TypeParam:
return
}

Expand Down
4 changes: 2 additions & 2 deletions go/ast/astutil/enclosing.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func childrenOf(n ast.Node) []ast.Node {
tok(n.Lbrack, len("[")),
tok(n.Rbrack, len("]")))

case *typeparams.IndexListExpr:
case *ast.IndexListExpr:
children = append(children,
tok(n.Lbrack, len("[")),
tok(n.Rbrack, len("]")))
Expand Down Expand Up @@ -588,7 +588,7 @@ func NodeDescription(n ast.Node) string {
return "decrement statement"
case *ast.IndexExpr:
return "index expression"
case *typeparams.IndexListExpr:
case *ast.IndexListExpr:
return "index list expression"
case *ast.InterfaceType:
return "interface type"
Expand Down
2 changes: 1 addition & 1 deletion go/ast/astutil/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.
a.apply(n, "X", nil, n.X)
a.apply(n, "Index", nil, n.Index)

case *typeparams.IndexListExpr:
case *ast.IndexListExpr:
a.apply(n, "X", nil, n.X)
a.applyList(n, "Indices")

Expand Down
9 changes: 4 additions & 5 deletions go/ast/inspector/inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"testing"

"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/internal/typeparams"
)

var netFiles []*ast.File
Expand Down Expand Up @@ -94,7 +93,7 @@ var _ i13[i14, i15]
inspect := inspector.New([]*ast.File{f})
found := make([]bool, 16)

indexListExprs := make(map[*typeparams.IndexListExpr]bool)
indexListExprs := make(map[*ast.IndexListExpr]bool)

// Verify that we reach all i* identifiers, and collect IndexListExpr nodes.
inspect.Preorder(nil, func(n ast.Node) {
Expand All @@ -107,7 +106,7 @@ var _ i13[i14, i15]
}
found[index] = true
}
case *typeparams.IndexListExpr:
case *ast.IndexListExpr:
indexListExprs[n] = false
}
})
Expand All @@ -122,8 +121,8 @@ var _ i13[i14, i15]
if len(indexListExprs) == 0 {
t.Fatal("no index list exprs found")
}
inspect.Preorder([]ast.Node{&typeparams.IndexListExpr{}}, func(n ast.Node) {
ix := n.(*typeparams.IndexListExpr)
inspect.Preorder([]ast.Node{&ast.IndexListExpr{}}, func(n ast.Node) {
ix := n.(*ast.IndexListExpr)
indexListExprs[ix] = true
})
for ix, v := range indexListExprs {
Expand Down
4 changes: 1 addition & 3 deletions go/ast/inspector/typeof.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ package inspector
import (
"go/ast"
"math"

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

const (
Expand Down Expand Up @@ -171,7 +169,7 @@ func typeOf(n ast.Node) uint64 {
return 1 << nIncDecStmt
case *ast.IndexExpr:
return 1 << nIndexExpr
case *typeparams.IndexListExpr:
case *ast.IndexListExpr:
return 1 << nIndexListExpr
case *ast.InterfaceType:
return 1 << nInterfaceType
Expand Down
10 changes: 5 additions & 5 deletions go/callgraph/vta/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,22 +661,22 @@ func addReturnFlows(b *builder, r *ssa.Return, site ssa.Value) {
func (b *builder) multiconvert(c *ssa.MultiConvert) {
// TODO(zpavlinovic): decide what to do on MultiConvert long term.
// TODO(zpavlinovic): add unit tests.
typeSetOf := func(typ types.Type) []*typeparams.Term {
typeSetOf := func(typ types.Type) []*types.Term {
// This is a adaptation of x/exp/typeparams.NormalTerms which x/tools cannot depend on.
var terms []*typeparams.Term
var terms []*types.Term
var err error
switch typ := typ.(type) {
case *typeparams.TypeParam:
case *types.TypeParam:
terms, err = typeparams.StructuralTerms(typ)
case *typeparams.Union:
case *types.Union:
terms, err = typeparams.UnionTermSet(typ)
case *types.Interface:
terms, err = typeparams.InterfaceTermSet(typ)
default:
// Common case.
// Specializing the len=1 case to avoid a slice
// had no measurable space/time benefit.
terms = []*typeparams.Term{typeparams.NewTerm(false, typ)}
terms = []*types.Term{typeparams.NewTerm(false, typ)}
}

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions go/ssa/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value {
if types.IsInterface(rt) {
// If v may be an interface type I (after instantiating),
// we must emit a check that v is non-nil.
if recv, ok := sel.recv.(*typeparams.TypeParam); ok {
if recv, ok := sel.recv.(*types.TypeParam); ok {
// Emit a nil check if any possible instantiation of the
// type parameter is an interface type.
if typeSetOf(recv).Len() > 0 {
Expand Down Expand Up @@ -848,7 +848,7 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value {

panic("unexpected expression-relative selector")

case *typeparams.IndexListExpr:
case *ast.IndexListExpr:
// f[X, Y] must be a generic function
if !instance(fn.info, e.X) {
panic("unexpected expression-could not match index list to instantiation")
Expand Down
2 changes: 1 addition & 1 deletion go/ssa/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func zeroString(t types.Type, from *types.Package) string {
components[i] = zeroString(t.At(i).Type(), from)
}
return "(" + strings.Join(components, ", ") + ")"
case *typeparams.TypeParam:
case *types.TypeParam:
return "*new(" + relType(t, from) + ")"
}
panic(fmt.Sprint("zeroString: unexpected ", t))
Expand Down
10 changes: 5 additions & 5 deletions go/ssa/coretype.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,27 @@ func isBytestring(T types.Type) bool {
}

// termList is a list of types.
type termList []*typeparams.Term // type terms of the type set
type termList []*types.Term // type terms of the type set
func (s termList) Len() int { return len(s) }
func (s termList) At(i int) types.Type { return s[i].Type() }

// typeSetOf returns the type set of typ. Returns an empty typeset on an error.
func typeSetOf(typ types.Type) termList {
// This is a adaptation of x/exp/typeparams.NormalTerms which x/tools cannot depend on.
var terms []*typeparams.Term
var terms []*types.Term
var err error
switch typ := typ.(type) {
case *typeparams.TypeParam:
case *types.TypeParam:
terms, err = typeparams.StructuralTerms(typ)
case *typeparams.Union:
case *types.Union:
terms, err = typeparams.UnionTermSet(typ)
case *types.Interface:
terms, err = typeparams.InterfaceTermSet(typ)
default:
// Common case.
// Specializing the len=1 case to avoid a slice
// had no measurable space/time benefit.
terms = []*typeparams.Term{typeparams.NewTerm(false, typ)}
terms = []*types.Term{typeparams.NewTerm(false, typ)}
}

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go/ssa/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func createFunction(prog *Program, obj *types.Func, name string, syntax ast.Node
sig := obj.Type().(*types.Signature)

// Collect type parameters.
var tparams *typeparams.TypeParamList
var tparams *types.TypeParamList
if rtparams := typeparams.RecvTypeParams(sig); rtparams.Len() > 0 {
tparams = rtparams // method of generic type
} else if sigparams := typeparams.ForSignature(sig); sigparams.Len() > 0 {
Expand Down
2 changes: 1 addition & 1 deletion go/ssa/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func forEachReachable(msets *typeutil.MethodSetCache, T types.Type, f func(types
visit(T.At(i).Type(), false)
}

case *typeparams.TypeParam, *typeparams.Union:
case *types.TypeParam, *types.Union:
// forEachReachable must not be called on parameterized types.
panic(T)

Expand Down
2 changes: 1 addition & 1 deletion go/ssa/parameterized.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (w *tpWalker) isParameterizedLocked(typ types.Type) (res bool) {
}
return w.isParameterizedLocked(t.Underlying()) // recurse for types local to parameterized functions

case *typeparams.TypeParam:
case *types.TypeParam:
return true

default:
Expand Down
3 changes: 1 addition & 2 deletions go/ssa/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"strings"

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

// relName returns the name of v relative to i.
Expand Down Expand Up @@ -51,7 +50,7 @@ func relType(t types.Type, from *types.Package) string {
return s
}

func relTerm(term *typeparams.Term, from *types.Package) string {
func relTerm(term *types.Term, from *types.Package) string {
s := relType(term.Type(), from)
if term.Tilde() {
return "~" + s
Expand Down
21 changes: 9 additions & 12 deletions go/ssa/ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type Program struct {
mode BuilderMode // set of mode bits for SSA construction
MethodSets typeutil.MethodSetCache // cache of type-checker's method-sets

canon *canonizer // type canonicalization map
ctxt *typeparams.Context // cache for type checking instantiations
canon *canonizer // type canonicalization map
ctxt *types.Context // cache for type checking instantiations

methodsMu sync.Mutex
methodSets typeutil.Map // maps type to its concrete *methodSet
Expand Down Expand Up @@ -339,10 +339,10 @@ type Function struct {
referrers []Instruction // referring instructions (iff Parent() != nil)
anonIdx int32 // position of a nested function in parent's AnonFuncs. fn.Parent()!=nil => fn.Parent().AnonFunc[fn.anonIdx] == fn.

typeparams *typeparams.TypeParamList // type parameters of this function. typeparams.Len() > 0 => generic or instance of generic function
typeargs []types.Type // type arguments that instantiated typeparams. len(typeargs) > 0 => instance of generic function
topLevelOrigin *Function // the origin function if this is an instance of a source function. nil if Parent()!=nil.
generic *generic // instances of this function, if generic
typeparams *types.TypeParamList // type parameters of this function. typeparams.Len() > 0 => generic or instance of generic function
typeargs []types.Type // type arguments that instantiated typeparams. len(typeargs) > 0 => instance of generic function
topLevelOrigin *Function // the origin function if this is an instance of a source function. nil if Parent()!=nil.
generic *generic // instances of this function, if generic

// The following fields are cleared after building.
currentBlock *BasicBlock // where to emit code
Expand Down Expand Up @@ -690,8 +690,8 @@ type Convert struct {
type MultiConvert struct {
register
X Value
from []*typeparams.Term
to []*typeparams.Term
from []*types.Term
to []*types.Term
}

// ChangeInterface constructs a value of one interface type from a
Expand Down Expand Up @@ -1539,10 +1539,7 @@ func (v *Function) Referrers() *[]Instruction {

// TypeParams are the function's type parameters if generic or the
// type parameters that were instantiated if fn is an instantiation.
//
// TODO(taking): declare result type as *types.TypeParamList
// after we drop support for go1.17.
func (fn *Function) TypeParams() *typeparams.TypeParamList {
func (fn *Function) TypeParams() *types.TypeParamList {
return fn.typeparams
}

Expand Down
Loading

0 comments on commit 67611a1

Please sign in to comment.