]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile: update types2.Info.FileVersions API to match go/types
authorRobert Griesemer <gri@golang.org>
Thu, 9 Nov 2023 22:37:56 +0000 (14:37 -0800)
committerGopher Robot <gobot@golang.org>
Fri, 10 Nov 2023 02:22:15 +0000 (02:22 +0000)
This CL changes the FileVersions map to map to version strings
rather than Version structs, for use with the new go/versions
package.

Adjust the cmd/dist bootstrap package list to include go/version.

Adjust the compiler's noder to work with the new API.

For #62605.
For #63974.

Change-Id: I191a7015ba3fb61c646e9f9d3c3dbafc9653ccb5
Reviewed-on: https://go-review.googlesource.com/c/go/+/541296
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/cmd/compile/internal/noder/irgen.go
src/cmd/compile/internal/noder/writer.go
src/cmd/compile/internal/types2/api.go
src/cmd/compile/internal/types2/api_test.go
src/cmd/compile/internal/types2/check.go
src/cmd/dist/buildtool.go
src/go/types/check.go

index e77119695ba6e90dc3b8611523cdecb66a53b8ab..46511d1f9736ed737223d5223b7e51b2a53156de 100644 (file)
@@ -61,7 +61,7 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
                Implicits:          make(map[syntax.Node]types2.Object),
                Scopes:             make(map[syntax.Node]*types2.Scope),
                Instances:          make(map[*syntax.Name]types2.Instance),
