Skip to content

Commit

Permalink
go/types, types2: add Alias.{TypeParams, SetTypeParams, TypeArgs, Ori…
Browse files Browse the repository at this point in the history
…gin}

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>
  • Loading branch information
griesemer authored and gopherbot committed May 15, 2024
1 parent 36b06c3 commit bf279b7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
4 changes: 4 additions & 0 deletions api/next/67143.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions doc/next/6-stdlib/99-minor/go/types/67143.md
Original file line number Diff line number Diff line change
@@ -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.
26 changes: 25 additions & 1 deletion src/cmd/compile/internal/types2/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 }
Expand Down Expand Up @@ -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
}
Expand Down
26 changes: 25 additions & 1 deletion src/go/types/alias.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bf279b7

Please sign in to comment.