1
0
mirror of https://github.com/golang/go synced 2024-09-30 03:34:51 -06:00

go/types: use file start position as key for Info.FileVersions

Per discussion on CL 515135.

While at it, also use the file start position as key for the
internal map Checker.posVers.

Change-Id: I14e9b1ff9e8ee5e3ba5de181fc9c7ffc39f28261
Reviewed-on: https://go-review.googlesource.com/c/go/+/515656
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Robert Griesemer 2023-08-03 12:55:16 -07:00 committed by Gopher Robot
parent cf39736f87
commit 2420cc0d00
3 changed files with 17 additions and 16 deletions

View File

@ -286,10 +286,10 @@ type Info struct {
// appear in this list.
InitOrder []*Initializer
// _FileVersions maps a file to the file's Go version.
// _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.File]_Version
_FileVersions map[token.Pos]_Version
}
func (info *Info) recordTypes() bool {

View File

@ -99,12 +99,12 @@ type Checker struct {
fset *token.FileSet
pkg *Package
*Info
version version // accepted language version
posVers map[*token.File]version // maps files to versions (may be nil)
nextID uint64 // unique Id for type parameters (first valid Id is 1)
objMap map[Object]*declInfo // maps package-level objects and (non-interface) methods to declaration info
impMap map[importKey]*Package // maps (import path, source directory) to (complete or fake) package
valids instanceLookup // valid *Named (incl. instantiated) types per the validType check
version version // accepted language version
posVers map[token.Pos]version // maps file start positions to versions (may be nil)
nextID uint64 // unique Id for type parameters (first valid Id is 1)
objMap map[Object]*declInfo // maps package-level objects and (non-interface) methods to declaration info
impMap map[importKey]*Package // maps (import path, source directory) to (complete or fake) package
valids instanceLookup // valid *Named (incl. instantiated) types per the validType check
// pkgPathMap maps package names to the set of distinct import paths we've
// seen for that name, anywhere in the import graph. It is used for
@ -288,8 +288,8 @@ func (check *Checker) initFiles(files []*ast.File) {
}
for _, file := range check.files {
tfile := check.fset.File(file.FileStart)
check.recordFileVersion(tfile, check.version) // record package version (possibly zero version)
fbase := file.FileStart
check.recordFileVersion(fbase, check.version) // record package version (possibly zero version)
v, _ := parseGoVersion(file.GoVersion)
if v.major > 0 {
if v.equal(check.version) {
@ -312,10 +312,10 @@ func (check *Checker) initFiles(files []*ast.File) {
continue
}
if check.posVers == nil {
check.posVers = make(map[*token.File]version)
check.posVers = make(map[token.Pos]version)
}
check.posVers[tfile] = v
check.recordFileVersion(tfile, v) // overwrite package version
check.posVers[fbase] = v
check.recordFileVersion(fbase, v) // overwrite package version
}
}
}
@ -640,8 +640,8 @@ func (check *Checker) recordScope(node ast.Node, scope *Scope) {
}
}
func (check *Checker) recordFileVersion(tfile *token.File, v version) {
func (check *Checker) recordFileVersion(pos token.Pos, v version) {
if m := check._FileVersions; m != nil {
m[tfile] = _Version{v.major, v.minor}
m[pos] = _Version{v.major, v.minor}
}
}

View File

@ -133,7 +133,8 @@ func (check *Checker) allowVersion(pkg *Package, at positioner, v version) bool
// If the source file declares its Go version, use that to decide.
if check.posVers != nil {
if src, ok := check.posVers[check.fset.File(at.Pos())]; ok && src.major >= 1 {
fileStart := check.fset.File(at.Pos()).Pos(0)
if src, ok := check.posVers[fileStart]; ok && src.major >= 1 {
return !src.before(v)
}
}