Skip to content

Commit d419f9c

Browse files
committed
go/types: spell out 'Type' in type parameter APIs
As discussed on the go/types proposal (#47916), we should spell out the word 'Type', rather than using 'T'. Change-Id: I5f51255eedc07fea61f909b7ecb3093a7fab765e Reviewed-on: https://go-review.googlesource.com/c/go/+/348376 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 12eb733 commit d419f9c

16 files changed

+85
-85
lines changed

src/go/internal/gcimporter/iimport.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func (r *importReader) obj(name string) {
305305
tparams = r.tparamList()
306306
}
307307
sig := r.signature(nil)
308-
sig.SetTParams(tparams)
308+
sig.SetTypeParams(tparams)
309309
r.declare(types.NewFunc(pos, r.currPkg, name, sig))
310310

311311
case 'T', 'U':
@@ -317,7 +317,7 @@ func (r *importReader) obj(name string) {
317317
// declaration before recursing.
318318
obj := types.NewTypeName(pos, r.currPkg, name, nil)
319319
named := types.NewNamed(obj, nil, nil)
320-
named.SetTParams(tparams)
320+
named.SetTypeParams(tparams)
321321
r.declare(obj)
322322

323323
underlying := r.p.typAt(r.uint64(), named).Underlying()
@@ -333,7 +333,7 @@ func (r *importReader) obj(name string) {
333333
// If the receiver has any targs, set those as the
334334
// rparams of the method (since those are the
335335
// typeparams being used in the method sig/body).
336-
targs := baseType(msig.Recv().Type()).TArgs()
336+
targs := baseType(msig.Recv().Type()).TypeArgs()
337337
if targs.Len() > 0 {
338338
rparams := make([]*types.TypeParam, targs.Len())
339339
for i := range rparams {

src/go/types/api_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,7 @@ func TestInstantiate(t *testing.T) {
18511851

18521852
// type T should have one type parameter
18531853
T := pkg.Scope().Lookup("T").Type().(*Named)
1854-
if n := T.TParams().Len(); n != 1 {
1854+
if n := T.TypeParams().Len(); n != 1 {
18551855
t.Fatalf("expected 1 type parameter; found %d", n)
18561856
}
18571857

src/go/types/assignments.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
7171
}
7272

7373
// A generic (non-instantiated) function value cannot be assigned to a variable.
74-
if sig := asSignature(x.typ); sig != nil && sig.TParams().Len() > 0 {
74+
if sig := asSignature(x.typ); sig != nil && sig.TypeParams().Len() > 0 {
7575
check.errorf(x, _Todo, "cannot use generic function %s without instantiation in %s", x, context)
7676
}
7777

src/go/types/call.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (check *Checker) funcInst(x *operand, ix *typeparams.IndexExpr) {
3131

3232
// check number of type arguments (got) vs number of type parameters (want)
3333
sig := x.typ.(*Signature)
34-
got, want := len(targs), sig.TParams().Len()
34+
got, want := len(targs), sig.TypeParams().Len()
3535
if got > want {
3636
check.errorf(ix.Indices[got-1], _Todo, "got %d type arguments but want %d", got, want)
3737
x.mode = invalid
@@ -43,7 +43,7 @@ func (check *Checker) funcInst(x *operand, ix *typeparams.IndexExpr) {
4343
inferred := false
4444

4545
if got < want {
46-
targs = check.infer(ix.Orig, sig.TParams().list(), targs, nil, nil, true)
46+
targs = check.infer(ix.Orig, sig.TypeParams().list(), targs, nil, nil, true)
4747
if targs == nil {
4848
// error was already reported
4949
x.mode = invalid
@@ -65,7 +65,7 @@ func (check *Checker) funcInst(x *operand, ix *typeparams.IndexExpr) {
6565

6666
// instantiate function signature
6767
res := check.instantiate(x.Pos(), sig, targs, poslist).(*Signature)
68-
assert(res.TParams().Len() == 0) // signature is not generic anymore
68+
assert(res.TypeParams().Len() == 0) // signature is not generic anymore
6969
if inferred {
7070
check.recordInferred(ix.Orig, targs, res)
7171
}
@@ -171,7 +171,7 @@ func (check *Checker) callExpr(x *operand, call *ast.CallExpr) exprKind {
171171
assert(len(targs) == len(ix.Indices))
172172

173173
// check number of type arguments (got) vs number of type parameters (want)
174-
got, want := len(targs), sig.TParams().Len()
174+
got, want := len(targs), sig.TypeParams().Len()
175175
if got > want {
176176
check.errorf(ix.Indices[want], _Todo, "got %d type arguments but want %d", got, want)
177177
check.use(call.Args...)
@@ -205,7 +205,7 @@ func (check *Checker) callExpr(x *operand, call *ast.CallExpr) exprKind {
205205

206206
// if type inference failed, a parametrized result must be invalidated
207207
// (operands cannot have a parametrized type)
208-
if x.mode == value && sig.TParams().Len() > 0 && isParameterized(sig.TParams().list(), x.typ) {
208+
if x.mode == value && sig.TypeParams().Len() > 0 && isParameterized(sig.TypeParams().list(), x.typ) {
209209
x.mode = invalid
210210
}
211211

@@ -334,7 +334,7 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type
334334
}
335335

336336
// infer type arguments and instantiate signature if necessary
337-
if sig.TParams().Len() > 0 {
337+
if sig.TypeParams().Len() > 0 {
338338
if !check.allowVersion(check.pkg, 1, 18) {
339339
switch call.Fun.(type) {
340340
case *ast.IndexExpr, *ast.MultiIndexExpr:
@@ -346,21 +346,21 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type
346346
}
347347
// TODO(gri) provide position information for targs so we can feed
348348
// it to the instantiate call for better error reporting
349-
targs := check.infer(call, sig.TParams().list(), targs, sigParams, args, true)
349+
targs := check.infer(call, sig.TypeParams().list(), targs, sigParams, args, true)
350350
if targs == nil {
351351
return // error already reported
352352
}
353353

354354
// compute result signature
355355
rsig = check.instantiate(call.Pos(), sig, targs, nil).(*Signature)
356-
assert(rsig.TParams().Len() == 0) // signature is not generic anymore
356+
assert(rsig.TypeParams().Len() == 0) // signature is not generic anymore
357357
check.recordInferred(call, targs, rsig)
358358

359359
// Optimization: Only if the parameter list was adjusted do we
360360
// need to compute it from the adjusted list; otherwise we can
361361
// simply use the result signature's parameter list.
362362
if adjusted {
363-
sigParams = check.subst(call.Pos(), sigParams, makeSubstMap(sig.TParams().list(), targs), nil).(*Tuple)
363+
sigParams = check.subst(call.Pos(), sigParams, makeSubstMap(sig.TypeParams().list(), targs), nil).(*Tuple)
364364
} else {
365365
sigParams = rsig.params
366366
}

src/go/types/decl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -641,13 +641,13 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *Named) {
641641
named.underlying = under(named)
642642

643643
// If the RHS is a type parameter, it must be from this type declaration.
644-
if tpar, _ := named.underlying.(*TypeParam); tpar != nil && tparamIndex(named.TParams().list(), tpar) < 0 {
644+
if tpar, _ := named.underlying.(*TypeParam); tpar != nil && tparamIndex(named.TypeParams().list(), tpar) < 0 {
645645
check.errorf(tdecl.Type, _Todo, "cannot use function type parameter %s as RHS in type declaration", tpar)
646646
named.underlying = Typ[Invalid]
647647
}
648648
}
649649

650-
func (check *Checker) collectTypeParams(list *ast.FieldList) *TParamList {
650+
func (check *Checker) collectTypeParams(list *ast.FieldList) *TypeParamList {
651651
var tparams []*TypeParam
652652
// Declare type parameters up-front, with empty interface as type bound.
653653
// The scope of type parameters starts at the beginning of the type parameter

src/go/types/index.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (check *Checker) indexExpr(x *operand, e *typeparams.IndexExpr) (isFuncInst
3535
return false
3636

3737
case value:
38-
if sig := asSignature(x.typ); sig != nil && sig.TParams().Len() > 0 {
38+
if sig := asSignature(x.typ); sig != nil && sig.TypeParams().Len() > 0 {
3939
// function instantiation
4040
return true
4141
}

src/go/types/instantiate.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ func Instantiate(env *Environment, typ Type, targs []Type, validate bool) (Type,
3838
var tparams []*TypeParam
3939
switch t := typ.(type) {
4040
case *Named:
41-
tparams = t.TParams().list()
41+
tparams = t.TypeParams().list()
4242
case *Signature:
43-
tparams = t.TParams().list()
43+
tparams = t.TypeParams().list()
4444
}
4545
if i, err := (*Checker)(nil).verify(token.NoPos, tparams, targs); err != nil {
4646
return inst, ArgumentError{i, err}
@@ -80,9 +80,9 @@ func (check *Checker) instantiate(pos token.Pos, typ Type, targs []Type, posList
8080
var tparams []*TypeParam
8181
switch t := typ.(type) {
8282
case *Named:
83-
tparams = t.TParams().list()
83+
tparams = t.TypeParams().list()
8484
case *Signature:
85-
tparams = t.TParams().list()
85+
tparams = t.TypeParams().list()
8686
}
8787
// Avoid duplicate errors; instantiate will have complained if tparams
8888
// and targs do not have the same length.
@@ -127,7 +127,7 @@ func (check *Checker) instance(pos token.Pos, typ Type, targs []Type, env *Envir
127127
return named
128128

129129
case *Signature:
130-
tparams := t.TParams()
130+
tparams := t.TypeParams()
131131
if !check.validateTArgLen(pos, tparams.Len(), len(targs)) {
132132
return Typ[Invalid]
133133
}

src/go/types/lookup.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,10 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
319319
// both methods must have the same number of type parameters
320320
ftyp := f.typ.(*Signature)
321321
mtyp := m.typ.(*Signature)
322-
if ftyp.TParams().Len() != mtyp.TParams().Len() {
322+
if ftyp.TypeParams().Len() != mtyp.TypeParams().Len() {
323323
return m, f
324324
}
325-
if ftyp.TParams().Len() > 0 {
325+
if ftyp.TypeParams().Len() > 0 {
326326
panic("method with type parameters")
327327
}
328328

@@ -332,7 +332,7 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
332332
// TODO(gri) is this always correct? what about type bounds?
333333
// (Alternative is to rename/subst type parameters and compare.)
334334
u := newUnifier(true)
335-
u.x.init(ftyp.TParams().list())
335+
u.x.init(ftyp.TypeParams().list())
336336
if !u.unify(ftyp, mtyp) {
337337
return m, f
338338
}
@@ -371,10 +371,10 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
371371
// both methods must have the same number of type parameters
372372
ftyp := f.typ.(*Signature)
373373
mtyp := m.typ.(*Signature)
374-
if ftyp.TParams().Len() != mtyp.TParams().Len() {
374+
if ftyp.TypeParams().Len() != mtyp.TypeParams().Len() {
375375
return m, f
376376
}
377-
if ftyp.TParams().Len() > 0 {
377+
if ftyp.TypeParams().Len() > 0 {
378378
panic("method with type parameters")
379379
}
380380

@@ -385,7 +385,7 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
385385
// In order to compare the signatures, substitute the receiver
386386
// type parameters of ftyp with V's instantiation type arguments.
387387
// This lazily instantiates the signature of method f.
388-
if Vn != nil && Vn.TParams().Len() > 0 {
388+
if Vn != nil && Vn.TypeParams().Len() > 0 {
389389
// Be careful: The number of type arguments may not match
390390
// the number of receiver parameters. If so, an error was
391391
// reported earlier but the length discrepancy is still

src/go/types/named.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import (
1212
// A Named represents a named (defined) type.
1313
type Named struct {
1414
check *Checker
15-
info typeInfo // for cycle detection
16-
obj *TypeName // corresponding declared object for declared types; placeholder for instantiated types
17-
orig *Named // original, uninstantiated type
18-
fromRHS Type // type (on RHS of declaration) this *Named type is derived of (for cycle reporting)
19-
underlying Type // possibly a *Named during setup; never a *Named once set up completely
20-
instPos *token.Pos // position information for lazy instantiation, or nil
21-
tparams *TParamList // type parameters, or nil
22-
targs *TypeList // type arguments (after instantiation), or nil
23-
methods []*Func // methods declared for this type (not the method set of this type); signatures are type-checked lazily
15+
info typeInfo // for cycle detection
16+
obj *TypeName // corresponding declared object for declared types; placeholder for instantiated types
17+
orig *Named // original, uninstantiated type
18+
fromRHS Type // type (on RHS of declaration) this *Named type is derived of (for cycle reporting)
19+
underlying Type // possibly a *Named during setup; never a *Named once set up completely
20+
instPos *token.Pos // position information for lazy instantiation, or nil
21+
tparams *TypeParamList // type parameters, or nil
22+
targs *TypeList // type arguments (after instantiation), or nil
23+
methods []*Func // methods declared for this type (not the method set of this type); signatures are type-checked lazily
2424

2525
resolve func(*Named) ([]*TypeParam, Type, []*Func)
2626
once sync.Once
@@ -58,10 +58,10 @@ func (t *Named) load() *Named {
5858
// (necessary because types2 expects the receiver type for methods
5959
// on defined interface types to be the Named rather than the
6060
// underlying Interface), maybe it should just handle calling
61-
// SetTParams, SetUnderlying, and AddMethod instead? Those
61+
// SetTypeParams, SetUnderlying, and AddMethod instead? Those
6262
// methods would need to support reentrant calls though. It would
6363
// also make the API more future-proof towards further extensions
64-
// (like SetTParams).
64+
// (like SetTypeParams).
6565

6666
tparams, underlying, methods := t.resolve(t)
6767

@@ -78,7 +78,7 @@ func (t *Named) load() *Named {
7878
}
7979

8080
// newNamed is like NewNamed but with a *Checker receiver and additional orig argument.
81-
func (check *Checker) newNamed(obj *TypeName, orig *Named, underlying Type, tparams *TParamList, methods []*Func) *Named {
81+
func (check *Checker) newNamed(obj *TypeName, orig *Named, underlying Type, tparams *TypeParamList, methods []*Func) *Named {
8282
typ := &Named{check: check, obj: obj, orig: orig, fromRHS: underlying, underlying: underlying, tparams: tparams, methods: methods}
8383
if typ.orig == nil {
8484
typ.orig = typ
@@ -119,15 +119,15 @@ func (t *Named) _Orig() *Named { return t.orig }
119119
// TODO(gri) Come up with a better representation and API to distinguish
120120
// between parameterized instantiated and non-instantiated types.
121121

122-
// TParams returns the type parameters of the named type t, or nil.
122+
// TypeParams returns the type parameters of the named type t, or nil.
123123
// The result is non-nil for an (originally) parameterized type even if it is instantiated.
124-
func (t *Named) TParams() *TParamList { return t.load().tparams }
124+
func (t *Named) TypeParams() *TypeParamList { return t.load().tparams }
125125

126-
// SetTParams sets the type parameters of the named type t.
127-
func (t *Named) SetTParams(tparams []*TypeParam) { t.load().tparams = bindTParams(tparams) }
126+
// SetTypeParams sets the type parameters of the named type t.
127+
func (t *Named) SetTypeParams(tparams []*TypeParam) { t.load().tparams = bindTParams(tparams) }
128128

129-
// TArgs returns the type arguments used to instantiate the named type t.
130-
func (t *Named) TArgs() *TypeList { return t.targs }
129+
// TypeArgs returns the type arguments used to instantiate the named type t.
130+
func (t *Named) TypeArgs() *TypeList { return t.targs }
131131

132132
// NumMethods returns the number of explicit methods whose receiver is named type t.
133133
func (t *Named) NumMethods() int { return len(t.load().methods) }
@@ -245,8 +245,8 @@ func (n *Named) setUnderlying(typ Type) {
245245
func (n *Named) expand(env *Environment) *Named {
246246
if n.instPos != nil {
247247
// n must be loaded before instantiation, in order to have accurate
248-
// tparams. This is done implicitly by the call to n.TParams, but making it
249-
// explicit is harmless: load is idempotent.
248+
// tparams. This is done implicitly by the call to n.TypeParams, but making
249+
// it explicit is harmless: load is idempotent.
250250
n.load()
251251
var u Type
252252
if n.check.validateTArgLen(*n.instPos, n.tparams.Len(), n.targs.Len()) {
@@ -268,7 +268,7 @@ func (n *Named) expand(env *Environment) *Named {
268268
// shouldn't return that instance from expand.
269269
env.typeForHash(h, n)
270270
}
271-
u = n.check.subst(*n.instPos, n.orig.underlying, makeSubstMap(n.TParams().list(), n.targs.list()), env)
271+
u = n.check.subst(*n.instPos, n.orig.underlying, makeSubstMap(n.TypeParams().list(), n.targs.list()), env)
272272
} else {
273273
u = Typ[Invalid]
274274
}

src/go/types/object.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ func writeObject(buf *bytes.Buffer, obj Object, qf Qualifier) {
429429
if _, ok := typ.(*Basic); ok {
430430
return
431431
}
432-
if named, _ := typ.(*Named); named != nil && named.TParams().Len() > 0 {
433-
newTypeWriter(buf, qf).tParamList(named.TParams().list())
432+
if named, _ := typ.(*Named); named != nil && named.TypeParams().Len() > 0 {
433+
newTypeWriter(buf, qf).tParamList(named.TypeParams().list())
434434
}
435435
if tname.IsAlias() {
436436
buf.WriteString(" =")

src/go/types/predicates.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func isNamed(typ Type) bool {
2121
func isGeneric(typ Type) bool {
2222
// A parameterized type is only instantiated if it doesn't have an instantiation already.
2323
named, _ := typ.(*Named)
24-
return named != nil && named.obj != nil && named.targs == nil && named.TParams() != nil
24+
return named != nil && named.obj != nil && named.targs == nil && named.TypeParams() != nil
2525
}
2626

2727
func is(typ Type, what BasicInfo) bool {
@@ -220,7 +220,7 @@ func identical(x, y Type, cmpTags bool, p *ifacePair) bool {
220220
// parameter names.
221221
if y, ok := y.(*Signature); ok {
222222
return x.variadic == y.variadic &&
223-
identicalTParams(x.TParams().list(), y.TParams().list(), cmpTags, p) &&
223+
identicalTParams(x.TypeParams().list(), y.TypeParams().list(), cmpTags, p) &&
224224
identical(x.params, y.params, cmpTags, p) &&
225225
identical(x.results, y.results, cmpTags, p)
226226
}
@@ -305,8 +305,8 @@ func identical(x, y Type, cmpTags bool, p *ifacePair) bool {
305305
x.expand(nil)
306306
y.expand(nil)
307307

308-
xargs := x.TArgs().list()
309-
yargs := y.TArgs().list()
308+
xargs := x.TypeArgs().list()
309+
yargs := y.TypeArgs().list()
310310

311311
if len(xargs) != len(yargs) {
312312
return false

0 commit comments

Comments
 (0)