summaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/tools/internal/aliases
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/golang.org/x/tools/internal/aliases')
-rw-r--r--vendor/golang.org/x/tools/internal/aliases/aliases.go12
-rw-r--r--vendor/golang.org/x/tools/internal/aliases/aliases_go121.go15
-rw-r--r--vendor/golang.org/x/tools/internal/aliases/aliases_go122.go69
3 files changed, 46 insertions, 50 deletions
diff --git a/vendor/golang.org/x/tools/internal/aliases/aliases.go b/vendor/golang.org/x/tools/internal/aliases/aliases.go
index f89112c8e..c24c2eee4 100644
--- a/vendor/golang.org/x/tools/internal/aliases/aliases.go
+++ b/vendor/golang.org/x/tools/internal/aliases/aliases.go
@@ -16,10 +16,14 @@ import (
// NewAlias creates a new TypeName in Package pkg that
// is an alias for the type rhs.
//
-// When GoVersion>=1.22 and GODEBUG=gotypesalias=1,
-// the Type() of the return value is a *types.Alias.
-func NewAlias(pos token.Pos, pkg *types.Package, name string, rhs types.Type) *types.TypeName {
- if enabled() {
+// The enabled parameter determines whether the resulting [TypeName]'s
+// type is an [types.Alias]. Its value must be the result of a call to
+// [Enabled], which computes the effective value of
+// GODEBUG=gotypesalias=... by invoking the type checker. The Enabled
+// function is expensive and should be called once per task (e.g.
+// package import), not once per call to NewAlias.
+func NewAlias(enabled bool, pos token.Pos, pkg *types.Package, name string, rhs types.Type) *types.TypeName {
+ if enabled {
tname := types.NewTypeName(pos, pkg, name, nil)
newAlias(tname, rhs)
return tname
diff --git a/vendor/golang.org/x/tools/internal/aliases/aliases_go121.go b/vendor/golang.org/x/tools/internal/aliases/aliases_go121.go
index 1872b56ff..c027b9f31 100644
--- a/vendor/golang.org/x/tools/internal/aliases/aliases_go121.go
+++ b/vendor/golang.org/x/tools/internal/aliases/aliases_go121.go
@@ -15,16 +15,17 @@ import (
// It will never be created by go/types.
type Alias struct{}
-func (*Alias) String() string { panic("unreachable") }
-
+func (*Alias) String() string { panic("unreachable") }
func (*Alias) Underlying() types.Type { panic("unreachable") }
-
-func (*Alias) Obj() *types.TypeName { panic("unreachable") }
+func (*Alias) Obj() *types.TypeName { panic("unreachable") }
+func Rhs(alias *Alias) types.Type { panic("unreachable") }
// Unalias returns the type t for go <=1.21.
func Unalias(t types.Type) types.Type { return t }
-// Always false for go <=1.21. Ignores GODEBUG.
-func enabled() bool { return false }
-
func newAlias(name *types.TypeName, rhs types.Type) *Alias { panic("unreachable") }
+
+// Enabled reports whether [NewAlias] should create [types.Alias] types.
+//
+// Before go1.22, this function always returns false.
+func Enabled() bool { return false }
diff --git a/vendor/golang.org/x/tools/internal/aliases/aliases_go122.go b/vendor/golang.org/x/tools/internal/aliases/aliases_go122.go
index 8b9211628..b32995484 100644
--- a/vendor/golang.org/x/tools/internal/aliases/aliases_go122.go
+++ b/vendor/golang.org/x/tools/internal/aliases/aliases_go122.go
@@ -12,14 +12,22 @@ import (
"go/parser"
"go/token"
"go/types"
- "os"
- "strings"
- "sync"
)
// Alias is an alias of types.Alias.
type Alias = types.Alias
+// Rhs returns the type on the right-hand side of the alias declaration.
+func Rhs(alias *Alias) types.Type {
+ if alias, ok := any(alias).(interface{ Rhs() types.Type }); ok {
+ return alias.Rhs() // go1.23+
+ }
+
+ // go1.22's Alias didn't have the Rhs method,
+ // so Unalias is the best we can do.
+ return Unalias(alias)
+}
+
// Unalias is a wrapper of types.Unalias.
func Unalias(t types.Type) types.Type { return types.Unalias(t) }
@@ -33,40 +41,23 @@ func newAlias(tname *types.TypeName, rhs types.Type) *Alias {
return a
}
-// enabled returns true when types.Aliases are enabled.
-func enabled() bool {
- // Use the gotypesalias value in GODEBUG if set.
- godebug := os.Getenv("GODEBUG")
- value := -1 // last set value.
- for _, f := range strings.Split(godebug, ",") {
- switch f {
- case "gotypesalias=1":
- value = 1
- case "gotypesalias=0":
- value = 0
- }
- }
- switch value {
- case 0:
- return false
- case 1:
- return true
- default:
- return aliasesDefault()
- }
-}
-
-// aliasesDefault reports if aliases are enabled by default.
-func aliasesDefault() bool {
- // Dynamically check if Aliases will be produced from go/types.
- aliasesDefaultOnce.Do(func() {
- fset := token.NewFileSet()
- f, _ := parser.ParseFile(fset, "a.go", "package p; type A = int", 0)
- pkg, _ := new(types.Config).Check("p", fset, []*ast.File{f}, nil)
- _, gotypesaliasDefault = pkg.Scope().Lookup("A").Type().(*types.Alias)
- })
- return gotypesaliasDefault
+// Enabled reports whether [NewAlias] should create [types.Alias] types.
+//
+// This function is expensive! Call it sparingly.
+func Enabled() bool {
+ // The only reliable way to compute the answer is to invoke go/types.
+ // We don't parse the GODEBUG environment variable, because
+ // (a) it's tricky to do so in a manner that is consistent
+ // with the godebug package; in particular, a simple
+ // substring check is not good enough. The value is a
+ // rightmost-wins list of options. But more importantly:
+ // (b) it is impossible to detect changes to the effective
+ // setting caused by os.Setenv("GODEBUG"), as happens in
+ // many tests. Therefore any attempt to cache the result
+ // is just incorrect.
+ fset := token.NewFileSet()
+ f, _ := parser.ParseFile(fset, "a.go", "package p; type A = int", 0)
+ pkg, _ := new(types.Config).Check("p", fset, []*ast.File{f}, nil)
+ _, enabled := pkg.Scope().Lookup("A").Type().(*types.Alias)
+ return enabled
}
-
-var gotypesaliasDefault bool
-var aliasesDefaultOnce sync.Once