Skip to content

Commit

Permalink
all: apply versions.InitFileVersions in x/tools.
Browse files Browse the repository at this point in the history
Updates golang/go#62605

Change-Id: I127b57f4eb5b6d2521dde7f139048987614e1904
Reviewed-on: https://go-review.googlesource.com/c/tools/+/533975
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Tim King <taking@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
timothy-king committed Nov 13, 2023
1 parent aafa2e0 commit b03756e
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 55 deletions.
2 changes: 2 additions & 0 deletions go/analysis/unitchecker/unitchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"golang.org/x/tools/go/analysis/internal/analysisflags"
"golang.org/x/tools/internal/facts"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/versions"
)

// A Config describes a compilation unit to be analyzed.
Expand Down Expand Up @@ -262,6 +263,7 @@ func run(fset *token.FileSet, cfg *Config, analyzers []*analysis.Analyzer) ([]re
Selections: make(map[*ast.SelectorExpr]*types.Selection),
}
typeparams.InitInstanceInfo(info)
versions.InitFileVersions(info)

pkg, err := tc.Check(cfg.ImportPath, fset, files, info)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions go/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/internal/cgo"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/versions"
)

var ignoreVendor build.ImportMode
Expand Down Expand Up @@ -1040,6 +1041,7 @@ func (imp *importer) newPackageInfo(path, dir string) *PackageInfo {
dir: dir,
}
typeparams.InitInstanceInfo(&info.Info)
versions.InitFileVersions(&info.Info)

// Copy the types.Config so we can vary it across PackageInfos.
tc := imp.conf.TypeChecker
Expand Down
2 changes: 2 additions & 0 deletions go/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"golang.org/x/tools/internal/packagesinternal"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/typesinternal"
"golang.org/x/tools/internal/versions"
)

// A LoadMode controls the amount of detail to return when loading.
Expand Down Expand Up @@ -1018,6 +1019,7 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
Selections: make(map[*ast.SelectorExpr]*types.Selection),
}
typeparams.InitInstanceInfo(lpkg.TypesInfo)
versions.InitFileVersions(lpkg.TypesInfo)
lpkg.TypesSizes = ld.sizes

