Skip to content

Commit 574a7f7

Browse files
griesemergopherbot
authored andcommitted
go/types: update FileVersions API to match proposal changes
For #62605. Change-Id: I6e9032eb92db758bf359e7cc9c4cedc1e0fb2309 Reviewed-on: https://go-review.googlesource.com/c/go/+/534018 Auto-Submit: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
1 parent aa8e4c5 commit 574a7f7

File tree

3 files changed

+25
-44
lines changed

3 files changed

+25
-44
lines changed

src/go/types/api.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,11 @@ type Info struct {
286286
// appear in this list.
287287
InitOrder []*Initializer
288288

289-
// _FileVersions maps a file's start position to the file's Go version.
290-
// If the file doesn't specify a version and Config.GoVersion is not
291-
// given, the reported version is the zero version (Major, Minor = 0, 0).
292-
_FileVersions map[token.Pos]_Version
289+
// _FileVersions maps a file to the file's Go version string.
290+
// If the file doesn't specify a version and Config.GoVersion
291+
// is not given, the reported version is the empty string.
292+
// TODO(gri) should this be "go0.0" instead in that case?
293+
_FileVersions map[*ast.File]string
293294
}
294295

295296
func (info *Info) recordTypes() bool {
@@ -414,12 +415,6 @@ func (init *Initializer) String() string {
414415
return buf.String()
415416
}
416417

417-
// A _Version represents a released Go version.
418-
type _Version struct {
419-
_Major int
420-
_Minor int
421-
}
422-
423418
// Check type-checks a package and returns the resulting package object and
424419
// the first error if any. Additionally, if info != nil, Check populates each
425420
// of the non-nil maps in the Info struct.

src/go/types/api_test.go

+13-20
Original file line numberDiff line numberDiff line change
@@ -2778,14 +2778,14 @@ func TestFileVersions(t *testing.T) {
27782778
for _, test := range []struct {
27792779
moduleVersion string
27802780
fileVersion string
2781-
want Version
2781+
wantVersion string
27822782
}{
2783-
{"", "", Version{0, 0}}, // no versions specified
2784-
{"go1.19", "", Version{1, 19}}, // module version specified
2785-
{"", "go1.20", Version{0, 0}}, // file upgrade ignored
2786-
{"go1.19", "go1.20", Version{1, 20}}, // file upgrade permitted
2787-
{"go1.20", "go1.19", Version{1, 20}}, // file downgrade not permitted
2788-
{"go1.21", "go1.19", Version{1, 19}}, // file downgrade permitted (module version is >= go1.21)
2783+
{"", "", ""}, // no versions specified
2784+
{"go1.19", "", "go1.19"}, // module version specified
2785+
{"", "go1.20", ""}, // file upgrade ignored
2786+
{"go1.19", "go1.20", "go1.20"}, // file upgrade permitted
2787+
{"go1.20", "go1.19", "go1.20"}, // file downgrade not permitted
2788+
{"go1.21", "go1.19", "go1.19"}, // file downgrade permitted (module version is >= go1.21)
27892789
} {
27902790
var src string
27912791
if test.fileVersion != "" {
@@ -2794,16 +2794,16 @@ func TestFileVersions(t *testing.T) {
27942794
src += "package p"
27952795

27962796
conf := Config{GoVersion: test.moduleVersion}
2797-
versions := make(map[*token.File]Version)
2797+
versions := make(map[*ast.File]string)
27982798
var info Info
27992799
*_FileVersionsAddr(&info) = versions
28002800
mustTypecheck(src, &conf, &info)
28012801

28022802
n := 0
28032803
for _, v := range versions {
2804-
want := test.want
2805-
if v.Major != want.Major || v.Minor != want.Minor {
2806-
t.Errorf("%q: unexpected file version: got %v, want %v", src, v, want)
2804+
want := test.wantVersion
2805+
if v != want {
2806+
t.Errorf("%q: unexpected file version: got %q, want %q", src, v, want)
28072807
}
28082808
n++
28092809
}
@@ -2813,15 +2813,8 @@ func TestFileVersions(t *testing.T) {
28132813
}
28142814
}
28152815

2816-
// Version must match types._Version exactly.
2817-
// TODO(gri) remove this declaration once types.Version is exported.
2818-
type Version struct {
2819-
Major int
2820-
Minor int
2821-
}
2822-
28232816
// _FileVersionsAddr(conf) returns the address of the field info._FileVersions.
2824-
func _FileVersionsAddr(info *Info) *map[*token.File]Version {
2817+
func _FileVersionsAddr(info *Info) *map[*ast.File]string {
28252818
v := reflect.Indirect(reflect.ValueOf(info))
2826-
return (*map[*token.File]Version)(v.FieldByName("_FileVersions").Addr().UnsafePointer())
2819+
return (*map[*ast.File]string)(v.FieldByName("_FileVersions").Addr().UnsafePointer())
28272820
}

src/go/types/check.go

+7-14
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,10 @@ func (check *Checker) initFiles(files []*ast.File) {
287287
}
288288
}
289289

290+
// collect file versions
290291
for _, file := range check.files {
291-
fbase := file.FileStart
292-
check.recordFileVersion(fbase, check.version) // record package version (possibly zero version)
293-
v, _ := parseGoVersion(file.GoVersion)
294-
if v.major > 0 {
292+
check.recordFileVersion(file, check.conf.GoVersion)
293+
if v, _ := parseGoVersion(file.GoVersion); v.major > 0 {
295294
if v.equal(check.version) {
296295
continue
297296
}
@@ -314,18 +313,12 @@ func (check *Checker) initFiles(files []*ast.File) {
314313
if check.posVers == nil {
315314
check.posVers = make(map[token.Pos]version)
316315
}
317-
check.posVers[fbase] = v
318-
check.recordFileVersion(fbase, v) // overwrite package version
316+
check.posVers[file.FileStart] = v
317+
check.recordFileVersion(file, file.GoVersion) // overwrite package version
319318
}
320319
}
321320
}
322321

323-
// A posVers records that the file starting at pos declares the Go version vers.
324-
type posVers struct {
325-
pos token.Pos
326-
vers version
327-
}
328-
329322
// A bailout panic is used for early termination.
330323
type bailout struct{}
331324

@@ -640,8 +633,8 @@ func (check *Checker) recordScope(node ast.Node, scope *Scope) {
640633
}
641634
}
642635

643-
func (check *Checker) recordFileVersion(pos token.Pos, v version) {
636+
func (check *Checker) recordFileVersion(file *ast.File, version string) {
644637
if m := check._FileVersions; m != nil {
645-
m[pos] = _Version{v.major, v.minor}
638+
m[file] = version
646639
}
647640
}

0 commit comments

Comments
 (0)