Skip to content

Commit

Permalink
go/types: spell out 'Type' in type parameter APIs
Browse files Browse the repository at this point in the history
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 <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
  • Loading branch information
findleyr committed Sep 8, 2021
1 parent 12eb733 commit d419f9c
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 85 deletions.
6 changes: 3 additions & 3 deletions src/go/internal/gcimporter/iimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (r *importReader) obj(name string) {
tparams = r.tparamList()
}
sig := r.signature(nil)
sig.SetTParams(tparams)
sig.SetTypeParams(tparams)
r.declare(types.NewFunc(pos, r.currPkg, name, sig))

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

underlying := r.p.typAt(r.uint64(), named).Underlying()
Expand All @@ -333,7 +333,7 @@ func (r *importReader) obj(name string) {
// If the receiver has any targs, set those as the
// rparams of the method (since those are the
// typeparams being used in the method sig/body).
targs := baseType(msig.Recv().Type()).TArgs()
targs := baseType(msig.Recv().Type()).TypeArgs()
if targs.Len() > 0 {
rparams := make([]*types.TypeParam, targs.Len())
for i := range rparams {
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1851,7 +1851,7 @@ func TestInstantiate(t *testing.T) {

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

Expand Down
2 changes: 1 addition & 1 deletion src/go/types/assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
}

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

Expand Down
18 changes: 9 additions & 9 deletions src/go/types/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (check *Checker) funcInst(x *operand, ix *typeparams.IndexExpr) {

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

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

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

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

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

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

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

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

// Optimization: Only if the parameter list was adjusted do we
// need to compute it from the adjusted list; otherwise we can
// simply use the result signature's parameter list.
if adjusted {
sigParams = check.subst(call.Pos(), sigParams, makeSubstMap(sig.TParams().list(), targs), nil).(*Tuple)
sigParams = check.subst(call.Pos(), sigParams, makeSubstMap(sig.TypeParams().list(), targs), nil).(*Tuple)
} else {
sigParams = rsig.params
}
Expand Down
4 changes: 2 additions & 2 deletions src/go/types/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,13 +641,13 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *Named) {
named.underlying = under(named)

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

func (check *Checker) collectTypeParams(list *ast.FieldList) *TParamList {
func (check *Checker) collectTypeParams(list *ast.FieldList) *TypeParamList {
var tparams []*TypeParam
// Declare type parameters up-front, with empty interface as type bound.
// The scope of type parameters starts at the beginning of the type parameter
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (check *Checker) indexExpr(x *operand, e *typeparams.IndexExpr) (isFuncInst
return false

case value:
if sig := asSignature(x.typ); sig != nil && sig.TParams().Len() > 0 {
if sig := asSignature(x.typ); sig != nil && sig.TypeParams().Len() > 0 {
// function instantiation
return true
}
Expand Down
10 changes: 5 additions & 5 deletions src/go/types/instantiate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func Instantiate(env *Environment, typ Type, targs []Type, validate bool) (Type,
var tparams []*TypeParam
switch t := typ.(type) {
case *Named:
tparams = t.TParams().list()
tparams = t.TypeParams().list()
case *Signature:
tparams = t.TParams().list()
tparams = t.TypeParams().list()
}
if i, err := (*Checker)(nil).verify(token.NoPos, tparams, targs); err != nil {
return inst, ArgumentError{i, err}
Expand Down Expand Up @@ -80,9 +80,9 @@ func (check *Checker) instantiate(pos token.Pos, typ Type, targs []Type, posList
var tparams []*TypeParam
switch t := typ.(type) {
case *Named:
tparams = t.TParams().list()
tparams = t.TypeParams().list()
case *Signature:
tparams = t.TParams().list()
tparams = t.TypeParams().list()
}
// Avoid duplicate errors; instantiate will have complained if tparams
// and targs do not have the same length.
Expand Down Expand Up @@ -127,7 +127,7 @@ func (check *Checker) instance(pos token.Pos, typ Type, targs []Type, env *Envir
return named

case *Signature:
tparams := t.TParams()
tparams := t.TypeParams()
if !check.validateTArgLen(pos, tparams.Len(), len(targs)) {
return Typ[Invalid]
}
Expand Down
12 changes: 6 additions & 6 deletions src/go/types/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,10 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
// both methods must have the same number of type parameters
ftyp := f.typ.(*Signature)
mtyp := m.typ.(*Signature)
if ftyp.TParams().Len() != mtyp.TParams().Len() {
if ftyp.TypeParams().Len() != mtyp.TypeParams().Len() {
return m, f
}
if ftyp.TParams().Len() > 0 {
if ftyp.TypeParams().Len() > 0 {
panic("method with type parameters")
}

Expand All @@ -332,7 +332,7 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
// TODO(gri) is this always correct? what about type bounds?
// (Alternative is to rename/subst type parameters and compare.)
u := newUnifier(true)
u.x.init(ftyp.TParams().list())
u.x.init(ftyp.TypeParams().list())
if !u.unify(ftyp, mtyp) {
return m, f
}
Expand Down Expand Up @@ -371,10 +371,10 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
// both methods must have the same number of type parameters
ftyp := f.typ.(*Signature)
mtyp := m.typ.(*Signature)
if ftyp.TParams().Len() != mtyp.TParams().Len() {
if ftyp.TypeParams().Len() != mtyp.TypeParams().Len() {
return m, f
}
if ftyp.TParams().Len() > 0 {
if ftyp.TypeParams().Len() > 0 {
panic("method with type parameters")
}

Expand All @@ -385,7 +385,7 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
// In order to compare the signatures, substitute the receiver
// type parameters of ftyp with V's instantiation type arguments.
// This lazily instantiates the signature of method f.
if Vn != nil && Vn.TParams().Len() > 0 {
if Vn != nil && Vn.TypeParams().Len() > 0 {
// Be careful: The number of type arguments may not match
// the number of receiver parameters. If so, an error was
// reported earlier but the length discrepancy is still
Expand Down
42 changes: 21 additions & 21 deletions src/go/types/named.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
// A Named represents a named (defined) type.
type Named struct {
check *Checker
info typeInfo // for cycle detection
obj *TypeName // corresponding declared object for declared types; placeholder for instantiated types
orig *Named // original, uninstantiated type
fromRHS Type // type (on RHS of declaration) this *Named type is derived of (for cycle reporting)
underlying Type // possibly a *Named during setup; never a *Named once set up completely
instPos *token.Pos // position information for lazy instantiation, or nil
tparams *TParamList // type parameters, or nil
targs *TypeList // type arguments (after instantiation), or nil
methods []*Func // methods declared for this type (not the method set of this type); signatures are type-checked lazily
info typeInfo // for cycle detection
obj *TypeName // corresponding declared object for declared types; placeholder for instantiated types
orig *Named // original, uninstantiated type
fromRHS Type // type (on RHS of declaration) this *Named type is derived of (for cycle reporting)
underlying Type // possibly a *Named during setup; never a *Named once set up completely
instPos *token.Pos // position information for lazy instantiation, or nil
tparams *TypeParamList // type parameters, or nil
targs *TypeList // type arguments (after instantiation), or nil
methods []*Func // methods declared for this type (not the method set of this type); signatures are type-checked lazily

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

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

Expand All @@ -78,7 +78,7 @@ func (t *Named) load() *Named {
}

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

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

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

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

// NumMethods returns the number of explicit methods whose receiver is named type t.
func (t *Named) NumMethods() int { return len(t.load().methods) }
Expand Down Expand Up @@ -245,8 +245,8 @@ func (n *Named) setUnderlying(typ Type) {
func (n *Named) expand(env *Environment) *Named {
if n.instPos != nil {
// n must be loaded before instantiation, in order to have accurate
// tparams. This is done implicitly by the call to n.TParams, but making it
// explicit is harmless: load is idempotent.
// tparams. This is done implicitly by the call to n.TypeParams, but making
// it explicit is harmless: load is idempotent.
n.load()
var u Type
if n.check.validateTArgLen(*n.instPos, n.tparams.Len(), n.targs.Len()) {
Expand All @@ -268,7 +268,7 @@ func (n *Named) expand(env *Environment) *Named {
// shouldn't return that instance from expand.
env.typeForHash(h, n)
}
u = n.check.subst(*n.instPos, n.orig.underlying, makeSubstMap(n.TParams().list(), n.targs.list()), env)
u = n.check.subst(*n.instPos, n.orig.underlying, makeSubstMap(n.TypeParams().list(), n.targs.list()), env)
} else {
u = Typ[Invalid]
}
Expand Down
4 changes: 2 additions & 2 deletions src/go/types/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ func writeObject(buf *bytes.Buffer, obj Object, qf Qualifier) {
if _, ok := typ.(*Basic); ok {
return
}
if named, _ := typ.(*Named); named != nil && named.TParams().Len() > 0 {
newTypeWriter(buf, qf).tParamList(named.TParams().list())
if named, _ := typ.(*Named); named != nil && named.TypeParams().Len() > 0 {
newTypeWriter(buf, qf).tParamList(named.TypeParams().list())
}
if tname.IsAlias() {
buf.WriteString(" =")
Expand Down
8 changes: 4 additions & 4 deletions src/go/types/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func isNamed(typ Type) bool {
func isGeneric(typ Type) bool {
// A parameterized type is only instantiated if it doesn't have an instantiation already.
named, _ := typ.(*Named)
return named != nil && named.obj != nil && named.targs == nil && named.TParams() != nil
return named != nil && named.obj != nil && named.targs == nil && named.TypeParams() != nil
}

func is(typ Type, what BasicInfo) bool {
Expand Down Expand Up @@ -220,7 +220,7 @@ func identical(x, y Type, cmpTags bool, p *ifacePair) bool {
// parameter names.
if y, ok := y.(*Signature); ok {
return x.variadic == y.variadic &&
identicalTParams(x.TParams().list(), y.TParams().list(), cmpTags, p) &&
identicalTParams(x.TypeParams().list(), y.TypeParams().list(), cmpTags, p) &&
identical(x.params, y.params, cmpTags, p) &&
identical(x.results, y.results, cmpTags, p)
}
Expand Down Expand Up @@ -305,8 +305,8 @@ func identical(x, y Type, cmpTags bool, p *ifacePair) bool {
x.expand(nil)
y.expand(nil)

xargs := x.TArgs().list()
yargs := y.TArgs().list()
xargs := x.TypeArgs().list()
yargs := y.TypeArgs().list()

if len(xargs) != len(yargs) {
return false
Expand Down
Loading

0 comments on commit d419f9c

Please sign in to comment.