Skip to content

Commit 1fc45e9

Browse files
griesemergopherbot
authored andcommitted
cmd/compile: update types2.Info.FileVersions API to match go/types
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 <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent eda42f7 commit 1fc45e9

File tree

7 files changed

+30
-31
lines changed

7 files changed

+30
-31
lines changed

src/cmd/compile/internal/noder/irgen.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
6161
Implicits: make(map[syntax.Node]types2.Object),
6262
Scopes: make(map[syntax.Node]*types2.Scope),
6363
Instances: make(map[*syntax.Name]types2.Instance),
64-
FileVersions: make(map[*syntax.PosBase]types2.Version),
64+
FileVersions: make(map[*syntax.PosBase]string),
6565
// expand as needed
6666
}
6767
conf.Error = func(err error) {
@@ -72,8 +72,7 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
7272
for !posBase.IsFileBase() { // line directive base
7373
posBase = posBase.Pos().Base()
7474
}
75-
v := info.FileVersions[posBase]
76-
fileVersion := fmt.Sprintf("go%d.%d", v.Major, v.Minor)
75+
fileVersion := info.FileVersions[posBase]
7776
file := posBaseMap[posBase]
7877
if file.GoVersion == fileVersion {
7978
// If we have a version error caused by //go:build, report it.

src/cmd/compile/internal/noder/writer.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"go/constant"
1010
"go/token"
11+
"go/version"
1112
"internal/buildcfg"
1213
"internal/pkgbits"
1314
"os"
@@ -1479,8 +1480,8 @@ func (w *writer) forStmt(stmt *syntax.ForStmt) {
14791480

14801481
func (w *writer) distinctVars(stmt *syntax.ForStmt) bool {
14811482
lv := base.Debug.LoopVar
1482-
v := w.p.info.FileVersions[stmt.Pos().Base()]
1483-
is122 := v.Major == 0 && v.Minor == 0 || v.Major == 1 && v.Minor >= 22
1483+
fileVersion := w.p.info.FileVersions[stmt.Pos().Base()]
1484+
is122 := fileVersion == "" || version.Compare(fileVersion, "go1.22") >= 0
14841485

14851486
// Turning off loopvar for 1.22 is only possible with loopvarhash=qn
14861487
//

src/cmd/compile/internal/types2/api.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,12 @@ type Info struct {
294294
// appear in this list.
295295
InitOrder []*Initializer
296296

297-
// FileVersions maps a file's position base to the file's Go version.
298-
// If the file doesn't specify a version and Config.GoVersion is not
299-
// given, the reported version is the zero version (Major, Minor = 0, 0).
300-
FileVersions map[*syntax.PosBase]Version
297+
// FileVersions maps a file to its Go version string.
298+
// If the file doesn't specify a version, the reported
299+
// string is Config.GoVersion.
300+
// Version strings begin with “go”, like “go1.21”, and
301+
// are suitable for use with the [go/version] package.
302+
FileVersions map[*syntax.PosBase]string
301303
}
302304

303305
func (info *Info) recordTypes() bool {
@@ -431,12 +433,6 @@ func (init *Initializer) String() string {
431433
return buf.String()
432434
}
433435

434-
// A Version represents a released Go version.
435-
type Version struct {
436-
Major int
437-
Minor int
438-
}
439-
440436
// Check type-checks a package and returns the resulting package object and
441437
// the first error if any. Additionally, if info != nil, Check populates each
442438
// of the non-nil maps in the Info struct.

src/cmd/compile/internal/types2/api_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -2769,14 +2769,14 @@ func TestFileVersions(t *testing.T) {
27692769
for _, test := range []struct {
27702770
moduleVersion string
27712771
fileVersion string
2772-
want Version
2772+
wantVersion string
27732773
}{
2774-
{"", "", Version{0, 0}}, // no versions specified
2775-
{"go1.19", "", Version{1, 19}}, // module version specified
2776-
{"", "go1.20", Version{0, 0}}, // file upgrade ignored
2777-
{"go1.19", "go1.20", Version{1, 20}}, // file upgrade permitted
2778-
{"go1.20", "go1.19", Version{1, 20}}, // file downgrade not permitted
2779-
{"go1.21", "go1.19", Version{1, 19}}, // file downgrade permitted (module version is >= go1.21)
2774+
{"", "", ""}, // no versions specified
2775+
{"go1.19", "", "go1.19"}, // module version specified
2776+
{"", "go1.20", ""}, // file upgrade ignored
2777+
{"go1.19", "go1.20", "go1.20"}, // file upgrade permitted
2778+
{"go1.20", "go1.19", "go1.20"}, // file downgrade not permitted
2779+
{"go1.21", "go1.19", "go1.19"}, // file downgrade permitted (module version is >= go1.21)
27802780
} {
27812781
var src string
27822782
if test.fileVersion != "" {
@@ -2785,15 +2785,15 @@ func TestFileVersions(t *testing.T) {
27852785
src += "package p"
27862786

27872787
conf := Config{GoVersion: test.moduleVersion}
2788-
versions := make(map[*syntax.PosBase]Version)
2788+
versions := make(map[*syntax.PosBase]string)
27892789
var info Info
27902790
info.FileVersions = versions
27912791
mustTypecheck(src, &conf, &info)
27922792

27932793
n := 0
27942794
for _, v := range info.FileVersions {
2795-
want := test.want
2796-
if v.Major != want.Major || v.Minor != want.Minor {
2795+
want := test.wantVersion
2796+
if v != want {
27972797
t.Errorf("%q: unexpected file version: got %v, want %v", src, v, want)
27982798
}
27992799
n++

src/cmd/compile/internal/types2/check.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ func (check *Checker) initFiles(files []*syntax.File) {
292292
}
293293

294294
for _, file := range check.files {
295-
fbase := base(file.Pos()) // fbase may be nil for tests
296-
check.recordFileVersion(fbase, check.version) // record package version (possibly zero version)
295+
fbase := base(file.Pos()) // fbase may be nil for tests
296+
check.recordFileVersion(fbase, check.conf.GoVersion) // record package version (possibly zero version)
297297
v, _ := parseGoVersion(file.GoVersion)
298298
if v.major > 0 {
299299
if v.equal(check.version) {
@@ -319,7 +319,7 @@ func (check *Checker) initFiles(files []*syntax.File) {
319319
check.posVers = make(map[*syntax.PosBase]version)
320320
}
321321
check.posVers[fbase] = v
322-
check.recordFileVersion(fbase, v) // overwrite package version
322+
check.recordFileVersion(fbase, file.GoVersion) // overwrite package version
323323
}
324324
}
325325
}
@@ -684,8 +684,8 @@ func (check *Checker) recordScope(node syntax.Node, scope *Scope) {
684684
}
685685
}
686686

687-
func (check *Checker) recordFileVersion(fbase *syntax.PosBase, v version) {
687+
func (check *Checker) recordFileVersion(fbase *syntax.PosBase, version string) {
688688
if m := check.FileVersions; m != nil {
689-
m[fbase] = Version{v.major, v.minor}
689+
m[fbase] = version
690690
}
691691
}

src/cmd/dist/buildtool.go

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
// include all packages within subdirectories as well.
3232
// These will be imported during bootstrap as bootstrap/name, like bootstrap/math/big.
3333
var bootstrapDirs = []string{
34+
"cmp",
3435
"cmd/asm",
3536
"cmd/asm/internal/...",
3637
"cmd/cgo",
@@ -61,6 +62,7 @@ var bootstrapDirs = []string{
6162
"debug/pe",
6263
"go/build/constraint",
6364
"go/constant",
65+
"go/version",
6466
"internal/abi",
6567
"internal/coverage",
6668
"cmd/internal/cov/covcmd",
@@ -70,6 +72,7 @@ var bootstrapDirs = []string{
7072
"internal/godebugs",
7173
"internal/goexperiment",
7274
"internal/goroot",
75+
"internal/gover",
7376
"internal/goversion",
7477
// internal/lazyregexp is provided by Go 1.17, which permits it to
7578
// be imported by other packages in this list, but is not provided

src/go/types/check.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func (check *Checker) initFiles(files []*ast.File) {
296296

297297
// collect file versions
298298
for _, file := range check.files {
299-
check.recordFileVersion(file, check.conf.GoVersion)
299+
check.recordFileVersion(file, check.conf.GoVersion) // record package version (possibly zero version)
300300
if v, _ := parseGoVersion(file.GoVersion); v.major > 0 {
301301
if v.equal(check.version) {
302302
continue

0 commit comments

Comments
 (0)