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

     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  
   117  	// It's fine to memoize nil types since it's the zero value for actual.
   118  	// It accomplishes nothing.
   119  	a0.actual = t
   120  	return t
   121  }
   122  
   123  // asNamed returns t as *Named if that is t's
   124  // actual type. It returns nil otherwise.
   125  func asNamed(t Type) *Named {
   126  	n, _ := Unalias(t).(*Named)
   127  	return n
   128  }
   129  
   130  // newAlias creates a new Alias type with the given type name and rhs.
   131  // If rhs is nil, the alias is incomplete.
   132  func (check *Checker) newAlias(obj *TypeName, rhs Type) *Alias {
   133  	a := new(Alias)
   134  	a.obj = obj
   135  	a.orig = a
   136  	a.fromRHS = rhs
   137  	if obj.typ == nil {
   138  		obj.typ = a
   139  	}
   140  
   141  	// Ensure that a.actual is set at the end of type checking.
   142  	if check != nil {
   143  		check.needsCleanup(a)
   144  	}
   145  
   146  	return a
   147  }
   148  
   149  // newAliasInstance creates a new alias instance for the given origin and type
   150  // arguments, recording pos as the position of its synthetic object (for error
   151  // reporting).
   152  func (check *Checker) newAliasInstance(pos syntax.Pos, orig *Alias, targs []Type, expanding *Named, ctxt *Context) *Alias {
   153  	assert(len(targs) > 0)
   154  	obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)
   155  	rhs := check.subst(pos, orig.fromRHS, makeSubstMap(orig.TypeParams().list(), targs), expanding, ctxt)
   156  	res := check.newAlias(obj, rhs)
   157  	res.orig = orig
   158  	res.tparams = orig.tparams
   159  	res.targs = newTypeList(targs)
   160  	return res
   161  }
   162  
   163  func (a *Alias) cleanup() {
   164  	// Ensure a.actual is set before types are published,
   165  	// so unalias is a pure "getter", not a "setter".
   166  	unalias(a)
   167  }
   168  

View as plain text