Source file src/cmd/vendor/golang.org/x/tools/internal/aliases/aliases_go122.go

     1  // Copyright 2024 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  //go:build go1.22
     6  // +build go1.22
     7  
     8  package aliases
     9  
    10  import (
    11  	"go/ast"
    12  	"go/parser"
    13  	"go/token"
    14  	"go/types"
    15  )
    16  
    17  // Alias is an alias of types.Alias.
    18  type Alias = types.Alias
    19  
    20  // Rhs returns the type on the right-hand side of the alias declaration.
    21  func Rhs(alias *Alias) types.Type {
    22  	if alias, ok := any(alias).(interface{ Rhs() types.Type }); ok {
    23  		return alias.Rhs() // go1.23+
    24  	}
    25  
    26  	// go1.22's Alias didn't have the Rhs method,
    27  	// so Unalias is the best we can do.
    28  	return Unalias(alias)
    29  }
    30  
    31  // TypeParams returns the type parameter list of the alias.
    32  func TypeParams(alias *Alias) *types.TypeParamList {
    33  	if alias, ok := any(alias).(interface{ TypeParams() *types.TypeParamList }); ok {
    34  		return alias.TypeParams() // go1.23+
    35  	}
    36  	return nil
    37  }
    38  
    39  // SetTypeParams sets the type parameters of the alias type.
    40  func SetTypeParams(alias *Alias, tparams []*types.TypeParam) {
    41  	if alias, ok := any(alias).(interface {
    42  		SetTypeParams(tparams []*types.TypeParam)
    43  	}); ok {
    44  		alias.SetTypeParams(tparams) // go1.23+
    45  	} else if len(tparams) > 0 {
    46  		panic("cannot set type parameters of an Alias type in go1.22")
    47  	}
    48  }
    49  
    50  // TypeArgs returns the type arguments used to instantiate the Alias type.
    51  func TypeArgs(alias *Alias) *types.TypeList {
    52  	if alias, ok := any(alias).(interface{ TypeArgs() *types.TypeList }); ok {
    53  		return alias.TypeArgs() // go1.23+
    54  	}
    55  	return nil // empty (go1.22)
    56  }
    57  
    58  // Origin returns the generic Alias type of which alias is an instance.
    59  // If alias is not an instance of a generic alias, Origin returns alias.
    60  func Origin(alias *Alias) *Alias {
    61  	if alias, ok := any(alias).(interface{ Origin() *types.Alias }); ok {
    62  		return alias.Origin() // go1.23+
    63  	}
    64  	return alias // not an instance of a generic alias (go1.22)
    65  }
    66  
    67  // Unalias is a wrapper of types.Unalias.
    68  func Unalias(t types.Type) types.Type { return types.Unalias(t) }
    69  
    70  // newAlias is an internal alias around types.NewAlias.
    71  // Direct usage is discouraged as the moment.
    72  // Try to use NewAlias instead.
    73  func newAlias(tname *types.TypeName, rhs types.Type, tparams []*types.TypeParam) *Alias {
    74  	a := types.NewAlias(tname, rhs)
    75  	SetTypeParams(a, tparams)
    76  	return a
    77  }
    78  
    79  // Enabled reports whether [NewAlias] should create [types.Alias] types.
    80  //
    81  // This function is expensive! Call it sparingly.
    82  func Enabled() bool {
    83  	// The only reliable way to compute the answer is to invoke go/types.
    84  	// We don't parse the GODEBUG environment variable, because
    85  	// (a) it's tricky to do so in a manner that is consistent
    86  	//     with the godebug package; in particular, a simple
    87  	//     substring check is not good enough. The value is a
    88  	//     rightmost-wins list of options. But more importantly:
    89  	// (b) it is impossible to detect changes to the effective
    90  	//     setting caused by os.Setenv("GODEBUG"), as happens in
    91  	//     many tests. Therefore any attempt to cache the result
    92  	//     is just incorrect.
    93  	fset := token.NewFileSet()
    94  	f, _ := parser.ParseFile(fset, "a.go", "package p; type A = int", 0)
    95  	pkg, _ := new(types.Config).Check("p", fset, []*ast.File{f}, nil)
    96  	_, enabled := pkg.Scope().Lookup("A").Type().(*types.Alias)
    97  	return enabled
    98  }
    99  

View as plain text