From 6f57139c7a6bc776f1335aca13d276d36343cf7c Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Wed, 21 Jul 2021 10:21:23 -0400 Subject: [PATCH] [dev.typeparams] go/types: set type parameter indices when they are bound It is invalid to use a type parameter for more than one type, so we can avoid passing the type parameter index to NewTypeParam and just set it when type parameters are bound to a type via SetTParams or during type checking. In order to enforce the correctness of this change, introduce a TypeParams type to represent a list of type parameters that have been associated with a type. For now, expose this new type as the API for type parameters, but this is of course not necessarily a final API. Allowing *TypeParams to be nil also decreases the size of Named and Signature, which is good as most instances of these types will not be parameterized. Change-Id: Ia1e39ba51edb05bb535eb5f41c34e9dd02d39c38 Reviewed-on: https://go-review.googlesource.com/c/go/+/336249 Trust: Robert Findley Run-TryBot: Robert Findley TryBot-Result: Go Bot Reviewed-by: Robert Griesemer --- src/go/types/api_test.go | 2 +- src/go/types/assignments.go | 2 +- src/go/types/builtins.go | 3 ++- src/go/types/call.go | 20 +++++++-------- src/go/types/decl.go | 8 +++--- src/go/types/index.go | 2 +- src/go/types/instance.go | 2 +- src/go/types/instantiate.go | 10 ++++---- src/go/types/lookup.go | 18 ++++++------- src/go/types/named.go | 16 ++++++------ src/go/types/object.go | 4 +-- src/go/types/predicates.go | 2 +- src/go/types/signature.go | 25 +++++++++++-------- src/go/types/sizeof_test.go | 4 +-- src/go/types/subst.go | 4 +-- src/go/types/typeparam.go | 50 ++++++++++++++++++++++++++++++++++--- src/go/types/typestring.go | 4 +-- 17 files changed, 111 insertions(+), 65 deletions(-) diff --git a/src/go/types/api_test.go b/src/go/types/api_test.go index 444cb44087..b2d532c4c8 100644 --- a/src/go/types/api_test.go +++ b/src/go/types/api_test.go @@ -1828,7 +1828,7 @@ func TestInstantiate(t *testing.T) { // type T should have one type parameter T := pkg.Scope().Lookup("T").Type().(*Named) - if n := len(T.TParams()); n != 1 { + if n := T.TParams().Len(); n != 1 { t.Fatalf("expected 1 type parameter; found %d", n) } diff --git a/src/go/types/assignments.go b/src/go/types/assignments.go index 18eae62184..595f426e10 100644 --- a/src/go/types/assignments.go +++ b/src/go/types/assignments.go @@ -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 && len(sig.tparams) > 0 { + if sig := asSignature(x.typ); sig != nil && sig.TParams().Len() > 0 { check.errorf(x, _Todo, "cannot use generic function %s without instantiation in %s", x, context) } diff --git a/src/go/types/builtins.go b/src/go/types/builtins.go index 2edf901165..b6fb36b185 100644 --- a/src/go/types/builtins.go +++ b/src/go/types/builtins.go @@ -806,7 +806,8 @@ func (check *Checker) applyTypeFunc(f func(Type) Type, x Type) Type { // type param is placed in the current package so export/import // works as expected. tpar := NewTypeName(token.NoPos, check.pkg, "", nil) - ptyp := check.NewTypeParam(tpar, tp.index, &emptyInterface) // assigns type to tpar as a side-effect + ptyp := check.NewTypeParam(tpar, &emptyInterface) // assigns type to tpar as a side-effect + ptyp.index = tp.index tsum := newUnion(rtypes, tildes) ptyp.bound = &Interface{complete: true, tset: &TypeSet{types: tsum}} diff --git a/src/go/types/call.go b/src/go/types/call.go index 9453b53c3a..96d0429af9 100644 --- a/src/go/types/call.go +++ b/src/go/types/call.go @@ -27,7 +27,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), len(sig.tparams) + got, want := len(targs), sig.TParams().Len() if got > want { check.errorf(ix.Indices[got-1], _Todo, "got %d type arguments but want %d", got, want) x.mode = invalid @@ -39,7 +39,7 @@ func (check *Checker) funcInst(x *operand, ix *typeparams.IndexExpr) { inferred := false if got < want { - targs = check.infer(ix.Orig, sig.tparams, targs, nil, nil, true) + targs = check.infer(ix.Orig, sig.TParams().list(), targs, nil, nil, true) if targs == nil { // error was already reported x.mode = invalid @@ -160,7 +160,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), len(sig.tparams) + got, want := len(targs), sig.TParams().Len() if got > want { check.errorf(ix.Indices[want], _Todo, "got %d type arguments but want %d", got, want) check.use(call.Args...) @@ -194,7 +194,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 && len(sig.tparams) > 0 && isParameterized(sig.tparams, x.typ) { + if x.mode == value && sig.TParams().Len() > 0 && isParameterized(sig.TParams().list(), x.typ) { x.mode = invalid } @@ -324,10 +324,10 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type } // infer type arguments and instantiate signature if necessary - if len(sig.tparams) > 0 { + if sig.TParams().Len() > 0 { // 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, targs, sigParams, args, true) + targs := check.infer(call, sig.TParams().list(), targs, sigParams, args, true) if targs == nil { return // error already reported } @@ -341,7 +341,7 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type // 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, targs)).(*Tuple) + sigParams = check.subst(call.Pos(), sigParams, makeSubstMap(sig.TParams().list(), targs)).(*Tuple) } else { sigParams = rsig.params } @@ -517,7 +517,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) { // the signature accordingly. // TODO(gri) factor this code out sig := m.typ.(*Signature) - if len(sig.rparams) > 0 { + if sig.RParams().Len() > 0 { // For inference to work, we must use the receiver type // matching the receiver in the actual method declaration. // If the method is embedded, the matching receiver is the @@ -545,7 +545,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) { // the receiver type arguments here, the receiver must be be otherwise invalid // and an error has been reported elsewhere. arg := operand{mode: variable, expr: x.expr, typ: recv} - targs := check.infer(m, sig.rparams, nil, NewTuple(sig.recv), []*operand{&arg}, false /* no error reporting */) + targs := check.infer(m, sig.RParams().list(), nil, NewTuple(sig.recv), []*operand{&arg}, false /* no error reporting */) if targs == nil { // We may reach here if there were other errors (see issue #40056). goto Error @@ -554,7 +554,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) { // (If we modify m, some tests will fail; possibly because the m is in use.) // TODO(gri) investigate and provide a correct explanation here copy := *m - copy.typ = check.subst(e.Pos(), m.typ, makeSubstMap(sig.rparams, targs)) + copy.typ = check.subst(e.Pos(), m.typ, makeSubstMap(sig.RParams().list(), targs)) obj = © } // TODO(gri) we also need to do substitution for parameterized interface methods diff --git a/src/go/types/decl.go b/src/go/types/decl.go index 1195104b59..be7753d9d1 100644 --- a/src/go/types/decl.go +++ b/src/go/types/decl.go @@ -625,13 +625,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, tpar) < 0 { + if tpar, _ := named.underlying.(*TypeParam); tpar != nil && tparamIndex(named.tparams.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) []*TypeName { +func (check *Checker) collectTypeParams(list *ast.FieldList) *TypeParams { var tparams []*TypeName // Declare type parameters up-front, with empty interface as type bound. // The scope of type parameters starts at the beginning of the type parameter @@ -655,13 +655,13 @@ func (check *Checker) collectTypeParams(list *ast.FieldList) []*TypeName { index += len(f.Names) } - return tparams + return bindTParams(tparams) } func (check *Checker) declareTypeParams(tparams []*TypeName, names []*ast.Ident) []*TypeName { for _, name := range names { tpar := NewTypeName(name.Pos(), check.pkg, name.Name, nil) - check.NewTypeParam(tpar, len(tparams), &emptyInterface) // assigns type to tpar as a side-effect + check.NewTypeParam(tpar, &emptyInterface) // assigns type to tpar as a side-effect check.declare(check.scope, name, tpar, check.scope.pos) // TODO(gri) check scope position tparams = append(tparams, tpar) } diff --git a/src/go/types/index.go b/src/go/types/index.go index b2a5a2e948..a49bc5519c 100644 --- a/src/go/types/index.go +++ b/src/go/types/index.go @@ -33,7 +33,7 @@ func (check *Checker) indexExpr(x *operand, e *typeparams.IndexExpr) (isFuncInst return false case value: - if sig := asSignature(x.typ); sig != nil && len(sig.tparams) > 0 { + if sig := asSignature(x.typ); sig != nil && sig.TParams().Len() > 0 { // function instantiation return true } diff --git a/src/go/types/instance.go b/src/go/types/instance.go index 9d31b42690..7e158ea352 100644 --- a/src/go/types/instance.go +++ b/src/go/types/instance.go @@ -24,7 +24,7 @@ type instance struct { func (n *Named) complete() { if n.instance != nil && len(n.targs) > 0 && n.underlying == nil { check := n.instance.check - inst := check.instantiate(n.instance.pos, n.orig.underlying, n.tparams, n.targs, n.instance.posList) + inst := check.instantiate(n.instance.pos, n.orig.underlying, n.TParams().list(), n.targs, n.instance.posList) n.underlying = inst n.fromRHS = inst n.methods = n.orig.methods diff --git a/src/go/types/instantiate.go b/src/go/types/instantiate.go index 14bbf2b12b..7e2f3173c3 100644 --- a/src/go/types/instantiate.go +++ b/src/go/types/instantiate.go @@ -28,9 +28,9 @@ func (check *Checker) Instantiate(pos token.Pos, typ Type, targs []Type, posList var tparams []*TypeName switch t := typ.(type) { case *Named: - tparams = t.TParams() + tparams = t.TParams().list() case *Signature: - tparams = t.tparams + tparams = t.TParams().list() defer func() { // If we had an unexpected failure somewhere don't panic below when // asserting res.(*Signature). Check for *Signature in case Typ[Invalid] @@ -109,9 +109,9 @@ func (check *Checker) InstantiateLazy(pos token.Pos, typ Type, targs []Type, pos if base == nil { panic(fmt.Sprintf("%v: cannot instantiate %v", pos, typ)) } - if verify && len(base.tparams) == len(targs) { + if verify && base.TParams().Len() == len(targs) { check.later(func() { - check.verify(pos, base.tparams, targs, posList) + check.verify(pos, base.tparams.list(), targs, posList) }) } h := instantiatedHash(base, targs) @@ -122,7 +122,7 @@ func (check *Checker) InstantiateLazy(pos token.Pos, typ Type, targs []Type, pos } tname := NewTypeName(pos, base.obj.pkg, base.obj.name, nil) - named := check.newNamed(tname, base, nil, base.tparams, base.methods) // methods are instantiated lazily + named := check.newNamed(tname, base, nil, base.TParams(), base.methods) // methods are instantiated lazily named.targs = targs named.instance = &instance{ check: check, diff --git a/src/go/types/lookup.go b/src/go/types/lookup.go index 304ae6e3c9..8b1d70a978 100644 --- a/src/go/types/lookup.go +++ b/src/go/types/lookup.go @@ -317,10 +317,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 len(ftyp.tparams) != len(mtyp.tparams) { + if ftyp.TParams().Len() != mtyp.TParams().Len() { return m, f } - if len(ftyp.tparams) > 0 { + if ftyp.TParams().Len() > 0 { panic("internal error: method with type parameters") } @@ -330,7 +330,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) + u.x.init(ftyp.TParams().list()) if !u.unify(ftyp, mtyp) { return m, f } @@ -373,10 +373,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 len(ftyp.tparams) != len(mtyp.tparams) { + if ftyp.TParams().Len() != mtyp.TParams().Len() { return m, f } - if len(ftyp.tparams) > 0 { + if ftyp.TParams().Len() > 0 { panic("internal error: method with type parameters") } @@ -387,17 +387,17 @@ 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 && len(Vn.TParams()) > 0 { + if Vn != nil && Vn.TParams().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 // here. Exit early in this case to prevent an assertion // failure in makeSubstMap. // TODO(gri) Can we avoid this check by fixing the lengths? - if len(ftyp.rparams) != len(Vn.targs) { + if len(ftyp.RParams().list()) != len(Vn.targs) { return } - ftyp = check.subst(token.NoPos, ftyp, makeSubstMap(ftyp.rparams, Vn.targs)).(*Signature) + ftyp = check.subst(token.NoPos, ftyp, makeSubstMap(ftyp.RParams().list(), Vn.targs)).(*Signature) } // If the methods have type parameters we don't care whether they @@ -406,7 +406,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.rparams) + u.x.init(ftyp.RParams().list()) if !u.unify(ftyp, mtyp) { return m, f } diff --git a/src/go/types/named.go b/src/go/types/named.go index a500f5663b..03af3fbc5a 100644 --- a/src/go/types/named.go +++ b/src/go/types/named.go @@ -16,7 +16,7 @@ type Named struct { 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 - tparams []*TypeName // type parameters, or nil + tparams *TypeParams // type parameters, or nil targs []Type // 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 @@ -56,7 +56,7 @@ func (t *Named) expand() *Named { panic("invalid underlying type") } - t.tparams = tparams + t.tparams = bindTParams(tparams) t.underlying = underlying t.methods = methods }) @@ -64,7 +64,7 @@ func (t *Named) expand() *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 []*TypeName, methods []*Func) *Named { +func (check *Checker) newNamed(obj *TypeName, orig *Named, underlying Type, tparams *TypeParams, methods []*Func) *Named { var inst *instance if check != nil { inst = &instance{ @@ -108,14 +108,14 @@ 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. +// TParams 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() []*TypeName { return t.expand().tparams } +func (t *Named) TParams() *TypeParams { return t.expand().tparams } -// _SetTParams sets the type parameters of the named type t. -func (t *Named) SetTParams(tparams []*TypeName) { t.expand().tparams = tparams } +// SetTParams sets the type parameters of the named type t. +func (t *Named) SetTParams(tparams []*TypeName) { t.expand().tparams = bindTParams(tparams) } -// _TArgs returns the type arguments after instantiation of the named type t, or nil if not instantiated. +// TArgs returns the type arguments after instantiation of the named type t, or nil if not instantiated. func (t *Named) TArgs() []Type { return t.targs } // SetTArgs sets the type arguments of the named type t. diff --git a/src/go/types/object.go b/src/go/types/object.go index 4ea2837ea7..7266623fbe 100644 --- a/src/go/types/object.go +++ b/src/go/types/object.go @@ -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 && len(named.tparams) > 0 { - writeTParamList(buf, named.tparams, qf, nil) + if named, _ := typ.(*Named); named != nil && named.TParams().Len() > 0 { + writeTParamList(buf, named.TParams().list(), qf, nil) } if tname.IsAlias() { buf.WriteString(" =") diff --git a/src/go/types/predicates.go b/src/go/types/predicates.go index ce350f4470..181e2fcfc5 100644 --- a/src/go/types/predicates.go +++ b/src/go/types/predicates.go @@ -242,7 +242,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, y.tparams, cmpTags, p) && + identicalTParams(x.TParams().list(), y.TParams().list(), cmpTags, p) && identical(x.params, y.params, cmpTags, p) && identical(x.results, y.results, cmpTags, p) } diff --git a/src/go/types/signature.go b/src/go/types/signature.go index da01ec801a..5a69bb17b5 100644 --- a/src/go/types/signature.go +++ b/src/go/types/signature.go @@ -21,8 +21,8 @@ type Signature struct { // and store it in the Func Object) because when type-checking a function // literal we call the general type checker which returns a general Type. // We then unpack the *Signature and use the scope for the literal body. - rparams []*TypeName // receiver type parameters from left to right, or nil - tparams []*TypeName // type parameters from left to right, or nil + rparams *TypeParams // receiver type parameters from left to right, or nil + tparams *TypeParams // type parameters from left to right, or nil scope *Scope // function scope, present for package-local signatures recv *Var // nil if not a method params *Tuple // (incoming) parameters from left to right; or nil @@ -56,13 +56,16 @@ func NewSignature(recv *Var, params, results *Tuple, variadic bool) *Signature { func (s *Signature) Recv() *Var { return s.recv } // TParams returns the type parameters of signature s, or nil. -func (s *Signature) TParams() []*TypeName { return s.tparams } +func (s *Signature) TParams() *TypeParams { return s.tparams } // SetTParams sets the type parameters of signature s. -func (s *Signature) SetTParams(tparams []*TypeName) { s.tparams = tparams } +func (s *Signature) SetTParams(tparams []*TypeName) { s.tparams = bindTParams(tparams) } + +// RParams returns the receiver type parameters of signature s, or nil. +func (s *Signature) RParams() *TypeParams { return s.rparams } // SetRParams sets the receiver type params of signature s. -func (s *Signature) SetRParams(rparams []*TypeName) { s.rparams = rparams } +func (s *Signature) SetRParams(rparams []*TypeName) { s.rparams = bindTParams(rparams) } // Params returns the parameters of signature s, or nil. func (s *Signature) Params() *Tuple { return s.params } @@ -115,7 +118,7 @@ func (check *Checker) funcType(sig *Signature, recvPar *ast.FieldList, ftyp *ast // blank identifiers were found => use rewritten receiver type recvTyp = isubst(recvPar.List[0].Type, smap) } - sig.rparams = check.declareTypeParams(nil, rparams) + sig.rparams = bindTParams(check.declareTypeParams(nil, rparams)) // determine receiver type to get its type parameters // and the respective type parameter bounds var recvTParams []*TypeName @@ -125,19 +128,19 @@ func (check *Checker) funcType(sig *Signature, recvPar *ast.FieldList, ftyp *ast // again when we type-check the signature. // TODO(gri) maybe the receiver should be marked as invalid instead? if recv := asNamed(check.genericType(rname, false)); recv != nil { - recvTParams = recv.TParams() + recvTParams = recv.TParams().list() } } // provide type parameter bounds // - only do this if we have the right number (otherwise an error is reported elsewhere) - if len(sig.rparams) == len(recvTParams) { + if sig.RParams().Len() == len(recvTParams) { // We have a list of *TypeNames but we need a list of Types. - list := make([]Type, len(sig.rparams)) - for i, t := range sig.rparams { + list := make([]Type, sig.RParams().Len()) + for i, t := range sig.RParams().list() { list[i] = t.typ } smap := makeSubstMap(recvTParams, list) - for i, tname := range sig.rparams { + for i, tname := range sig.RParams().list() { bound := recvTParams[i].typ.(*TypeParam).bound // bound is (possibly) parameterized in the context of the // receiver type declaration. Substitute parameters for the diff --git a/src/go/types/sizeof_test.go b/src/go/types/sizeof_test.go index fc548f7c58..29e298103b 100644 --- a/src/go/types/sizeof_test.go +++ b/src/go/types/sizeof_test.go @@ -25,12 +25,12 @@ func TestSizeof(t *testing.T) { {Struct{}, 24, 48}, {Pointer{}, 8, 16}, {Tuple{}, 12, 24}, - {Signature{}, 44, 88}, + {Signature{}, 28, 56}, {Union{}, 24, 48}, {Interface{}, 40, 80}, {Map{}, 16, 32}, {Chan{}, 12, 24}, - {Named{}, 84, 160}, + {Named{}, 76, 144}, {TypeParam{}, 28, 48}, {top{}, 0, 0}, diff --git a/src/go/types/subst.go b/src/go/types/subst.go index 42be508cd9..197d79b6a8 100644 --- a/src/go/types/subst.go +++ b/src/go/types/subst.go @@ -203,7 +203,7 @@ func (subst *subster) typ(typ Type) Type { if len(t.targs) > 0 { // already instantiated dump(">>> %s already instantiated", t) - assert(len(t.targs) == len(t.TParams())) + assert(len(t.targs) == t.TParams().Len()) // For each (existing) type argument targ, determine if it needs // to be substituted; i.e., if it is or contains a type parameter // that has a type argument for it. @@ -213,7 +213,7 @@ func (subst *subster) typ(typ Type) Type { if newTarg != targ { dump(">>> substituted %d targ %s => %s", i, targ, newTarg) if newTargs == nil { - newTargs = make([]Type, len(t.TParams())) + newTargs = make([]Type, t.TParams().Len()) copy(newTargs, t.targs) } newTargs[i] = newTarg diff --git a/src/go/types/typeparam.go b/src/go/types/typeparam.go index bb5b28cdf8..8c18b52a9a 100644 --- a/src/go/types/typeparam.go +++ b/src/go/types/typeparam.go @@ -28,15 +28,19 @@ type TypeParam struct { bound Type // *Named or *Interface; underlying type is always *Interface } -// NewTypeParam returns a new TypeParam. bound can be nil (and set later). -func (check *Checker) NewTypeParam(obj *TypeName, index int, bound Type) *TypeParam { +// NewTypeParam returns a new TypeParam. Type parameters may be set on a Named +// or Signature type by calling SetTParams. Setting a type parameter on more +// than one type will result in a panic. +// +// The bound argument can be nil, and set later via SetBound. +func (check *Checker) NewTypeParam(obj *TypeName, bound Type) *TypeParam { // Always increment lastID, even if it is not used. id := nextID() if check != nil { check.nextID++ id = check.nextID } - typ := &TypeParam{check: check, id: id, obj: obj, index: index, bound: bound} + typ := &TypeParam{check: check, id: id, obj: obj, index: -1, bound: bound} if obj.typ == nil { obj.typ = typ } @@ -56,6 +60,8 @@ func (t *TypeParam) _SetId(id uint64) { t.id = id } +// TODO(rfindley): document the Bound and SetBound methods. + func (t *TypeParam) Bound() *Interface { // we may not have an interface (error reported elsewhere) iface, _ := under(t.bound).(*Interface) @@ -72,7 +78,7 @@ func (t *TypeParam) Bound() *Interface { return iface } -func (t *TypeParam) _SetBound(bound Type) { +func (t *TypeParam) SetBound(bound Type) { if bound == nil { panic("internal error: bound must not be nil") } @@ -82,6 +88,42 @@ func (t *TypeParam) _SetBound(bound Type) { func (t *TypeParam) Underlying() Type { return t } func (t *TypeParam) String() string { return TypeString(t, nil) } +// TypeParams holds a list of type parameters bound to a type. +type TypeParams struct{ tparams []*TypeName } + +// Len returns the number of type parameters in the list. +// It is safe to call on a nil receiver. +func (tps *TypeParams) Len() int { + return len(tps.list()) +} + +// At returns the i'th type parameter in the list. +// It is safe to call on a nil receiver. +func (tps *TypeParams) At(i int) *TypeName { + return tps.list()[i] +} + +func (tps *TypeParams) list() []*TypeName { + if tps == nil { + return nil + } + return tps.tparams +} + +func bindTParams(list []*TypeName) *TypeParams { + if len(list) == 0 { + return nil + } + for i, tp := range list { + typ := tp.Type().(*TypeParam) + if typ.index >= 0 { + panic("internal error: type parameter bound more than once") + } + typ.index = i + } + return &TypeParams{tparams: list} +} + // ---------------------------------------------------------------------------- // Implementation diff --git a/src/go/types/typestring.go b/src/go/types/typestring.go index ef3808230a..18c436e3ef 100644 --- a/src/go/types/typestring.go +++ b/src/go/types/typestring.go @@ -278,7 +278,7 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) { buf.WriteByte(']') } else if t.TParams() != nil { // parameterized type - writeTParamList(buf, t.TParams(), qf, visited) + writeTParamList(buf, t.TParams().list(), qf, visited) } case *TypeParam: @@ -425,7 +425,7 @@ func WriteSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier) { func writeSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier, visited []Type) { if sig.tparams != nil { - writeTParamList(buf, sig.tparams, qf, visited) + writeTParamList(buf, sig.TParams().list(), qf, visited) } writeTuple(buf, sig.params, sig.variadic, qf, visited) -- 2.50.0