From b96f1b94191f7a404599e61b8cdd8ac010805545 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Fri, 16 Jul 2021 14:43:30 -0400 Subject: [PATCH] [dev.typeparams] go/types: add some missing APIs for the importer This is a partial port of CL 319930, containing only changes to go/types. Importer changes will be made in a separate CL. The TypeParams APIs are left unexported for now; they will be exported when they are needed. Change-Id: I74bd246d4c174cb38f8360d921c733fa03900eab Reviewed-on: https://go-review.googlesource.com/c/go/+/335143 Trust: Robert Findley Run-TryBot: Robert Findley TryBot-Result: Go Bot Reviewed-by: Robert Griesemer --- src/go/types/api_test.go | 12 ++++++------ src/go/types/signature.go | 7 +++++-- src/go/types/typeparam.go | 13 ++++++++++++- src/go/types/typestring.go | 7 +++++++ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/go/types/api_test.go b/src/go/types/api_test.go index 9ca24db1de6..0a91f139fe0 100644 --- a/src/go/types/api_test.go +++ b/src/go/types/api_test.go @@ -331,16 +331,16 @@ func TestTypesInfo(t *testing.T) { {broken + `x5; func _() { var x map[string][...]int; x = map[string][...]int{"": {1,2,3}} }`, `x`, `map[string][-1]int`}, // parameterized functions - {genericPkg + `p0; func f[T any](T); var _ = f[int]`, `f`, `func[T₁ interface{}](T₁)`}, + {genericPkg + `p0; func f[T any](T); var _ = f[int]`, `f`, `func[generic_p0.T₁ interface{}](generic_p0.T₁)`}, {genericPkg + `p1; func f[T any](T); var _ = f[int]`, `f[int]`, `func(int)`}, - {genericPkg + `p2; func f[T any](T); func _() { f(42) }`, `f`, `func[T₁ interface{}](T₁)`}, + {genericPkg + `p2; func f[T any](T); func _() { f(42) }`, `f`, `func[generic_p2.T₁ interface{}](generic_p2.T₁)`}, {genericPkg + `p3; func f[T any](T); func _() { f(42) }`, `f(42)`, `()`}, // type parameters {genericPkg + `t0; type t[] int; var _ t`, `t`, `generic_t0.t`}, // t[] is a syntax error that is ignored in this test in favor of t - {genericPkg + `t1; type t[P any] int; var _ t[int]`, `t`, `generic_t1.t[P₁ interface{}]`}, - {genericPkg + `t2; type t[P interface{}] int; var _ t[int]`, `t`, `generic_t2.t[P₁ interface{}]`}, - {genericPkg + `t3; type t[P, Q interface{}] int; var _ t[int, int]`, `t`, `generic_t3.t[P₁, Q₂ interface{}]`}, + {genericPkg + `t1; type t[P any] int; var _ t[int]`, `t`, `generic_t1.t[generic_t1.P₁ interface{}]`}, + {genericPkg + `t2; type t[P interface{}] int; var _ t[int]`, `t`, `generic_t2.t[generic_t2.P₁ interface{}]`}, + {genericPkg + `t3; type t[P, Q interface{}] int; var _ t[int, int]`, `t`, `generic_t3.t[generic_t3.P₁, generic_t3.Q₂ interface{}]`}, // TODO (rFindley): compare with types2, which resolves the type broken_t4.t[P₁, Q₂ interface{m()}] here {broken + `t4; type t[P, Q interface{ m() }] int; var _ t[int, int]`, `t`, `broken_t4.t`}, @@ -349,7 +349,7 @@ func TestTypesInfo(t *testing.T) { {genericPkg + `g0; type t[P any] int; var x struct{ f t[int] }; var _ = x.f`, `x.f`, `generic_g0.t[int]`}, // issue 45096 - {genericPkg + `issue45096; func _[T interface{ ~int8 | ~int16 | ~int32 }](x T) { _ = x < 0 }`, `0`, `T₁`}, + {genericPkg + `issue45096; func _[T interface{ ~int8 | ~int16 | ~int32 }](x T) { _ = x < 0 }`, `0`, `generic_issue45096.T₁`}, } for _, test := range tests { diff --git a/src/go/types/signature.go b/src/go/types/signature.go index 665514587ee..85a735120f4 100644 --- a/src/go/types/signature.go +++ b/src/go/types/signature.go @@ -55,12 +55,15 @@ func NewSignature(recv *Var, params, results *Tuple, variadic bool) *Signature { // contain methods whose receiver type is a different interface. func (s *Signature) Recv() *Var { return s.recv } -// _TParams returns the type parameters of signature s, or nil. +// TParams returns the type parameters of signature s, or nil. func (s *Signature) TParams() []*TypeName { return s.tparams } -// _SetTParams sets the type parameters of signature s. +// SetTParams sets the type parameters of signature s. func (s *Signature) SetTParams(tparams []*TypeName) { s.tparams = tparams } +// SetRParams sets the receiver type params of signature s. +func (s *Signature) SetRParams(rparams []*TypeName) { s.rparams = rparams } + // Params returns the parameters of signature s, or nil. func (s *Signature) Params() *Tuple { return s.params } diff --git a/src/go/types/typeparam.go b/src/go/types/typeparam.go index 92b048f2478..89ac3ecf38f 100644 --- a/src/go/types/typeparam.go +++ b/src/go/types/typeparam.go @@ -50,7 +50,18 @@ func (check *Checker) newTypeParam(obj *TypeName, index int, bound Type) *TypePa return typ } -// TODO(rfindley): types2 to has Index and SetID. Should we add them here? +// TODO(rfindley): remove or export these placeholder APIs. + +// Index returns the index of the type param within its param list. +func (t *TypeParam) _Index() int { + return t.index +} + +// SetId sets the unique id of a type param. Should only be used for type params +// in imported generic types. +func (t *TypeParam) _SetId(id uint64) { + t.id = id +} func (t *TypeParam) Bound() *Interface { // we may not have an interface (error reported elsewhere) diff --git a/src/go/types/typestring.go b/src/go/types/typestring.go index 4e73030613c..cba678588a9 100644 --- a/src/go/types/typestring.go +++ b/src/go/types/typestring.go @@ -284,6 +284,13 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) { case *TypeParam: s := "?" if t.obj != nil { + // Optionally write out package for typeparams (like Named). + // TODO(rfindley): this is required for import/export, so + // we maybe need a separate function that won't be changed + // for debugging purposes. + if t.obj.pkg != nil { + writePackage(buf, t.obj.pkg, qf) + } s = t.obj.name } buf.WriteString(s + subscript(t.id))