]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: add Alias.{TypeParams, SetTypeParams, TypeArgs, Origin}
authorRobert Griesemer <gri@golang.org>
Tue, 7 May 2024 16:38:28 +0000 (09:38 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 15 May 2024 21:32:30 +0000 (21:32 +0000)
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>

api/next/67143.txt [new file with mode: 0644]
doc/next/6-stdlib/99-minor/go/types/67143.md [new file with mode: 0644]
src/cmd/compile/internal/types2/alias.go
src/go/types/alias.go

diff --git a/api/next/67143.txt b/api/next/67143.txt
new file mode 100644 (file)
index 0000000..f124034
--- /dev/null
@@ -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
diff --git a/doc/next/6-stdlib/99-minor/go/types/67143.md b/doc/next/6-stdlib/99-minor/go/types/67143.md
new file mode 100644 (file)
index 0000000..405c679
--- /dev/null
@@ -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.
index ecd8637814a4eeb5e27317578510e7a4fa6fd16d..68475c54a46f11c057dd7d242086f665c30b6b65 100644 (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
        }
index 48bf9c0feb8b1494c95f1cf28dd8151d74fe285e..3fdd12ea0295e119766a4c9de2794f5dcee77c55 100644 (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
        }