Debian Codegraph @

src/cmd/compile/internal/types2/alias.go

raw
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package types2
6
7 import (
8 "cmd/compile/internal/syntax"
9 )
10
11 // An Alias represents an alias type.
12 //
13 // Alias types are created by alias declarations such as:
14 //
15 // type A = int
16 //
17 // The type on the right-hand side of the declaration can be accessed
18 // using [Alias.Rhs]. This type may itself be an alias.
19 // Call [Unalias] to obtain the first non-alias type in a chain of
20 // alias type declarations.
21 //
22 // Like a defined ([Named]) type, an alias type has a name.
23 // Use the [Alias.Obj] method to access its [TypeName] object.
24 //
25 // Historically, Alias types were not materialized so that, in the example
26 // above, A's type was represented by a Basic (int), not an Alias
27 // whose [Alias.Rhs] is int. But Go 1.24 allows you to declare an
28 // alias type with type parameters or arguments:
29 //
30 // type Set[K comparable] = map[K]bool
31 // s := make(Set[String])
32 //
33 // and this requires that Alias types be materialized. Use the
34 // [Alias.TypeParams] and [Alias.TypeArgs] methods to access them.
35 //
36 // To ease the transition, the Alias type was introduced in go1.22,
37 // but the type-checker would not construct values of this type unless
38 // the GODEBUG=gotypesalias=1 environment variable was provided.
39 // Starting in go1.23, this variable is enabled by default.
40 // This setting also causes the predeclared type "any" to be
41 // represented as an Alias, not a bare [Interface].
42 type Alias struct {
43 obj *TypeName // corresponding declared alias object
44 orig *Alias // original, uninstantiated alias
45 tparams *TypeParamList // type parameters, or nil
46 targs *TypeList // type arguments, or nil
47 fromRHS Type // RHS of type alias declaration; may be an alias
48 actual Type // actual (aliased) type; never an alias
49 }
50
51 // NewAlias creates a new Alias type with the given type name and rhs.
52 // If rhs is nil, the alias is incomplete.
53 func NewAlias(obj *TypeName, rhs Type) *Alias {
54 alias := (*Checker)(nil).newAlias(obj, rhs)
55 // Ensure that alias.actual is set (#65455).
56 alias.cleanup()
57 return alias
58 }
59
60 // Obj returns the type name for the declaration defining the alias type a.
61 // For instantiated types, this is same as the type name of the origin type.
62 func (a *Alias) Obj() *TypeName { return a.orig.obj }
63
64 func (a *Alias) String() string { return TypeString(a, nil) }
65
66 // Underlying returns the [underlying type] of the alias type a, which is the
67 // underlying type of the aliased type. Underlying types are never Named,
68 // TypeParam, or Alias types.
69 //
70 // [underlying type]: https://go.dev/ref/spec#Underlying_types.
71 func (a *Alias) Underlying() Type { return unalias(a).Underlying() }
72
73 // Origin returns the generic Alias type of which a is an instance.
74 // If a is not an instance of a generic alias, Origin returns a.
75 func (a *Alias) Origin() *Alias { return a.orig }
76
77 // TypeParams returns the type parameters of the alias type a, or nil.
78 // A generic Alias and its instances have the same type parameters.
79 func (a *Alias) TypeParams() *TypeParamList { return a.tparams }
80
81 // SetTypeParams sets the type parameters of the alias type a.
82 // The alias a must not have type arguments.
83 func (a *Alias) SetTypeParams(tparams []*TypeParam) {
84 assert(a.targs == nil)
85 a.tparams = bindTParams(tparams)
86 }
87
88 // TypeArgs returns the type arguments used to instantiate the Alias type.
89 // If a is not an instance of a generic alias, the result is nil.
90 func (a *Alias) TypeArgs() *TypeList { return a.targs }
91
92 // Rhs returns the type R on the right-hand side of an alias
93 // declaration "type A = R", which may be another alias.
94 func (a *Alias) Rhs() Type { return a.fromRHS }
95
96 // Unalias returns t if it is not an alias type;
97 // otherwise it follows t's alias chain until it
98 // reaches a non-alias type which is then returned.
99 // Consequently, the result is never an alias type.
100 // Returns nil if the alias is incomplete.
101 func Unalias(t Type) Type {
102 if a0, _ := t.(*Alias); a0 != nil {
103 return unalias(a0)
104 }
105 return t
106 }
107
108 func unalias(a0 *Alias) Type {
109 if a0.actual != nil {
110 return a0.actual
111 }
112 var t Type
113 for a := a0; a != nil; a, _ = t.(*Alias) {
114 t = a.fromRHS
115 }
116 // It's fine to memoize nil types since it's the zero value for actual.
117 // It accomplishes nothing.
118 a0.actual = t
119 return t
120 }
121
122 // asNamed returns t as *Named if that is t's
123 // actual type. It returns nil otherwise.
124 func asNamed(t Type) *Named {
125 n, _ := Unalias(t).(*Named)
126 return n
127 }
128
129 // newAlias creates a new Alias type with the given type name and rhs.
130 // If rhs is nil, the alias is incomplete.
131 func (check *Checker) newAlias(obj *TypeName, rhs Type) *Alias {
132 a := new(Alias)
133 a.obj = obj
134 a.orig = a
135 a.fromRHS = rhs
136 if obj.typ == nil {
137 obj.typ = a
138 }
139
140 // Ensure that a.actual is set at the end of type checking.
141 if check != nil {
142 check.needsCleanup(a)
143 }
144
145 return a
146 }
147
148 // newAliasInstance creates a new alias instance for the given origin and type
149 // arguments, recording pos as the position of its synthetic object (for error
150 // reporting).
151 func (check *Checker) newAliasInstance(pos syntax.Pos, orig *Alias, targs []Type, expanding *Named, ctxt *Context) *Alias {
152 assert(len(targs) > 0)
153 obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)
154 rhs := check.subst(pos, orig.fromRHS, makeSubstMap(orig.TypeParams().list(), targs), expanding, ctxt)
155 res := check.newAlias(obj, rhs)
156 res.orig = orig
157 res.tparams = orig.tparams
158 res.targs = newTypeList(targs)
159 return res
160 }
161
162 func (a *Alias) cleanup() {
163 // Ensure a.actual is set before types are published,
164 // so unalias is a pure "getter", not a "setter".
165 unalias(a)
166 }