// appear in this list.
InitOrder []*Initializer
- // _FileVersions maps a file's start position 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[token.Pos]_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 given, the reported version is the empty string.
+ // TODO(gri) should this be "go0.0" instead in that case?
+ _FileVersions map[*ast.File]string
}
func (info *Info) recordTypes() bool {
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.
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 != "" {
src += "package p"
conf := Config{GoVersion: test.moduleVersion}
- versions := make(map[*token.File]Version)
+ versions := make(map[*ast.File]string)
var info Info
*_FileVersionsAddr(&info) = versions
mustTypecheck(src, &conf, &info)
n := 0
for _, v := range versions {
- want := test.want
- if v.Major != want.Major || v.Minor != want.Minor {
- t.Errorf("%q: unexpected file version: got %v, want %v", src, v, want)
+ want := test.wantVersion
+ if v != want {
+ t.Errorf("%q: unexpected file version: got %q, want %q", src, v, want)
}
n++
}
}
}
-// 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.
-func _FileVersionsAddr(info *Info) *map[*token.File]Version {
+func _FileVersionsAddr(info *Info) *map[*ast.File]string {
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())
}
}
}
+ // collect file versions
for _, file := range check.files {
- fbase := file.FileStart
- check.recordFileVersion(fbase, check.version) // record package version (possibly zero version)
- v, _ := parseGoVersion(file.GoVersion)
- if v.major > 0 {
+ check.recordFileVersion(file, check.conf.GoVersion)
+ if v, _ := parseGoVersion(file.GoVersion); v.major > 0 {
if v.equal(check.version) {
continue
}
if check.posVers == nil {
check.posVers = make(map[token.Pos]version)
}
- check.posVers[fbase] = v
- check.recordFileVersion(fbase, v) // overwrite package version
+ check.posVers[file.FileStart] = v
+ 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.
type bailout struct{}
}
}
-func (check *Checker) recordFileVersion(pos token.Pos, v version) {
+func (check *Checker) recordFileVersion(file *ast.File, version string) {
if m := check._FileVersions; m != nil {
- m[pos] = _Version{v.major, v.minor}
+ m[file] = version
}
}