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  type Alias struct {
    28  	obj     *TypeName      // corresponding declared alias object
    29  	orig    *Alias         // original, uninstantiated alias
    30  	tparams *TypeParamList // type parameters, or nil
    31  	targs   *TypeList      // type arguments, or nil
    32  	fromRHS Type           // RHS of type alias declaration; may be an alias
    33  	actual  Type           // actual (aliased) type; never an alias
    34  	check   *Checker       // for cycle detection and cleanup; nil after cleanup
    35  }
    36  
    37  // NewAlias creates a new Alias type with the given type name and rhs.
    38  // If rhs is nil, the alias is incomplete.
    39  func NewAlias(obj *TypeName, rhs Type) *Alias {
    40  	alias := (*Checker)(nil).newAlias(obj, rhs)
    41  	// Ensure that alias.actual is set (go.dev/issue/65455).
    42  	if rhs != nil {
    43  		alias.cleanup()
    44  	}
    45  	return alias
    46  }
    47  
    48  // Obj returns the type name for the declaration defining the alias type a.
    49  // For instantiated types, this is same as the type name of the origin type.
    50  func (a *Alias) Obj() *TypeName { return a.orig.obj }
    51  
    52  func (a *Alias) String() string { return TypeString(a, nil) }
    53  
    54  // Underlying returns the [underlying type] of the alias type a, which is the
    55  // underlying type of the aliased type. Underlying types are never Named,
    56  // TypeParam, or Alias types.
    57  //
    58  // [underlying type]: https://go.dev/ref/spec#Underlying_types.
    59  func (a *Alias) Underlying() Type {
    60  	u := unalias(a)
    61  	if u == nil {
    62  		// Before CL 825744, Underlying() returned unalias(a).Underlying(). If the alias
    63  		// was incomplete (its RHS had not yet been set or resolved), unalias(a) returned
    64  		// nil, causing an immediate unhandled nil-pointer dereference panic.
    65  		//
    66  		// Callers must ensure an alias has been completed before querying its underlying type.
    67  		panic("Underlying() called on incomplete Alias")
    68  	}
    69  	return u.Underlying()
    70  }
    71  
    72  // Origin returns the generic Alias type of which a is an instance.
    73  // If a is not an instance of a generic alias, Origin returns a.
    74  func (a *Alias) Origin() *Alias { return a.orig }
    75  
    76  // TypeParams returns the type parameters of the alias type a, or nil.
    77  // A generic Alias and its instances have the same type parameters.
    78  func (a *Alias) TypeParams() *TypeParamList { return a.tparams }
    79  
    80  // SetTypeParams sets the type parameters of the alias type a.
    81  // The alias a must not have type arguments.
    82  func (a *Alias) SetTypeParams(tparams []*TypeParam) {
    83  	assert(a.targs == nil)
    84  	a.tparams = bindTParams(tparams)
    85  }
    86  
    87  // TypeArgs returns the type arguments used to instantiate the Alias type.
    88  // If a is not an instance of a generic alias, the result is nil.
    89  func (a *Alias) TypeArgs() *TypeList { return a.targs }
    90  
    91  // Rhs returns the type R on the right-hand side of an alias
    92  // declaration "type A = R", which may be another alias.
    93  func (a *Alias) Rhs() Type {
    94  	if a.fromRHS == nil {
    95  		// Before CL 825744, Rhs() returned a.fromRHS directly without checking for nil.
    96  		// If the alias was incomplete, callers expecting a non-nil Type could panic.
    97  		//
    98  		// An incomplete alias has no defined RHS. Rhs panics if called on an incomplete
    99  		// alias. Users must complete the alias first.
   100  		panic("Rhs() called on incomplete Alias")
   101  	}
   102  	return a.fromRHS
   103  }
   104  
   105  // Unalias returns t if it is not an alias type;
   106  // otherwise it follows t's alias chain until it
   107  // reaches a non-alias type which is then returned.
   108  // Consequently, the result is never an alias type.
   109  // Returns nil if the alias is incomplete.
   110  func Unalias(t Type) Type {
   111  	if a0, _ := t.(*Alias); a0 != nil {
   112  		return unalias(a0)
   113  	}
   114  	return t
   115  }
   116  
   117  func unalias(a0 *Alias) Type {
   118  	if a0.actual != nil {
   119  		return a0.actual
   120  	}
   121  	var t Type
   122  	for a := a0; a != nil; a, _ = t.(*Alias) {
   123  		t = a.fromRHS
   124  	}
   125  	// Memoize during type checking (single-threaded) avoids future
   126  	// quadratic chain traversal. Once a0.check becomes nil, unalias
   127  	// is a pure getter to prevent concurrent write races (go.dev/issue/81138).
   128  	if a0.check != nil && t != nil {
   129  		a0.actual = t
   130  	}
   131  	return t
   132  }
   133  
   134  // asNamed returns t as *Named if that is t's
   135  // actual type. It returns nil otherwise.
   136  func asNamed(t Type) *Named {
   137  	n, _ := Unalias(t).(*Named)
   138  	return n
   139  }
   140  
   141  // newAlias creates a new Alias type with the given type name and rhs.
   142  // If rhs is nil, the alias is incomplete.
   143  func (check *Checker) newAlias(obj *TypeName, rhs Type) *Alias {
   144  	a := new(Alias)
   145  	a.obj = obj
   146  	a.orig = a
   147  	a.fromRHS = rhs
   148  	a.check = check
   149  	if obj.typ == nil {
   150  		obj.typ = a
   151  	}
   152  
   153  	// Ensure that a.actual is set at the end of type checking.
   154  	if check != nil {
   155  		check.needsCleanup(a)
   156  	}
   157  
   158  	return a
   159  }
   160  
   161  // newAliasInstance creates a new alias instance for the given origin and type
   162  // arguments, recording pos as the position of its synthetic object (for error
   163  // reporting).
   164  func (check *Checker) newAliasInstance(pos token.Pos, orig *Alias, targs []Type, expanding *Named, ctxt *Context) *Alias {
   165  	assert(len(targs) > 0)
   166  	obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)
   167  	rhs := check.subst(pos, orig.fromRHS, makeSubstMap(orig.TypeParams().list(), targs), expanding, ctxt)
   168  	res := check.newAlias(obj, rhs)
   169  	res.orig = orig
   170  	res.tparams = orig.tparams
   171  	res.targs = newTypeList(targs)
   172  	// Ensure that res.actual is set before returning an instance constructed
   173  	// outside of package type checking (e.g. via types.Instantiate). When check != nil,
   174  	// res was added to check.cleanups by newAlias and will be cleaned up at the end
   175  	// of checking. But when check == nil, no cleanup pass will run, so we must clean
   176  	// up eagerly to prevent concurrent calls to Unalias from racing on res.actual (go.dev/issue/81138).
   177  	if check == nil {
   178  		res.cleanup()
   179  	}
   180  	return res
   181  }
   182  
   183  func (a *Alias) cleanup() {
   184  	// Before CL 825744, an incomplete alias produced during type checking (e.g.
   185  	// due to a cycle or syntax error) could be left with fromRHS == nil and
   186  	// actual == nil. Callers traversing the published types would then panic
   187  	// when inspecting the alias.
   188  	//
   189  	// Here, we normalize incomplete aliases to Typ[Invalid] so the published
   190  	// package graph never contains buried nils.
   191  	//
   192  	// Invariant: The type checker must never return Typ[Invalid] unless an error was
   193  	// reported. If an alias created during package checking has a nil RHS at
   194  	// cleanup time, ensure an error is reported.
   195  	if a.fromRHS == nil {
   196  		if a.check != nil && a.check.firstErr == nil {
   197  			a.check.internalErrorf(a.obj, "alias %v has nil RHS", a.obj.name)
   198  		}
   199  		a.fromRHS = Typ[Invalid]
   200  	}
   201  	// Ensure a.actual is set before types are published,
   202  	// so unalias is a pure "getter", not a "setter".
   203  	a.actual = unalias(a)
   204  	a.check = nil
   205  }
   206  

View as plain text