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