Skip to content

Commit 28b92af

Browse files
adonovangopherbot
authored andcommitted
internal/typeparams: eliminate remainining compatibility shims
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 <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent ee35f8e commit 28b92af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+126
-295
lines changed

go/analysis/passes/ifaceassert/parameterized.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (w *tpWalker) isParameterized(typ types.Type) (res bool) {
9595
return w.isParameterized(t.Elem())
9696

9797
case *types.Named:
98-
list := typeparams.NamedTypeArgs(t)
98+
list := t.TypeArgs()
9999
for i, n := 0, list.Len(); i < n; i++ {
100100
if w.isParameterized(list.At(i)) {
101101
return true

go/analysis/passes/tests/tests.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717

1818
"golang.org/x/tools/go/analysis"
1919
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
20-
"golang.org/x/tools/internal/typeparams"
2120
)
2221

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

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

463-
if tparams := typeparams.ForFuncType(fn.Type); tparams != nil && len(tparams.List) > 0 {
462+
if tparams := fn.Type.TypeParams; tparams != nil && len(tparams.List) > 0 {
464463
// Note: cmd/go/internal/load also errors about TestXXX and BenchmarkXXX functions with type parameters.
465464
// We have currently decided to also warn before compilation/package loading. This can help users in IDEs.
466465
// TODO(adonovan): use ReportRangef(tparams).

go/analysis/unitchecker/unitchecker.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ import (
5050
"golang.org/x/tools/go/analysis"
5151
"golang.org/x/tools/go/analysis/internal/analysisflags"
5252
"golang.org/x/tools/internal/facts"
53-
"golang.org/x/tools/internal/typeparams"
5453
"golang.org/x/tools/internal/versions"
5554
)
5655

@@ -259,10 +258,10 @@ func run(fset *token.FileSet, cfg *Config, analyzers []*analysis.Analyzer) ([]re
259258
Defs: make(map[*ast.Ident]types.Object),
260259
Uses: make(map[*ast.Ident]types.Object),
261260
Implicits: make(map[ast.Node]types.Object),
261+
Instances: make(map[*ast.Ident]types.Instance),
262262
Scopes: make(map[ast.Node]*types.Scope),
263263
Selections: make(map[*ast.SelectorExpr]*types.Selection),
264264
}
265-
typeparams.InitInstanceInfo(info)
266265
versions.InitFileVersions(info)
267266

268267
pkg, err := tc.Check(cfg.ImportPath, fset, files, info)

go/ast/astutil/enclosing.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"go/ast"
1212
"go/token"
1313
"sort"
14-
15-
"golang.org/x/tools/internal/typeparams"
1614
)
1715

1816
// PathEnclosingInterval returns the node that encloses the source
@@ -322,7 +320,7 @@ func childrenOf(n ast.Node) []ast.Node {
322320
children = append(children, n.Recv)
323321
}
324322
children = append(children, n.Name)
325-
if tparams := typeparams.ForFuncType(n.Type); tparams != nil {
323+
if tparams := n.Type.TypeParams; tparams != nil {
326324
children = append(children, tparams)
327325
}
328326
if n.Type.Params != nil {

go/ast/astutil/rewrite.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"go/ast"
1010
"reflect"
1111
"sort"
12-
13-
"golang.org/x/tools/internal/typeparams"
1412
)
1513

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

295293
case *ast.FuncType:
296-
if tparams := typeparams.ForFuncType(n); tparams != nil {
294+
if tparams := n.TypeParams; tparams != nil {
297295
a.apply(n, "TypeParams", nil, tparams)
298296
}
299297
a.apply(n, "Params", nil, n.Params)
@@ -408,7 +406,7 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.
408406
case *ast.TypeSpec:
409407
a.apply(n, "Doc", nil, n.Doc)
410408
a.apply(n, "Name", nil, n.Name)
411-
if tparams := typeparams.ForTypeSpec(n); tparams != nil {
409+
if tparams := n.TypeParams; tparams != nil {
412410
a.apply(n, "TypeParams", nil, tparams)
413411
}
414412
a.apply(n, "Type", nil, n.Type)

go/callgraph/vta/graph.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ func (b *builder) multiconvert(c *ssa.MultiConvert) {
676676
// Common case.
677677
// Specializing the len=1 case to avoid a slice
678678
// had no measurable space/time benefit.
679-
terms = []*types.Term{typeparams.NewTerm(false, typ)}
679+
terms = []*types.Term{types.NewTerm(false, typ)}
680680
}
681681

682682
if err != nil {

go/loader/loader.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
"golang.org/x/tools/go/ast/astutil"
2525
"golang.org/x/tools/go/internal/cgo"
26-
"golang.org/x/tools/internal/typeparams"
2726
"golang.org/x/tools/internal/versions"
2827
)
2928

@@ -1034,13 +1033,13 @@ func (imp *importer) newPackageInfo(path, dir string) *PackageInfo {
10341033
Defs: make(map[*ast.Ident]types.Object),
10351034
Uses: make(map[*ast.Ident]types.Object),
10361035
Implicits: make(map[ast.Node]types.Object),
1036+
Instances: make(map[*ast.Ident]types.Instance),
10371037
Scopes: make(map[ast.Node]*types.Scope),
10381038
Selections: make(map[*ast.SelectorExpr]*types.Selection),
10391039
},
10401040
errorFunc: imp.conf.TypeChecker.Error,
10411041
dir: dir,
10421042
}
1043-
typeparams.InitInstanceInfo(&info.Info)
10441043
versions.InitFileVersions(&info.Info)
10451044

10461045
// Copy the types.Config so we can vary it across PackageInfos.

go/packages/packages.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"golang.org/x/tools/go/gcexportdata"
2828
"golang.org/x/tools/internal/gocommand"
2929
"golang.org/x/tools/internal/packagesinternal"
30-
"golang.org/x/tools/internal/typeparams"
3130
"golang.org/x/tools/internal/typesinternal"
3231
"golang.org/x/tools/internal/versions"
3332
)
@@ -1015,10 +1014,10 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
10151014
Defs: make(map[*ast.Ident]types.Object),
10161015
Uses: make(map[*ast.Ident]types.Object),
10171016
Implicits: make(map[ast.Node]types.Object),
1017+
Instances: make(map[*ast.Ident]types.Instance),
10181018
Scopes: make(map[ast.Node]*types.Scope),
10191019
Selections: make(map[*ast.SelectorExpr]*types.Selection),
10201020
}
1021-
typeparams.InitInstanceInfo(lpkg.TypesInfo)
10221021
versions.InitFileVersions(lpkg.TypesInfo)
10231022
lpkg.TypesSizes = ld.sizes
10241023

go/ssa/builder_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"golang.org/x/tools/go/ssa"
2727
"golang.org/x/tools/go/ssa/ssautil"
2828
"golang.org/x/tools/internal/testenv"
29-
"golang.org/x/tools/internal/typeparams"
3029
"golang.org/x/tools/txtar"
3130
)
3231

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

692-
if load := p.Func("Load"); typeparams.ForSignature(load.Signature).Len() != 1 {
691+
if load := p.Func("Load"); load.Signature.TypeParams().Len() != 1 {
693692
t.Errorf("expected a single type param T for Load got %q", load.Signature)
694693
}
695-
if ptr := p.Type("Pointer"); typeparams.ForNamed(ptr.Type().(*types.Named)).Len() != 1 {
694+
if ptr := p.Type("Pointer"); ptr.Type().(*types.Named).TypeParams().Len() != 1 {
696695
t.Errorf("expected a single type param T for Pointer got %q", ptr.Type())
697696
}
698697
}

go/ssa/const_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"testing"
1616

1717
"golang.org/x/tools/go/ssa"
18-
"golang.org/x/tools/internal/typeparams"
1918
)
2019

2120
func TestConstString(t *testing.T) {
@@ -93,7 +92,7 @@ func TestConstString(t *testing.T) {
9392

9493
// Test type-param
9594
gen := pkg.Scope().Lookup("gen")
96-
tp := typeparams.ForSignature(gen.Type().(*types.Signature)).At(0)
95+
tp := gen.Type().(*types.Signature).TypeParams().At(0)
9796
if got, want := ssa.NewConst(nil, tp).String(), "0:T"; got != want {
9897
t.Errorf("ssa.NewConst(%v, %s).String() = %v, want %v", nil, tup, got, want)
9998
}

go/ssa/coretype.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func typeSetOf(typ types.Type) termList {
6060
// Common case.
6161
// Specializing the len=1 case to avoid a slice
6262
// had no measurable space/time benefit.
63-
terms = []*types.Term{typeparams.NewTerm(false, typ)}
63+
terms = []*types.Term{types.NewTerm(false, typ)}
6464
}
6565

6666
if err != nil {

go/ssa/create.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"os"
1616
"sync"
1717

18-
"golang.org/x/tools/internal/typeparams"
1918
"golang.org/x/tools/internal/versions"
2019
)
2120

@@ -40,7 +39,7 @@ func NewProgram(fset *token.FileSet, mode BuilderMode) *Program {
4039
packages: make(map[*types.Package]*Package),
4140
mode: mode,
4241
canon: newCanonizer(),
43-
ctxt: typeparams.NewContext(),
42+
ctxt: types.NewContext(),
4443
parameterized: tpWalker{seen: make(map[types.Type]bool)},
4544
}
4645
}
@@ -118,9 +117,9 @@ func createFunction(prog *Program, obj *types.Func, name string, syntax ast.Node
118117

119118
// Collect type parameters.
120119
var tparams *types.TypeParamList
121-
if rtparams := typeparams.RecvTypeParams(sig); rtparams.Len() > 0 {
120+
if rtparams := sig.RecvTypeParams(); rtparams.Len() > 0 {
122121
tparams = rtparams // method of generic type
123-
} else if sigparams := typeparams.ForSignature(sig); sigparams.Len() > 0 {
122+
} else if sigparams := sig.TypeParams(); sigparams.Len() > 0 {
124123
tparams = sigparams // generic function
125124
}
126125

go/ssa/func.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import (
1414
"io"
1515
"os"
1616
"strings"
17-
18-
"golang.org/x/tools/internal/typeparams"
1917
)
2018

2119
// Like ObjectOf, but panics instead of returning nil.
@@ -45,7 +43,7 @@ func (f *Function) typ(T types.Type) types.Type {
4543
// If id is an Instance, returns info.Instances[id].Type.
4644
// Otherwise returns f.typeOf(id).
4745
func (f *Function) instanceType(id *ast.Ident) types.Type {
48-
if t, ok := typeparams.GetInstances(f.info)[id]; ok {
46+
if t, ok := f.info.Instances[id]; ok {
4947
return t.Type
5048
}
5149
return f.typeOf(id)

go/ssa/instantiate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func createInstance(fn *Function, targs []types.Type, cr *creator) *Function {
5959
sig = obj.Type().(*types.Signature)
6060
} else {
6161
// function
62-
instSig, err := typeparams.Instantiate(prog.ctxt, fn.Signature, targs, false)
62+
instSig, err := types.Instantiate(prog.ctxt, fn.Signature, targs, false)
6363
if err != nil {
6464
panic(err)
6565
}

go/ssa/parameterized.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ func (w *tpWalker) isParameterizedLocked(typ types.Type) (res bool) {
105105
return w.isParameterizedLocked(t.Elem())
106106

107107
case *types.Named:
108-
args := typeparams.NamedTypeArgs(t)
108+
args := t.TypeArgs()
109109
// TODO(taking): this does not match go/types/infer.go. Check with rfindley.
110-
if params := typeparams.ForNamed(t); params.Len() > args.Len() {
110+
if params := t.TypeParams(); params.Len() > args.Len() {
111111
return true
112112
}
113113
for i, n := 0, args.Len(); i < n; i++ {

go/ssa/ssautil/load.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"golang.org/x/tools/go/loader"
1515
"golang.org/x/tools/go/packages"
1616
"golang.org/x/tools/go/ssa"
17-
"golang.org/x/tools/internal/typeparams"
1817
"golang.org/x/tools/internal/versions"
1918
)
2019

@@ -163,10 +162,10 @@ func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, fil
163162
Defs: make(map[*ast.Ident]types.Object),
164163
Uses: make(map[*ast.Ident]types.Object),
165164
Implicits: make(map[ast.Node]types.Object),
165+
Instances: make(map[*ast.Ident]types.Instance),
166166
Scopes: make(map[ast.Node]*types.Scope),
167167
Selections: make(map[*ast.SelectorExpr]*types.Selection),
168168
}
169-
typeparams.InitInstanceInfo(info)
170169
versions.InitFileVersions(info)
171170
if err := types.NewChecker(tc, fset, pkg, info).Files(files); err != nil {
172171
return nil, nil, err

go/ssa/ssautil/visit.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"go/types"
1010

1111
"golang.org/x/tools/go/ssa"
12-
"golang.org/x/tools/internal/typeparams"
1312

1413
_ "unsafe" // for linkname hack
1514
)
@@ -105,7 +104,7 @@ func AllFunctions(prog *ssa.Program) map[*ssa.Function]bool {
105104
// Consider only named types.
106105
// (Ignore aliases and unsafe.Pointer.)
107106
if named, ok := t.Type().(*types.Named); ok {
108-
if typeparams.ForNamed(named) == nil {
107+
if named.TypeParams() == nil {
109108
methodsOf(named) // T
110109
methodsOf(types.NewPointer(named)) // *T
111110
}

go/ssa/subst.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ package ssa
66

77
import (
88
"go/types"
9-
10-
"golang.org/x/tools/internal/typeparams"
119
)
1210

1311
// Type substituter for a fixed set of replacement types.
@@ -233,12 +231,12 @@ func (subst *subster) union(u *types.Union) *types.Union {
233231
}
234232
}
235233
if out != nil {
236-
out[i] = typeparams.NewTerm(t.Tilde(), r)
234+
out[i] = types.NewTerm(t.Tilde(), r)
237235
}
238236
}
239237

240238
if out != nil {
241-
return typeparams.NewUnion(out)
239+
return types.NewUnion(out)
242240
}
243241
return u
244242
}
@@ -310,7 +308,7 @@ func (subst *subster) named(t *types.Named) types.Type {
310308
// (2) locally scoped type,
311309
// (3) generic (type parameters but no type arguments), or
312310
// (4) instantiated (type parameters and type arguments).
313-
tparams := typeparams.ForNamed(t)
311+
tparams := t.TypeParams()
314312
if tparams.Len() == 0 {
315313
if subst.scope != nil && !subst.scope.Contains(t.Obj().Pos()) {
316314
// Outside the current function scope?
@@ -344,7 +342,7 @@ func (subst *subster) named(t *types.Named) types.Type {
344342
n.SetUnderlying(subst.typ(t.Underlying()))
345343
return n
346344
}
347-
targs := typeparams.NamedTypeArgs(t)
345+
targs := t.TypeArgs()
348346

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

375373
func (subst *subster) signature(t *types.Signature) types.Type {
376-
tparams := typeparams.ForSignature(t)
374+
tparams := t.TypeParams()
377375

378376
// We are choosing not to support tparams.Len() > 0 until a need has been observed in practice.
379377
//
@@ -398,7 +396,7 @@ func (subst *subster) signature(t *types.Signature) types.Type {
398396
params := subst.tuple(t.Params())
399397
results := subst.tuple(t.Results())
400398
if recv != t.Recv() || params != t.Params() || results != t.Results() {
401-
return typeparams.NewSignatureType(recv, nil, nil, params, results, t.Variadic())
399+
return types.NewSignatureType(recv, nil, nil, params, results, t.Variadic())
402400
}
403401
return t
404402
}

go/ssa/subst_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"go/token"
1111
"go/types"
1212
"testing"
13-
14-
"golang.org/x/tools/internal/typeparams"
1513
)
1614

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

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

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

0 commit comments

Comments
 (0)