importer := importerFunc(func(path string) (*types.Package, error) {
Expand Down
8 changes: 3 additions & 5 deletions go/ssa/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import (
"sync"

"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/versions"
)

type opaqueType struct {
Expand Down Expand Up @@ -1744,8 +1745,7 @@ func (b *builder) forStmt(fn *Function, s *ast.ForStmt, label *lblock) {
// Use forStmtGo122 instead if it applies.
if s.Init != nil {
if assign, ok := s.Init.(*ast.AssignStmt); ok && assign.Tok == token.DEFINE {
major, minor := parseGoVersion(fn.goversion)
afterGo122 := major >= 1 && minor >= 22
afterGo122 := versions.Compare(fn.goversion, "go1.21") > 0
if afterGo122 {
b.forStmtGo122(fn, s, label)
return
Expand Down Expand Up @@ -2195,9 +2195,7 @@ func (b *builder) rangeStmt(fn *Function, s *ast.RangeStmt, label *lblock) {
}
}

major, minor := parseGoVersion(fn.goversion)
afterGo122 := major >= 1 && minor >= 22

afterGo122 := versions.Compare(fn.goversion, "go1.21") > 0
if s.Tok == token.DEFINE && !afterGo122 {
// pre-go1.22: If iteration variables are defined (:=), this
// occurs once outside the loop.
Expand Down
3 changes: 2 additions & 1 deletion go/ssa/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sync"

"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/versions"
)

// NewProgram returns a new SSA Program.
Expand Down Expand Up @@ -245,7 +246,7 @@ func (prog *Program) CreatePackage(pkg *types.Package, files []*ast.File, info *
if len(files) > 0 {
// Go source package.
for _, file := range files {
goversion := goversionOf(p, file)
goversion := versions.Lang(versions.FileVersions(p.info, file))
for _, decl := range file.Decls {
membersFromDecl(p, decl, goversion)
}
Expand Down
8 changes: 7 additions & 1 deletion go/ssa/interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/interp"
"golang.org/x/tools/go/ssa/ssautil"
"golang.org/x/tools/internal/testenv"
"golang.org/x/tools/internal/typeparams"
)

Expand Down Expand Up @@ -172,7 +173,12 @@ func run(t *testing.T, input string, goroot string) {
t.Skipf("skipping: width32.go checks behavior for a 32-bit int")
}

conf := loader.Config{Build: &ctx}
gover := ""
if p := testenv.Go1Point(); p > 0 {
gover = fmt.Sprintf("go1.%d", p)
}

conf := loader.Config{Build: &ctx, TypeChecker: types.Config{GoVersion: gover}}
if _, err := conf.FromArgs([]string{input}, true); err != nil {
t.Fatalf("FromArgs(%s) failed: %s", input, err)
}
Expand Down
26 changes: 0 additions & 26 deletions go/ssa/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ package ssa
// the originating syntax, as specified.

import (
"fmt"
"go/ast"
"go/token"
"go/types"
Expand Down Expand Up @@ -132,31 +131,6 @@ func findNamedFunc(pkg *Package, pos token.Pos) *Function {
return nil
}

// goversionOf returns the goversion of a node in the package
// where the node is either a function declaration or the initial
// value of a package level variable declaration.
func goversionOf(p *Package, file *ast.File) string {
if p.info == nil {
return ""
}

// TODO(taking): Update to the following when internal/versions available:
// return versions.Lang(versions.FileVersions(p.info, file))
return fileVersions(file)
}

// TODO(taking): Remove when internal/versions is available.
var fileVersions = func(file *ast.File) string { return "" }

// parses a goXX.YY version or returns a negative version on an error.
// TODO(taking): Switch to a permanent solution when internal/versions is submitted.
func parseGoVersion(x string) (major, minor int) {
if _, err := fmt.Sscanf(x, "go%d.%d", &major, &minor); err != nil || major < 0 || minor < 0 {
return -1, -1
}
return
}

// ValueForExpr returns the SSA Value that corresponds to non-constant
// expression e.
//
Expand Down
3 changes: 2 additions & 1 deletion go/ssa/ssautil/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/versions"
)

// Packages creates an SSA program for a set of packages.
Expand Down Expand Up @@ -165,7 +166,7 @@ func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, fil
Selections: make(map[*ast.SelectorExpr]*types.Selection),
}
typeparams.InitInstanceInfo(info)
// versions.InitFileVersions(info) // TODO(taking): Enable when internal/versions is available.
versions.InitFileVersions(info)
if err := types.NewChecker(tc, fset, pkg, info).Files(files); err != nil {
return nil, nil, err
}
Expand Down
21 changes: 0 additions & 21 deletions go/ssa/versions_go122.go

This file was deleted.

2 changes: 2 additions & 0 deletions go/types/typeutil/callee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"golang.org/x/tools/go/types/typeutil"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/versions"
)

func TestStaticCallee(t *testing.T) {
Expand Down Expand Up @@ -129,6 +130,7 @@ func testStaticCallee(t *testing.T, contents []string) {
Selections: make(map[*ast.SelectorExpr]*types.Selection),
}
typeparams.InitInstanceInfo(info)
versions.InitFileVersions(info)

var files []*ast.File
for i, content := range contents {
Expand Down
2 changes: 2 additions & 0 deletions refactor/satisfy/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"testing"

"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/versions"
"golang.org/x/tools/refactor/satisfy"
)

Expand Down Expand Up @@ -228,6 +229,7 @@ func constraints(t *testing.T, src string) []string {
Selections: make(map[*ast.SelectorExpr]*types.Selection),
}
typeparams.InitInstanceInfo(info)
versions.InitFileVersions(info)
conf := types.Config{
Importer: importer.Default(),
}
Expand Down

0 comments on commit b03756e

Please sign in to comment.