Revert "internal/aliases: add a function to conditionally enable aliases"

This reverts commit f8f3c13ff3.

Reason for revert: https://go.dev/cl/619395 appears to be a better solution for the same problem.

Change-Id: Ie3f290fca74b2cc2627484504b6db780f89b3bb4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/619415
Reviewed-by: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Commit-Queue: Tim King <taking@google.com>
This commit is contained in:
Tim King 2024-10-10 16:49:59 +00:00 коммит произвёл Go LUCI
Родитель 4f6e118e23
Коммит 50179f2225
2 изменённых файлов: 0 добавлений и 130 удалений

Просмотреть файл

@ -5,13 +5,8 @@
package aliases package aliases
import ( import (
"go/build"
"go/token" "go/token"
"go/types" "go/types"
"os"
"slices"
"strings"
"sync"
) )
// Package aliases defines backward compatible shims // Package aliases defines backward compatible shims
@ -41,52 +36,3 @@ func NewAlias(enabled bool, pos token.Pos, pkg *types.Package, name string, rhs
} }
return types.NewTypeName(pos, pkg, name, rhs) return types.NewTypeName(pos, pkg, name, rhs)
} }
// ConditionallyEnableGoTypesAlias enables the gotypesalias GODEBUG setting if
// * the version of go used to compile the program is between 1.23 and 1.26,
// * gotypesalias are not already enabled, and
// * gotypesalias is not set in GODEBUG already
// exactly once. Otherwise it does nothing.
//
// Recommended usage is to do the following within a main package:
//
// func init() { ConditionallyEnableGoTypesAlias() }
//
// within a module with go version 1.22 or in GOPATH mode.
func ConditionallyEnableGoTypesAlias() { conditionallyEnableGoTypesAliasOnce() }
var conditionallyEnableGoTypesAliasOnce = sync.OnceFunc(conditionallyEnableGoTypesAlias)
func conditionallyEnableGoTypesAlias() {
// Let R be the version of go the program was compiled with. Then
// if R < 1.22, do nothing as gotypesalias is meaningless,
// if R == 1.22, do not turn on gotypesalias (latent bugs),
// if 1.23 <= R && R <= 1.26, turn on gotypesalias, and
// if R >= 1.27, this is a guaranteed no-op.
if !slices.Contains(build.Default.ReleaseTags, "go1.23") {
return // R < 1.23 (do nothing)
}
if slices.Contains(build.Default.ReleaseTags, "go1.27") {
return // R >= 1.27 (do nothing)
}
// 1.23 <= R <= 1.26
_, anyIsAlias := types.Universe.Lookup("any").Type().(*types.Alias)
if anyIsAlias {
return // gotypesalias are already enabled.
}
// Does GODEBUG have gotypesalias set already?
godebugs := strings.Split(os.Getenv("GODEBUG"), ",")
for _, p := range godebugs {
if strings.HasPrefix(strings.TrimSpace(p), "gotypesalias=") {
// gotypesalias is set in GODEBUG already.
// Do not override this setting.
return
}
}
// Enable gotypesalias.
godebugs = append(godebugs, "gotypesalias=1")
os.Setenv("GODEBUG", strings.Join(godebugs, ","))
}

Просмотреть файл

@ -1,76 +0,0 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package aliases_test
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"testing"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/testenv"
)
func init() {
if os.Getenv("ConditionallyEnableGoTypesAlias_CHILD") == "1" {
go aliases.ConditionallyEnableGoTypesAlias() // Throw in an extra call. Should be fine.
aliases.ConditionallyEnableGoTypesAlias()
}
}
func TestConditionallyEnableGoTypesAlias(t *testing.T) {
if !(runtime.GOOS == "linux" || runtime.GOOS == "darwin") {
t.Skipf("skipping fork/exec test on this platform")
}
if os.Getenv("ConditionallyEnableGoTypesAlias_CHILD") == "1" {
// child process
enabled := aliases.Enabled()
fmt.Printf("gotypesalias is enabled %v", enabled)
return
}
testenv.NeedsTool(t, "go")
var wants map[string]string
const (
enabled = "gotypesalias is enabled true"
disabled = "gotypesalias is enabled false"
)
goversion := testenv.Go1Point()
if goversion <= 22 {
wants = map[string]string{
"": disabled,
"0": disabled,
"1": enabled,
}
} else {
wants = map[string]string{
"": enabled,
"0": disabled,
"1": enabled,
}
}
for _, test := range []string{"", "0", "1"} {
cmd := exec.Command(os.Args[0], "-test.run=TestConditionallyEnableGoTypesAlias")
cmd.Env = append(os.Environ(), "ConditionallyEnableGoTypesAlias_CHILD=1")
if test != "" {
cmd.Env = append(cmd.Env, fmt.Sprintf("GODEBUG=gotypesalias=%s", test))
}
out, err := cmd.CombinedOutput()
if err != nil {
t.Error(err)
}
want := wants[test]
if !strings.Contains(string(out), want) {
t.Errorf("gotypesalias=%q: want %s", test, want)
t.Logf("(go 1.%d) %q: out=<<%s>>", goversion, test, out)
}
}
}