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  	"fmt"
    12  	"go/token"
    13  )
    14  
    15  // An Alias represents an alias type.
    16  //
    17  // Alias types are created by alias declarations such as:
    18  //
    19  //	type A = int
    20  //
    21  // The type on the right-hand side of the declaration can be accessed
    22  // using [Alias.Rhs]. This type may itself be an alias.
    23  // Call [Unalias] to obtain the first non-alias type in a chain of
    24  // alias type declarations.
    25  //
    26  // Like a defined ([Named]) type, an alias type has a name.
    27  // Use the [Alias.Obj] method to access its [TypeName] object.
    28  //
    29  // Historically, Alias types were not materialized so that, in the example
    30  // above, A's type was represented by a Basic (int), not an Alias
    31  // whose [Alias.Rhs] is int. But Go 1.24 allows you to declare an
    32  // alias type with type parameters or arguments:
    33  //
    34  //	type Set[K comparable] = map[K]bool
    35  //	s := make(Set[String])
    36  //
    37  // and this requires that Alias types be materialized. Use the
    38  // [Alias.TypeParams] and [Alias.TypeArgs] methods to access them.
    39  //
    40  // To ease the transition, the Alias type was introduced in go1.22,
    41  // but the type-checker would not construct values of this type unless
    42  // the GODEBUG=gotypesalias=1 environment variable was provided.
    43  // Starting in go1.23, this variable is enabled by default.
    44  // This setting also causes the predeclared type "any" to be
    45  // represented as an Alias, not a bare [Interface].
    46  type Alias struct {
    47  	obj     *TypeName      // corresponding declared alias object
    48  	orig    *Alias         // original, uninstantiated alias
    49  	tparams *TypeParamList // type parameters, or nil
    50  	targs   *TypeList      // type arguments, or nil
    51  	fromRHS Type           // RHS of type alias declaration; may be an alias
    52  	actual  Type           // actual (aliased) type; never an alias
    53  }
    54  
    55  // NewAlias creates a new Alias type with the given type name and rhs.
    56  // rhs must not be nil.
    57  func NewAlias(obj *TypeName, rhs Type) *Alias {
    58  	alias := (*Checker)(nil).newAlias(obj, rhs)
    59  	// Ensure that alias.actual is set (#65455).
    60  	alias.cleanup()
    61  	return alias
    62  }
    63  
    64  // Obj returns the type name for the declaration defining the alias type a.
    65  // For instantiated types, this is same as the type name of the origin type.
    66  func (a *Alias) Obj() *TypeName { return a.orig.obj }
    67  
    68  func (a *Alias) String() string { return TypeString(a, nil) }
    69  
    70  // Underlying returns the [underlying type] of the alias type a, which is the
    71  // underlying type of the aliased type. Underlying types are never Named,
    72  // TypeParam, or Alias types.
    73  //
    74  // [underlying type]: https://go.dev/ref/spec#Underlying_types.
    75  func (a *Alias) Underlying() Type { return unalias(a).Underlying() }
    76  
    77  // Origin returns the generic Alias type of which a is an instance.
    78  // If a is not an instance of a generic alias, Origin returns a.
    79  func (a *Alias) Origin() *Alias { return a.orig }
    80  
    81  // TypeParams returns the type parameters of the alias type a, or nil.
    82  // A generic Alias and its instances have the same type parameters.
    83  func (a *Alias) TypeParams() *TypeParamList { return a.tparams }
    84  
    85  // SetTypeParams sets the type parameters of the alias type a.
    86  // The alias a must not have type arguments.
    87  func (a *Alias) SetTypeParams(tparams []*TypeParam) {
    88  	assert(a.targs == nil)
    89  	a.tparams = bindTParams(tparams)
    90  }
    91  
    92  // TypeArgs returns the type arguments used to instantiate the Alias type.
    93  // If a is not an instance of a generic alias, the result is nil.
    94  func (a *Alias) TypeArgs() *TypeList { return a.targs }
    95  
    96  // Rhs returns the type R on the right-hand side of an alias
    97  // declaration "type A = R", which may be another alias.
    98  func (a *Alias) Rhs() Type { return a.fromRHS }
    99  
   100  // Unalias returns t if it is not an alias type;
   101  // otherwise it follows t's alias chain until it
   102  // reaches a non-alias type which is then returned.
   103  // Consequently, the result is never an alias type.
   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  	if t == nil {
   120  		panic(fmt.Sprintf("non-terminated alias %s", a0.obj.name))
   121  	}
   122  
   123  	// Memoize the type only if valid.
   124  	// In the presence of unfinished cyclic declarations, Unalias
   125  	// would otherwise latch the invalid value (#66704).
   126  	// TODO(adonovan): rethink, along with checker.typeDecl's use
   127  	// of Invalid to mark unfinished aliases.
   128  	if t != Typ[Invalid] {
   129  		a0.actual = t
   130  	}
   131  
   132  	return t
   133  }
   134  
   135  // asNamed returns t as *Named if that is t's
   136  // actual type. It returns nil otherwise.
   137  func asNamed(t Type) *Named {
   138  	n, _ := Unalias(t).(*Named)
   139  	return n
   140  }
   141  
   142  // newAlias creates a new Alias type with the given type name and rhs.
   143  // rhs must not be nil.
   144  func (check *Checker) newAlias(obj *TypeName, rhs Type) *Alias {
   145  	assert(rhs != nil)
   146  	a := new(Alias)
   147  	a.obj = obj
   148  	a.orig = a
   149  	a.fromRHS = rhs
   150  	if obj.typ == nil {
   151  		obj.typ = a
   152  	}
   153  
   154  	// Ensure that a.actual is set at the end of type checking.
   155  	if check != nil {
   156  		check.needsCleanup(a)
   157  	}
   158  
   159  	return a
   160  }
   161  
   162  // newAliasInstance creates a new alias instance for the given origin and type
   163  // arguments, recording pos as the position of its synthetic object (for error
   164  // reporting).
   165  func (check *Checker) newAliasInstance(pos token.Pos, orig *Alias, targs []Type, expanding *Named, ctxt *Context) *Alias {
   166  	assert(len(targs) > 0)
   167  	obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)
   168  	rhs := check.subst(pos, orig.fromRHS, makeSubstMap(orig.TypeParams().list(), targs), expanding, ctxt)
   169  	res := check.newAlias(obj, rhs)
   170  	res.orig = orig
   171  	res.tparams = orig.tparams
   172  	res.targs = newTypeList(targs)
   173  	return res
   174  }
   175  
   176  func (a *Alias) cleanup() {
   177  	// Ensure a.actual is set before types are published,
   178  	// so Unalias is a pure "getter", not a "setter".
   179  	actual := Unalias(a)
   180  
   181  	if actual == Typ[Invalid] {
   182  		// We don't set a.actual to Typ[Invalid] during type checking,
   183  		// as it may indicate that the RHS is not fully set up.
   184  		a.actual = actual
   185  	}
   186  }
   187  

View as plain text