Skip to content

Commit

Permalink
internal/typeparams: eliminate remainining compatibility shims
Browse files Browse the repository at this point in the history
The nil checks in wrappers that had them were all redundant.

Change-Id: Ide7296f2253610638b59dc4980b0487b9de72f0c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/549236
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
adonovan authored and gopherbot committed Dec 12, 2023
1 parent ee35f8e commit 28b92af
Show file tree
Hide file tree
Showing 52 changed files with 126 additions and 295 deletions.
2 changes: 1 addition & 1 deletion go/analysis/passes/ifaceassert/parameterized.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (w *tpWalker) isParameterized(typ types.Type) (res bool) {
return w.isParameterized(t.Elem())

case *types.Named:
list := typeparams.NamedTypeArgs(t)
list := t.TypeArgs()
for i, n := 0, list.Len(); i < n; i++ {
if w.isParameterized(list.At(i)) {
return true
Expand Down
5 changes: 2 additions & 3 deletions go/analysis/passes/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

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

//go:embed doc.go
Expand Down Expand Up @@ -391,7 +390,7 @@ func checkExampleName(pass *analysis.Pass, fn *ast.FuncDecl) {
if results := fn.Type.Results; results != nil && len(results.List) != 0 {
pass.Reportf(fn.Pos(), "%s should return nothing", fnName)
}
if tparams := typeparams.ForFuncType(fn.Type); tparams != nil && len(tparams.List) > 0 {
if tparams := fn.Type.TypeParams; tparams != nil && len(tparams.List) > 0 {
pass.Reportf(fn.Pos(), "%s should not have type params", fnName)
}

Expand Down Expand Up @@ -460,7 +459,7 @@ func checkTest(pass *analysis.Pass, fn *ast.FuncDecl, prefix string) {
return
}

if tparams := typeparams.ForFuncType(fn.Type); tparams != nil && len(tparams.List) > 0 {
if tparams := fn.Type.TypeParams; tparams != nil && len(tparams.List) > 0 {
// Note: cmd/go/internal/load also errors about TestXXX and BenchmarkXXX functions with type parameters.
// We have currently decided to also warn before compilation/package loading. This can help users in IDEs.
// TODO(adonovan): use ReportRangef(tparams).
Expand Down
3 changes: 1 addition & 2 deletions go/analysis/unitchecker/unitchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import (
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/internal/analysisflags"
"golang.org/x/tools/internal/facts"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/versions"
)

Expand Down Expand Up @@ -259,10 +258,10 @@ func run(fset *token.FileSet, cfg *Config, analyzers []*analysis.Analyzer) ([]re
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Implicits: make(map[ast.Node]types.Object),
Instances: make(map[*ast.Ident]types.Instance),
Scopes: make(map[ast.Node]*types.Scope),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
}
typeparams.InitInstanceInfo(info)
versions.InitFileVersions(info)

pkg, err := tc.Check(cfg.ImportPath, fset, files, info)
Expand Down
4 changes: 1 addition & 3 deletions go/ast/astutil/enclosing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"go/ast"
"go/token"
"sort"

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

// PathEnclosingInterval returns the node that encloses the source
Expand Down Expand Up @@ -322,7 +320,7 @@ func childrenOf(n ast.Node) []ast.Node {
children = append(children, n.Recv)
}
children = append(children, n.Name)
if tparams := typeparams.ForFuncType(n.Type); tparams != nil {
if tparams := n.Type.TypeParams; tparams != nil {
children = append(children, tparams)
}
if n.Type.Params != nil {
Expand Down
6 changes: 2 additions & 4 deletions go/ast/astutil/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"go/ast"
"reflect"
"sort"

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

// An ApplyFunc is invoked by Apply for each node n, even if n is nil,
Expand Down Expand Up @@ -293,7 +291,7 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.
a.apply(n, "Fields", nil, n.Fields)

case *ast.FuncType:
if tparams := typeparams.ForFuncType(n); tparams != nil {
if tparams := n.TypeParams; tparams != nil {
a.apply(n, "TypeParams", nil, tparams)
}
a.apply(n, "Params", nil, n.Params)
Expand Down Expand Up @@ -408,7 +406,7 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.
case *ast.TypeSpec:
a.apply(n, "Doc", nil, n.Doc)
a.apply(n, "Name", nil, n.Name)
if tparams := typeparams.ForTypeSpec(n); tparams != nil {
if tparams := n.TypeParams; tparams != nil {
a.apply(n, "TypeParams", nil, tparams)
}
a.apply(n, "Type", nil, n.Type)
Expand Down
2 changes: 1 addition & 1 deletion go/callgraph/vta/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ func (b *builder) multiconvert(c *ssa.MultiConvert) {
// Common case.
// Specializing the len=1 case to avoid a slice
// had no measurable space/time benefit.
terms = []*types.Term{typeparams.NewTerm(false, typ)}
terms = []*types.Term{types.NewTerm(false, typ)}
}

if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions go/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/internal/cgo"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/versions"
)

Expand Down Expand Up @@ -1034,13 +1033,13 @@ func (imp *importer) newPackageInfo(path, dir string) *PackageInfo {
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Implicits: make(map[ast.Node]types.Object),
Instances: make(map[*ast.Ident]types.Instance),
Scopes: make(map[ast.Node]*types.Scope),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
},
errorFunc: imp.conf.TypeChecker.Error,
dir: dir,
}
typeparams.InitInstanceInfo(&info.Info)
versions.InitFileVersions(&info.Info)

// Copy the types.Config so we can vary it across PackageInfos.
Expand Down
3 changes: 1 addition & 2 deletions go/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"golang.org/x/tools/go/gcexportdata"
"golang.org/x/tools/internal/gocommand"
"golang.org/x/tools/internal/packagesinternal"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/typesinternal"
"golang.org/x/tools/internal/versions"
)
Expand Down Expand Up @@ -1015,10 +1014,10 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Implicits: make(map[ast.Node]types.Object),
Instances: make(map[*ast.Ident]types.Instance),
Scopes: make(map[ast.Node]*types.Scope),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
}
typeparams.InitInstanceInfo(lpkg.TypesInfo)
versions.InitFileVersions(lpkg.TypesInfo)
lpkg.TypesSizes = ld.sizes

Expand Down
5 changes: 2 additions & 3 deletions go/ssa/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
"golang.org/x/tools/internal/testenv"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/txtar"
)

Expand Down Expand Up @@ -689,10 +688,10 @@ func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)
p := prog.Package(lprog.Package("p").Pkg)
p.Build()

if load := p.Func("Load"); typeparams.ForSignature(load.Signature).Len() != 1 {
if load := p.Func("Load"); load.Signature.TypeParams().Len() != 1 {
t.Errorf("expected a single type param T for Load got %q", load.Signature)
}
if ptr := p.Type("Pointer"); typeparams.ForNamed(ptr.Type().(*types.Named)).Len() != 1 {
if ptr := p.Type("Pointer"); ptr.Type().(*types.Named).TypeParams().Len() != 1 {
t.Errorf("expected a single type param T for Pointer got %q", ptr.Type())
}
}
Expand Down
3 changes: 1 addition & 2 deletions go/ssa/const_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"testing"

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

func TestConstString(t *testing.T) {
Expand Down Expand Up @@ -93,7 +92,7 @@ func TestConstString(t *testing.T) {

// Test type-param
gen := pkg.Scope().Lookup("gen")
tp := typeparams.ForSignature(gen.Type().(*types.Signature)).At(0)
tp := gen.Type().(*types.Signature).TypeParams().At(0)
if got, want := ssa.NewConst(nil, tp).String(), "0:T"; got != want {
t.Errorf("ssa.NewConst(%v, %s).String() = %v, want %v", nil, tup, got, want)
}
Expand Down
2 changes: 1 addition & 1 deletion go/ssa/coretype.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func typeSetOf(typ types.Type) termList {
// Common case.
// Specializing the len=1 case to avoid a slice
// had no measurable space/time benefit.
terms = []*types.Term{typeparams.NewTerm(false, typ)}
terms = []*types.Term{types.NewTerm(false, typ)}
}

if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions go/ssa/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"os"
"sync"

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

Expand All @@ -40,7 +39,7 @@ func NewProgram(fset *token.FileSet, mode BuilderMode) *Program {
packages: make(map[*types.Package]*Package),
mode: mode,
canon: newCanonizer(),
ctxt: typeparams.NewContext(),
ctxt: types.NewContext(),
parameterized: tpWalker{seen: make(map[types.Type]bool)},
}
}
Expand Down Expand Up @@ -118,9 +117,9 @@ func createFunction(prog *Program, obj *types.Func, name string, syntax ast.Node

// Collect type parameters.
var tparams *types.TypeParamList
if rtparams := typeparams.RecvTypeParams(sig); rtparams.Len() > 0 {
if rtparams := sig.RecvTypeParams(); rtparams.Len() > 0 {
tparams = rtparams // method of generic type
} else if sigparams := typeparams.ForSignature(sig); sigparams.Len() > 0 {
} else if sigparams := sig.TypeParams(); sigparams.Len() > 0 {
tparams = sigparams // generic function
}

Expand Down
4 changes: 1 addition & 3 deletions go/ssa/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"io"
"os"
"strings"

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

// Like ObjectOf, but panics instead of returning nil.
Expand Down Expand Up @@ -45,7 +43,7 @@ func (f *Function) typ(T types.Type) types.Type {
// If id is an Instance, returns info.Instances[id].Type.
// Otherwise returns f.typeOf(id).
func (f *Function) instanceType(id *ast.Ident) types.Type {
if t, ok := typeparams.GetInstances(f.info)[id]; ok {
if t, ok := f.info.Instances[id]; ok {
return t.Type
}
return f.typeOf(id)
Expand Down
2 changes: 1 addition & 1 deletion go/ssa/instantiate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func createInstance(fn *Function, targs []types.Type, cr *creator) *Function {
sig = obj.Type().(*types.Signature)
} else {
// function
instSig, err := typeparams.Instantiate(prog.ctxt, fn.Signature, targs, false)
instSig, err := types.Instantiate(prog.ctxt, fn.Signature, targs, false)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions go/ssa/parameterized.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func (w *tpWalker) isParameterizedLocked(typ types.Type) (res bool) {
return w.isParameterizedLocked(t.Elem())

case *types.Named:
args := typeparams.NamedTypeArgs(t)
args := t.TypeArgs()
// TODO(taking): this does not match go/types/infer.go. Check with rfindley.
if params := typeparams.ForNamed(t); params.Len() > args.Len() {
if params := t.TypeParams(); params.Len() > args.Len() {
return true
}
for i, n := 0, args.Len(); i < n; i++ {
Expand Down
3 changes: 1 addition & 2 deletions go/ssa/ssautil/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/versions"
)

Expand Down Expand Up @@ -163,10 +162,10 @@ func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, fil
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Implicits: make(map[ast.Node]types.Object),
Instances: make(map[*ast.Ident]types.Instance),
Scopes: make(map[ast.Node]*types.Scope),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
}
typeparams.InitInstanceInfo(info)
versions.InitFileVersions(info)
if err := types.NewChecker(tc, fset, pkg, info).Files(files); err != nil {
return nil, nil, err
Expand Down
3 changes: 1 addition & 2 deletions go/ssa/ssautil/visit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"go/types"

"golang.org/x/tools/go/ssa"
"golang.org/x/tools/internal/typeparams"

_ "unsafe" // for linkname hack
)
Expand Down Expand Up @@ -105,7 +104,7 @@ func AllFunctions(prog *ssa.Program) map[*ssa.Function]bool {
// Consider only named types.
// (Ignore aliases and unsafe.Pointer.)
if named, ok := t.Type().(*types.Named); ok {
if typeparams.ForNamed(named) == nil {
if named.TypeParams() == nil {
methodsOf(named) // T
methodsOf(types.NewPointer(named)) // *T
}
Expand Down
16 changes: 7 additions & 9 deletions go/ssa/subst.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package ssa

import (
"go/types"

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

// Type substituter for a fixed set of replacement types.
Expand Down Expand Up @@ -233,12 +231,12 @@ func (subst *subster) union(u *types.Union) *types.Union {
}
}
if out != nil {
out[i] = typeparams.NewTerm(t.Tilde(), r)
out[i] = types.NewTerm(t.Tilde(), r)
}
}

if out != nil {
return typeparams.NewUnion(out)
return types.NewUnion(out)
}
return u
}
Expand Down Expand Up @@ -310,7 +308,7 @@ func (subst *subster) named(t *types.Named) types.Type {
// (2) locally scoped type,
// (3) generic (type parameters but no type arguments), or
// (4) instantiated (type parameters and type arguments).
tparams := typeparams.ForNamed(t)
tparams := t.TypeParams()
if tparams.Len() == 0 {
if subst.scope != nil && !subst.scope.Contains(t.Obj().Pos()) {
// Outside the current function scope?
Expand Down Expand Up @@ -344,7 +342,7 @@ func (subst *subster) named(t *types.Named) types.Type {
n.SetUnderlying(subst.typ(t.Underlying()))
return n
}
targs := typeparams.NamedTypeArgs(t)
targs := t.TypeArgs()

// insts are arguments to instantiate using.
insts := make([]types.Type, tparams.Len())
Expand All @@ -367,13 +365,13 @@ func (subst *subster) named(t *types.Named) types.Type {
inst := subst.typ(targs.At(i)) // TODO(generic): Check with rfindley for mutual recursion
insts[i] = inst
}
r, err := typeparams.Instantiate(subst.ctxt, typeparams.NamedTypeOrigin(t), insts, false)
r, err := types.Instantiate(subst.ctxt, t.Origin(), insts, false)
assert(err == nil, "failed to Instantiate Named type")
return r
}

func (subst *subster) signature(t *types.Signature) types.Type {
tparams := typeparams.ForSignature(t)
tparams := t.TypeParams()

// We are choosing not to support tparams.Len() > 0 until a need has been observed in practice.
//
Expand All @@ -398,7 +396,7 @@ func (subst *subster) signature(t *types.Signature) types.Type {
params := subst.tuple(t.Params())
results := subst.tuple(t.Results())
if recv != t.Recv() || params != t.Params() || results != t.Results() {
return typeparams.NewSignatureType(recv, nil, nil, params, results, t.Variadic())
return types.NewSignatureType(recv, nil, nil, params, results, t.Variadic())
}
return t
}
Expand Down
4 changes: 1 addition & 3 deletions go/ssa/subst_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"go/token"
"go/types"
"testing"

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

func TestSubst(t *testing.T) {
Expand Down Expand Up @@ -96,7 +94,7 @@ var _ L[int] = Fn0[L[int]](nil)

T := tv.Type.(*types.Named)

subst := makeSubster(typeparams.NewContext(), nil, typeparams.ForNamed(T), targs, true)
subst := makeSubster(types.NewContext(), nil, T.TypeParams(), targs, true)
sub := subst.typ(T.Underlying())
if got := sub.String(); got != test.want {
t.Errorf("subst{%v->%v}.typ(%s) = %v, want %v", test.expr, test.args, T.Underlying(), got, test.want)
Expand Down
Loading

0 comments on commit 28b92af

Please sign in to comment.