1
0
mirror of https://github.com/golang/go synced 2024-11-23 19:50:06 -07:00

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>
This commit is contained in:
Robert Griesemer 2023-10-09 14:49:37 -07:00 committed by Gopher Robot
parent aa8e4c5e61
commit 574a7f71a5
3 changed files with 25 additions and 44 deletions

View File

@ -286,10 +286,11 @@ type Info struct {
// appear in this list. // appear in this list.
InitOrder []*Initializer InitOrder []*Initializer
// _FileVersions maps a file's start position to the file's Go version. // _FileVersions maps a file to the file's Go version string.
// If the file doesn't specify a version and Config.GoVersion is not // If the file doesn't specify a version and Config.GoVersion
// given, the reported version is the zero version (Major, Minor = 0, 0). // is not given, the reported version is the empty string.
_FileVersions map[token.Pos]_Version // TODO(gri) should this be "go0.0" instead in that case?
_FileVersions map[*ast.File]string
} }
func (info *Info) recordTypes() bool { func (info *Info) recordTypes() bool {
@ -414,12 +415,6 @@ func (init *Initializer) String() string {
return buf.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 // Check type-checks a package and returns the resulting package object and
// the first error if any. Additionally, if info != nil, Check populates each // the first error if any. Additionally, if info != nil, Check populates each
// of the non-nil maps in the Info struct. // of the non-nil maps in the Info struct.

View File

@ -2778,14 +2778,14 @@ func TestFileVersions(t *testing.T) {
for _, test := range []struct { for _, test := range []struct {
moduleVersion string moduleVersion string
fileVersion string fileVersion string
want Version wantVersion string
}{ }{
{"", "", Version{0, 0}}, // no versions specified {"", "", ""}, // no versions specified
{"go1.19", "", Version{1, 19}}, // module version specified {"go1.19", "", "go1.19"}, // module version specified
{"", "go1.20", Version{0, 0}}, // file upgrade ignored {"", "go1.20", ""}, // file upgrade ignored
{"go1.19", "go1.20", Version{1, 20}}, // file upgrade permitted {"go1.19", "go1.20", "go1.20"}, // file upgrade permitted
{"go1.20", "go1.19", Version{1, 20}}, // file downgrade not permitted {"go1.20", "go1.19", "go1.20"}, // file downgrade not permitted
{"go1.21", "go1.19", Version{1, 19}}, // file downgrade permitted (module version is >= go1.21) {"go1.21", "go1.19", "go1.19"}, // file downgrade permitted (module version is >= go1.21)
} { } {
var src string var src string
if test.fileVersion != "" { if test.fileVersion != "" {
@ -2794,16 +2794,16 @@ func TestFileVersions(t *testing.T) {
src += "package p" src += "package p"
conf := Config{GoVersion: test.moduleVersion} conf := Config{GoVersion: test.moduleVersion}
versions := make(map[*token.File]Version) versions := make(map[*ast.File]string)
var info Info var info Info
*_FileVersionsAddr(&info) = versions *_FileVersionsAddr(&info) = versions
mustTypecheck(src, &conf, &info) mustTypecheck(src, &conf, &info)
n := 0 n := 0
for _, v := range versions { for _, v := range versions {
want := test.want want := test.wantVersion
if v.Major != want.Major || v.Minor != want.Minor { if v != want {
t.Errorf("%q: unexpected file version: got %v, want %v", src, v, want) t.Errorf("%q: unexpected file version: got %q, want %q", src, v, want)
} }
n++ n++
} }
@ -2813,15 +2813,8 @@ func TestFileVersions(t *testing.T) {
} }
} }
// Version must match types._Version exactly.
// TODO(gri) remove this declaration once types.Version is exported.
type Version struct {
Major int
Minor int
}
// _FileVersionsAddr(conf) returns the address of the field info._FileVersions. // _FileVersionsAddr(conf) returns the address of the field info._FileVersions.
func _FileVersionsAddr(info *Info) *map[*token.File]Version { func _FileVersionsAddr(info *Info) *map[*ast.File]string {
v := reflect.Indirect(reflect.ValueOf(info)) v := reflect.Indirect(reflect.ValueOf(info))
return (*map[*token.File]Version)(v.FieldByName("_FileVersions").Addr().UnsafePointer()) return (*map[*ast.File]string)(v.FieldByName("_FileVersions").Addr().UnsafePointer())
} }

View File

@ -287,11 +287,10 @@ func (check *Checker) initFiles(files []*ast.File) {
} }
} }
// collect file versions
for _, file := range check.files { for _, file := range check.files {
fbase := file.FileStart check.recordFileVersion(file, check.conf.GoVersion)
check.recordFileVersion(fbase, check.version) // record package version (possibly zero version) if v, _ := parseGoVersion(file.GoVersion); v.major > 0 {
v, _ := parseGoVersion(file.GoVersion)
if v.major > 0 {
if v.equal(check.version) { if v.equal(check.version) {
continue continue
} }
@ -314,18 +313,12 @@ func (check *Checker) initFiles(files []*ast.File) {
if check.posVers == nil { if check.posVers == nil {
check.posVers = make(map[token.Pos]version) check.posVers = make(map[token.Pos]version)
} }
check.posVers[fbase] = v check.posVers[file.FileStart] = v
check.recordFileVersion(fbase, v) // overwrite package version check.recordFileVersion(file, file.GoVersion) // overwrite package version
} }
} }
} }
// A posVers records that the file starting at pos declares the Go version vers.
type posVers struct {
pos token.Pos
vers version
}
// A bailout panic is used for early termination. // A bailout panic is used for early termination.
type bailout struct{} type bailout struct{}
@ -640,8 +633,8 @@ func (check *Checker) recordScope(node ast.Node, scope *Scope) {
} }
} }
func (check *Checker) recordFileVersion(pos token.Pos, v version) { func (check *Checker) recordFileVersion(file *ast.File, version string) {
if m := check._FileVersions; m != nil { if m := check._FileVersions; m != nil {
m[pos] = _Version{v.major, v.minor} m[file] = version
} }
} }