diff --git a/go/analysis/unitchecker/unitchecker.go b/go/analysis/unitchecker/unitchecker.go index 0a40652c1b5..36eed808d87 100644 --- a/go/analysis/unitchecker/unitchecker.go +++ b/go/analysis/unitchecker/unitchecker.go @@ -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. @@ -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 { diff --git a/go/loader/loader.go b/go/loader/loader.go index edf62c2cc03..41ee08b9b0a 100644 --- a/go/loader/loader.go +++ b/go/loader/loader.go @@ -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 @@ -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 diff --git a/go/packages/packages.go b/go/packages/packages.go index fd7ecf77e99..bd79efc1aaf 100644 --- a/go/packages/packages.go +++ b/go/packages/packages.go @@ -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. @@ -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) { diff --git a/go/ssa/builder.go b/go/ssa/builder.go index b64c50b4349..b607c29ad4b 100644 --- a/go/ssa/builder.go +++ b/go/ssa/builder.go @@ -82,6 +82,7 @@ import ( "sync" "golang.org/x/tools/internal/typeparams" + "golang.org/x/tools/internal/versions" ) type opaqueType struct { @@ -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 @@ -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. diff --git a/go/ssa/create.go b/go/ssa/create.go index eaaf4695e85..653ce2e5c3d 100644 --- a/go/ssa/create.go +++ b/go/ssa/create.go @@ -16,6 +16,7 @@ import ( "sync" "golang.org/x/tools/internal/typeparams" + "golang.org/x/tools/internal/versions" ) // NewProgram returns a new SSA Program. @@ -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) } diff --git a/go/ssa/interp/interp_test.go b/go/ssa/interp/interp_test.go index 7e12dd84131..d217ded0783 100644 --- a/go/ssa/interp/interp_test.go +++ b/go/ssa/interp/interp_test.go @@ -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" ) @@ -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) } diff --git a/go/ssa/source.go b/go/ssa/source.go index 7b1eb8527f9..6700305bd9a 100644 --- a/go/ssa/source.go +++ b/go/ssa/source.go @@ -11,7 +11,6 @@ package ssa // the originating syntax, as specified. import ( - "fmt" "go/ast" "go/token" "go/types" @@ -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. // diff --git a/go/ssa/ssautil/load.go b/go/ssa/ssautil/load.go index 67e75cb261a..fb62c2bd413 100644 --- a/go/ssa/ssautil/load.go +++ b/go/ssa/ssautil/load.go @@ -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. @@ -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 } diff --git a/go/ssa/versions_go122.go b/go/ssa/versions_go122.go deleted file mode 100644 index b74165a8e32..00000000000 --- a/go/ssa/versions_go122.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2023 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. - -//go:build go1.22 -// +build go1.22 - -package ssa - -import ( - "go/ast" -) - -func init() { - fileVersions = func(file *ast.File) string { - if maj, min := parseGoVersion(file.GoVersion); maj >= 0 && min >= 0 { - return file.GoVersion - } - return "" - } -} diff --git a/go/types/typeutil/callee_test.go b/go/types/typeutil/callee_test.go index 345236147d8..ed590719b59 100644 --- a/go/types/typeutil/callee_test.go +++ b/go/types/typeutil/callee_test.go @@ -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) { @@ -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 { diff --git a/refactor/satisfy/find_test.go b/refactor/satisfy/find_test.go index 2cbd8c15ca3..995b95f168d 100644 --- a/refactor/satisfy/find_test.go +++ b/refactor/satisfy/find_test.go @@ -16,6 +16,7 @@ import ( "testing" "golang.org/x/tools/internal/typeparams" + "golang.org/x/tools/internal/versions" "golang.org/x/tools/refactor/satisfy" ) @@ -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(), }