go/types, types2: add Alias.{TypeParams, SetTypeParams, TypeArgs, Origin}

Fixes #67143.

Change-Id: I8bf9c2559f95d3d6a40874454208ae074b68875c
Reviewed-on: https://go-review.googlesource.com/c/go/+/583757
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Robert Griesemer 2024-05-07 09:38:28 -07:00 committed by Gopher Robot
parent 36b06c3d2f
commit bf279b71e2
4 changed files with 56 additions and 2 deletions

4
api/next/67143.txt Normal file
View file

@ -0,0 +1,4 @@
pkg go/types, method (*Alias) Origin() *Alias #67143
pkg go/types, method (*Alias) SetTypeParams([]*TypeParam) #67143
pkg go/types, method (*Alias) TypeArgs() *TypeList #67143
pkg go/types, method (*Alias) TypeParams() *TypeParamList #67143

View file

@ -0,0 +1,2 @@
The methods [Alias.Origin], [Alias.SetTypeParams], [Alias.TypeParams],
and [Alias.TypeArgs] have been added. They are needed for generic alias types.

View file

@ -14,7 +14,9 @@ import "fmt"
// which points directly to the actual (aliased) type.
type Alias struct {
obj *TypeName // corresponding declared alias object
orig *Alias // original, uninstantiated alias
tparams *TypeParamList // type parameters, or nil
targs *TypeList // type arguments, or nil
fromRHS Type // RHS of type alias declaration; may be an alias
actual Type // actual (aliased) type; never an alias
}
@ -38,6 +40,25 @@ func (a *Alias) String() string { return TypeString(a, nil) }
// [underlying type]: https://go.dev/ref/spec#Underlying_types.
func (a *Alias) Underlying() Type { return unalias(a).Underlying() }
// Origin returns the generic Alias type of which a is an instance.
// If a is not an instance of a generic alias, Origin returns a.
func (a *Alias) Origin() *Alias { return a.orig }
// TypeParams returns the type parameters of the alias type a, or nil.
// A generic Alias and its instances have the same type parameters.
func (a *Alias) TypeParams() *TypeParamList { return a.tparams }
// SetTypeParams sets the type parameters of the alias type a.
// The alias a must not have type arguments.
func (a *Alias) SetTypeParams(tparams []*TypeParam) {
assert(a.targs == nil)
a.tparams = bindTParams(tparams)
}
// TypeArgs returns the type arguments used to instantiate the Alias type.
// If a is not an instance of a generic alias, the result is nil.
func (a *Alias) TypeArgs() *TypeList { return a.targs }
// Rhs returns the type R on the right-hand side of an alias
// declaration "type A = R", which may be another alias.
func (a *Alias) Rhs() Type { return a.fromRHS }
@ -88,7 +109,10 @@ func asNamed(t Type) *Named {
// rhs must not be nil.
func (check *Checker) newAlias(obj *TypeName, rhs Type) *Alias {
assert(rhs != nil)
a := &Alias{obj, nil, rhs, nil}
a := new(Alias)
a.obj = obj
a.orig = a
a.fromRHS = rhs
if obj.typ == nil {
obj.typ = a
}

View file

@ -17,7 +17,9 @@ import "fmt"
// which points directly to the actual (aliased) type.
type Alias struct {
obj *TypeName // corresponding declared alias object
orig *Alias // original, uninstantiated alias
tparams *TypeParamList // type parameters, or nil
targs *TypeList // type arguments, or nil
fromRHS Type // RHS of type alias declaration; may be an alias
actual Type // actual (aliased) type; never an alias
}
@ -41,6 +43,25 @@ func (a *Alias) String() string { return TypeString(a, nil) }
// [underlying type]: https://go.dev/ref/spec#Underlying_types.
func (a *Alias) Underlying() Type { return unalias(a).Underlying() }
// Origin returns the generic Alias type of which a is an instance.
// If a is not an instance of a generic alias, Origin returns a.
func (a *Alias) Origin() *Alias { return a.orig }
// TypeParams returns the type parameters of the alias type a, or nil.
// A generic Alias and its instances have the same type parameters.
func (a *Alias) TypeParams() *TypeParamList { return a.tparams }
// SetTypeParams sets the type parameters of the alias type a.
// The alias a must not have type arguments.
func (a *Alias) SetTypeParams(tparams []*TypeParam) {
assert(a.targs == nil)
a.tparams = bindTParams(tparams)
}
// TypeArgs returns the type arguments used to instantiate the Alias type.
// If a is not an instance of a generic alias, the result is nil.
func (a *Alias) TypeArgs() *TypeList { return a.targs }
// Rhs returns the type R on the right-hand side of an alias
// declaration "type A = R", which may be another alias.
func (a *Alias) Rhs() Type { return a.fromRHS }
@ -91,7 +112,10 @@ func asNamed(t Type) *Named {
// rhs must not be nil.
func (check *Checker) newAlias(obj *TypeName, rhs Type) *Alias {
assert(rhs != nil)
a := &Alias{obj, nil, rhs, nil}
a := new(Alias)
a.obj = obj
a.orig = a
a.fromRHS = rhs
if obj.typ == nil {
obj.typ = a
}