-               FileVersions:       make(map[*syntax.PosBase]types2.Version),
+               FileVersions:       make(map[*syntax.PosBase]string),
                // expand as needed
        }
        conf.Error = func(err error) {
@@ -72,8 +72,7 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
                        for !posBase.IsFileBase() { // line directive base
                                posBase = posBase.Pos().Base()
                        }
-                       v := info.FileVersions[posBase]
-                       fileVersion := fmt.Sprintf("go%d.%d", v.Major, v.Minor)
+                       fileVersion := info.FileVersions[posBase]
                        file := posBaseMap[posBase]
                        if file.GoVersion == fileVersion {
                                // If we have a version error caused by //go:build, report it.
index 08da36eccbb595a8fbd1fefad4d0617d1befe4bb..d75caf064a99b80a109ef5ca0cdd479a5258e63c 100644 (file)
@@ -8,6 +8,7 @@ import (
        "fmt"
        "go/constant"
        "go/token"
+       "go/version"
        "internal/buildcfg"
        "internal/pkgbits"
        "os"
@@ -1479,8 +1480,8 @@ func (w *writer) forStmt(stmt *syntax.ForStmt) {
 
 func (w *writer) distinctVars(stmt *syntax.ForStmt) bool {
        lv := base.Debug.LoopVar
-       v := w.p.info.FileVersions[stmt.Pos().Base()]
-       is122 := v.Major == 0 && v.Minor == 0 || v.Major == 1 && v.Minor >= 22
+       fileVersion := w.p.info.FileVersions[stmt.Pos().Base()]
+       is122 := fileVersion == "" || version.Compare(fileVersion, "go1.22") >= 0
 
        // Turning off loopvar for 1.22 is only possible with loopvarhash=qn
        //
index d807096db599900300dd784714155ba04f38bae0..675882c49f1b8d874e6355d8b9b6cd322e33be8c 100644 (file)
@@ -294,10 +294,12 @@ type Info struct {
        // appear in this list.
        InitOrder []*Initializer
 
-       // FileVersions maps a file's position base to the file's Go version.
-       // If the file doesn't specify a version and Config.GoVersion is not
-       // given, the reported version is the zero version (Major, Minor = 0, 0).
-       FileVersions map[*syntax.PosBase]Version
+       // FileVersions maps a file to its Go version string.
+       // If the file doesn't specify a version, the reported
+       // string is Config.GoVersion.
+       // Version strings begin with “go”, like “go1.21”, and
+       // are suitable for use with the [go/version] package.
+       FileVersions map[*syntax.PosBase]string
 }
 
 func (info *Info) recordTypes() bool {
@@ -431,12 +433,6 @@ func (init *Initializer) String() string {
        return buf.String()
 }
 
-// A Version represents a released Go version.
-type Version struct {
-       Major int
-       Minor int
-}
-
 // Check type-checks a package and returns the resulting package object and
 // the first error if any. Additionally, if info != nil, Check populates each
 // of the non-nil maps in the Info struct.
index 4cabad2e9e28b468e7fbb9b1992ceefbdaedcd20..f5bdcf213daf14b2b319272974306deac23400d1 100644 (file)
@@ -2769,14 +2769,14 @@ func TestFileVersions(t *testing.T) {
        for _, test := range []struct {
                moduleVersion string
                fileVersion   string
-               want          Version
+               wantVersion   string
        }{
-               {"", "", Version{0, 0}},              // no versions specified
-               {"go1.19", "", Version{1, 19}},       // module version specified
-               {"", "go1.20", Version{0, 0}},        // file upgrade ignored
-               {"go1.19", "go1.20", Version{1, 20}}, // file upgrade permitted
-               {"go1.20", "go1.19", Version{1, 20}}, // file downgrade not permitted
-               {"go1.21", "go1.19", Version{1, 19}}, // file downgrade permitted (module version is >= go1.21)
+               {"", "", ""},                   // no versions specified
+               {"go1.19", "", "go1.19"},       // module version specified
+               {"", "go1.20", ""},             // file upgrade ignored
+               {"go1.19", "go1.20", "go1.20"}, // file upgrade permitted
+               {"go1.20", "go1.19", "go1.20"}, // file downgrade not permitted
+               {"go1.21", "go1.19", "go1.19"}, // file downgrade permitted (module version is >= go1.21)
        } {
                var src string
                if test.fileVersion != "" {
@@ -2785,15 +2785,15 @@ func TestFileVersions(t *testing.T) {
                src += "package p"
 
                conf := Config{GoVersion: test.moduleVersion}
-               versions := make(map[*syntax.PosBase]Version)
+               versions := make(map[*syntax.PosBase]string)
                var info Info
                info.FileVersions = versions
                mustTypecheck(src, &conf, &info)
 
                n := 0
                for _, v := range info.FileVersions {
-                       want := test.want
-                       if v.Major != want.Major || v.Minor != want.Minor {
+                       want := test.wantVersion
+                       if v != want {
                                t.Errorf("%q: unexpected file version: got %v, want %v", src, v, want)
                        }
                        n++
index 3748926e402a933d146b24b4fb039c7be9f2c33a..60422d8dd9e297a5e95a721a66652242f5ca70cf 100644 (file)
@@ -292,8 +292,8 @@ func (check *Checker) initFiles(files []*syntax.File) {
        }
 
        for _, file := range check.files {
-               fbase := base(file.Pos())                     // fbase may be nil for tests
-               check.recordFileVersion(fbase, check.version) // record package version (possibly zero version)
+               fbase := base(file.Pos())                            // fbase may be nil for tests
+               check.recordFileVersion(fbase, check.conf.GoVersion) // record package version (possibly zero version)
                v, _ := parseGoVersion(file.GoVersion)
                if v.major > 0 {
                        if v.equal(check.version) {
@@ -319,7 +319,7 @@ func (check *Checker) initFiles(files []*syntax.File) {
                                check.posVers = make(map[*syntax.PosBase]version)
                        }
                        check.posVers[fbase] = v
-                       check.recordFileVersion(fbase, v) // overwrite package version
+                       check.recordFileVersion(fbase, file.GoVersion) // overwrite package version
                }
        }
 }
@@ -684,8 +684,8 @@ func (check *Checker) recordScope(node syntax.Node, scope *Scope) {
        }
 }
 
-func (check *Checker) recordFileVersion(fbase *syntax.PosBase, v version) {
+func (check *Checker) recordFileVersion(fbase *syntax.PosBase, version string) {
        if m := check.FileVersions; m != nil {
-               m[fbase] = Version{v.major, v.minor}
+               m[fbase] = version
        }
 }
index c20ba4f3a080bded73b881cb6df0e44de2100288..3232896f262564f66d0565854eb249fd01e3e0af 100644 (file)
@@ -31,6 +31,7 @@ import (
 // include all packages within subdirectories as well.
 // These will be imported during bootstrap as bootstrap/name, like bootstrap/math/big.
 var bootstrapDirs = []string{
+       "cmp",
        "cmd/asm",
        "cmd/asm/internal/...",
        "cmd/cgo",
@@ -61,6 +62,7 @@ var bootstrapDirs = []string{
        "debug/pe",
        "go/build/constraint",
        "go/constant",
+       "go/version",
        "internal/abi",
        "internal/coverage",
        "cmd/internal/cov/covcmd",
@@ -70,6 +72,7 @@ var bootstrapDirs = []string{
        "internal/godebugs",
        "internal/goexperiment",
        "internal/goroot",
+       "internal/gover",
        "internal/goversion",
        // internal/lazyregexp is provided by Go 1.17, which permits it to
        // be imported by other packages in this list, but is not provided
index f9decbf9a0afc1940352cd43c8cf79ec2d52dea7..1fddb450ea47a635edb1b2c40c6505f15e06bcb0 100644 (file)
@@ -296,7 +296,7 @@ func (check *Checker) initFiles(files []*ast.File) {
 
        // collect file versions
        for _, file := range check.files {
-               check.recordFileVersion(file, check.conf.GoVersion)
+               check.recordFileVersion(file, check.conf.GoVersion) // record package version (possibly zero version)
                if v, _ := parseGoVersion(file.GoVersion); v.major > 0 {
                        if v.equal(check.version) {
                                continue