[dev.typeparams] go/types: use the TParams API consistently

Even internally to the type checker, we should use the TParams and
RParams methods instead of accessing fields directly, as TParams may be
lazily expanded, and in the future we may want to pack receiver and
function type parameters into a single field on Signature.

We should also not differentiate a nil *TParamList from an empty
*TParamList.

Change-Id: I85c616e6c708a89b6a5eb1e69fe0b014276eda90
Reviewed-on: https://go-review.googlesource.com/c/go/+/336251
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Rob Findley 2021-07-21 11:26:00 -04:00 committed by Robert Findley
parent 6f57139c7a
commit 5ba06495c1
4 changed files with 6 additions and 6 deletions

View file

@ -61,7 +61,7 @@ func (check *Checker) funcInst(x *operand, ix *typeparams.IndexExpr) {
// instantiate function signature // instantiate function signature
res := check.Instantiate(x.Pos(), sig, targs, poslist, true).(*Signature) res := check.Instantiate(x.Pos(), sig, targs, poslist, true).(*Signature)
assert(res.tparams == nil) // signature is not generic anymore assert(res.TParams().Len() == 0) // signature is not generic anymore
if inferred { if inferred {
check.recordInferred(ix.Orig, targs, res) check.recordInferred(ix.Orig, targs, res)
} }
@ -334,7 +334,7 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type
// compute result signature // compute result signature
rsig = check.Instantiate(call.Pos(), sig, targs, nil, true).(*Signature) rsig = check.Instantiate(call.Pos(), sig, targs, nil, true).(*Signature)
assert(rsig.tparams == nil) // signature is not generic anymore assert(rsig.TParams().Len() == 0) // signature is not generic anymore
check.recordInferred(call, targs, rsig) check.recordInferred(call, targs, rsig)
// Optimization: Only if the parameter list was adjusted do we // Optimization: Only if the parameter list was adjusted do we

View file

@ -625,7 +625,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *Named) {
named.underlying = under(named) named.underlying = under(named)
// If the RHS is a type parameter, it must be from this type declaration. // 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.TParams().list(), tpar) < 0 {
check.errorf(tdecl.Type, _Todo, "cannot use function type parameter %s as RHS in type declaration", tpar) check.errorf(tdecl.Type, _Todo, "cannot use function type parameter %s as RHS in type declaration", tpar)
named.underlying = Typ[Invalid] named.underlying = Typ[Invalid]
} }

View file

@ -193,7 +193,7 @@ func (subst *subster) typ(typ Type) Type {
} }
} }
if t.TParams() == nil { if t.TParams().Len() == 0 {
dump(">>> %s is not parameterized", t) dump(">>> %s is not parameterized", t)
return t // type is not parameterized return t // type is not parameterized
} }

View file

@ -276,7 +276,7 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
buf.WriteByte('[') buf.WriteByte('[')
writeTypeList(buf, t.targs, qf, visited) writeTypeList(buf, t.targs, qf, visited)
buf.WriteByte(']') buf.WriteByte(']')
} else if t.TParams() != nil { } else if t.TParams().Len() != 0 {
// parameterized type // parameterized type
writeTParamList(buf, t.TParams().list(), qf, visited) writeTParamList(buf, t.TParams().list(), qf, visited)
} }
@ -424,7 +424,7 @@ func WriteSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier) {
} }
func writeSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier, visited []Type) { func writeSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier, visited []Type) {
if sig.tparams != nil { if sig.TParams().Len() != 0 {
writeTParamList(buf, sig.TParams().list(), qf, visited) writeTParamList(buf, sig.TParams().list(), qf, visited)
} }