Skip to content

Commit

Permalink
go/types, types2: remove local version processing in favor of go/version
Browse files Browse the repository at this point in the history
In the Checker, maintain a map of versions for each file, even if the
file doensn't specify a version. In that case, the version is the module
version.

If Info.FileVersions is set, use that map directly; otherwise allocate
a Checker-local map.

Introduce a new type, goVersion, which represents a Go language version.
This type effectively takes the role of the earlier version struct.
Replace all versions-related logic accordingly and use the go/version
package for version parsing/validation/comparison.

Added more tests.

Fixes #63974.

Change-Id: Ia05ff47a9eae0f0bb03c6b4cb65a7ce0a5857402
Reviewed-on: https://go-review.googlesource.com/c/go/+/541395
Run-TryBot: Robert Griesemer <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
  • Loading branch information
griesemer committed Nov 15, 2023
1 parent 3073f3f commit 56c91c0
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 326 deletions.
36 changes: 32 additions & 4 deletions src/cmd/compile/internal/types2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"cmd/compile/internal/syntax"
"errors"
"fmt"
"internal/goversion"
"internal/testenv"
"reflect"
"regexp"
Expand Down Expand Up @@ -2839,26 +2840,53 @@ var _ = f(1, 2)
}
}

func TestModuleVersion(t *testing.T) {
// version go1.dd must be able to typecheck go1.dd.0, go1.dd.1, etc.
goversion := fmt.Sprintf("go1.%d", goversion.Version)
for _, v := range []string{
goversion,
goversion + ".0",
goversion + ".1",
goversion + ".rc",
} {
conf := Config{GoVersion: v}
pkg := mustTypecheck("package p", &conf, nil)
if pkg.GoVersion() != conf.GoVersion {
t.Errorf("got %s; want %s", pkg.GoVersion(), conf.GoVersion)
}
}
}

func TestFileVersions(t *testing.T) {
for _, test := range []struct {
moduleVersion string
fileVersion string
wantVersion string
goVersion string
fileVersion string
wantVersion string
}{
{"", "", ""}, // 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)

// versions containing release numbers
// (file versions containing release numbers are considered invalid)
{"go1.19.0", "", "go1.19.0"}, // no file version specified
{"go1.20", "go1.20.1", "go1.20"}, // file upgrade ignored
{"go1.20.1", "go1.20", "go1.20.1"}, // file upgrade ignored
{"go1.20.1", "go1.21", "go1.21"}, // file upgrade permitted
{"go1.20.1", "go1.19", "go1.20.1"}, // file downgrade not permitted
{"go1.21.1", "go1.19.1", "go1.21.1"}, // file downgrade not permitted (invalid file version)
{"go1.21.1", "go1.19", "go1.19"}, // file downgrade permitted (module version is >= go1.21)
} {
var src string
if test.fileVersion != "" {
src = "//go:build " + test.fileVersion + "\n"
}
src += "package p"

conf := Config{GoVersion: test.moduleVersion}
conf := Config{GoVersion: test.goVersion}
versions := make(map[*syntax.PosBase]string)
var info Info
info.FileVersions = versions
Expand Down
100 changes: 51 additions & 49 deletions src/cmd/compile/internal/types2/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"fmt"
"go/constant"
"internal/godebug"
"internal/goversion"
. "internal/types/errors"
)

Expand Down Expand Up @@ -107,12 +106,11 @@ type Checker struct {
ctxt *Context // context for de-duplicating instances
pkg *Package
*Info
version version // accepted language version
posVers map[*syntax.PosBase]version // maps file PosBases 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 goVersion // accepted language version
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
Expand All @@ -128,6 +126,7 @@ type Checker struct {
// (initialized by Files, valid only for the duration of check.Files;
// maps and lists are allocated on demand)
files []*syntax.File // list of package files
versions map[*syntax.PosBase]string // maps file bases to version strings (each file has an entry)
imports []*PkgName // list of imported packages
dotImportMap map[dotImportKey]*PkgName // maps dot-imported objects to the package they were dot-imported through
recvTParamMap map[*syntax.Name]*TypeParam // maps blank receiver type parameters to their type
Expand Down Expand Up @@ -261,6 +260,7 @@ func NewChecker(conf *Config, pkg *Package, info *Info) *Checker {
ctxt: conf.Context,
pkg: pkg,
Info: info,
version: asGoVersion(conf.GoVersion),
objMap: make(map[Object]*declInfo),
impMap: make(map[importKey]*Package),
}
Expand Down Expand Up @@ -302,36 +302,51 @@ func (check *Checker) initFiles(files []*syntax.File) {
}
}

// reuse Info.FileVersions if provided
versions := check.Info.FileVersions
if versions == nil {
versions = make(map[*syntax.PosBase]string)
}
check.versions = versions

pkgVersionOk := check.version.isValid()
downgradeOk := check.version.cmp(go1_21) >= 0

// determine Go version for each file
for _, file := range check.files {
fbase := base(file.Pos()) // fbase may be nil for tests
check.recordFileVersion(fbase, check.conf.GoVersion) // record package version (possibly zero version)
v, _ := parseGoVersion(file.GoVersion)
if v.major > 0 {
if v.equal(check.version) {
continue
}
// Go 1.21 introduced the feature of setting the go.mod
// go line to an early version of Go and allowing //go:build lines
// to “upgrade” the Go version in a given file.
// We can do that backwards compatibly.
// Go 1.21 also introduced the feature of allowing //go:build lines
// to “downgrade” the Go version in a given file.
// That can't be done compatibly in general, since before the
// build lines were ignored and code got the module's Go version.
// To work around this, downgrades are only allowed when the
// module's Go version is Go 1.21 or later.
// If there is no check.version, then we don't really know what Go version to apply.
// Legacy tools may do this, and they historically have accepted everything.
// Preserve that behavior by ignoring //go:build constraints entirely in that case.
if (v.before(check.version) && check.version.before(go1_21)) || check.version.equal(go0_0) {
continue
}
if check.posVers == nil {
check.posVers = make(map[*syntax.PosBase]version)
// use unaltered Config.GoVersion by default
// (This version string may contain dot-release numbers as in go1.20.1,
// unlike file versions which are Go language versions only, if valid.)
v := check.conf.GoVersion
// use the file version, if applicable
// (file versions are either the empty string or of the form go1.dd)
if pkgVersionOk {
fileVersion := asGoVersion(file.GoVersion)
if fileVersion.isValid() {
cmp := fileVersion.cmp(check.version)
// Go 1.21 introduced the feature of setting the go.mod
// go line to an early version of Go and allowing //go:build lines
// to “upgrade” (cmp > 0) the Go version in a given file.
// We can do that backwards compatibly.
//
// Go 1.21 also introduced the feature of allowing //go:build lines
// to “downgrade” (cmp < 0) the Go version in a given file.
// That can't be done compatibly in general, since before the
// build lines were ignored and code got the module's Go version.
// To work around this, downgrades are only allowed when the
// module's Go version is Go 1.21 or later.
//
// If there is no valid check.version, then we don't really know what
// Go version to apply.
// Legacy tools may do this, and they historically have accepted everything.
// Preserve that behavior by ignoring //go:build constraints entirely in that
// case (!pkgVersionOk).
if cmp > 0 || cmp < 0 && downgradeOk {
v = file.GoVersion
}
}
check.posVers[fbase] = v
check.recordFileVersion(fbase, file.GoVersion) // overwrite package version
}
versions[base(file.Pos())] = v // base(file.Pos()) may be nil for tests
}
}

Expand Down Expand Up @@ -362,15 +377,8 @@ func (check *Checker) checkFiles(files []*syntax.File) (err error) {
return nil
}

// Note: parseGoVersion and the subsequent checks should happen once,
// when we create a new Checker, not for each batch of files.
// We can't change it at this point because NewChecker doesn't
// return an error.
check.version, err = parseGoVersion(check.conf.GoVersion)
if err != nil {
return err
}
if check.version.after(version{1, goversion.Version}) {
// Note: NewChecker doesn't return an error, so we need to check the version here.
if check.version.cmp(go_current) > 0 {
return fmt.Errorf("package requires newer Go version %v", check.version)
}
if check.conf.FakeImportC && check.conf.go115UsesCgo {
Expand Down Expand Up @@ -694,9 +702,3 @@ func (check *Checker) recordScope(node syntax.Node, scope *Scope) {
m[node] = scope
}
}

func (check *Checker) recordFileVersion(fbase *syntax.PosBase, version string) {
if m := check.FileVersions; m != nil {
m[fbase] = version
}
}
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/types2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (check *Checker) softErrorf(at poser, code Code, format string, args ...int
check.err(at, code, check.sprintf(format, args...), true)
}

func (check *Checker) versionErrorf(at poser, v version, format string, args ...interface{}) {
func (check *Checker) versionErrorf(at poser, v goVersion, format string, args ...interface{}) {
msg := check.sprintf(format, args...)
msg = fmt.Sprintf("%s requires %s or later", msg, v)
check.err(at, UnsupportedFeature, msg, true)
Expand Down
128 changes: 43 additions & 85 deletions src/cmd/compile/internal/types2/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,90 +7,46 @@ package types2
import (
"cmd/compile/internal/syntax"
"fmt"
"go/version"
"internal/goversion"
"strings"
)

// A version represents a released Go version.
type version struct {
major, minor int
}

func (v version) String() string {
return fmt.Sprintf("go%d.%d", v.major, v.minor)
}
// A goVersion is a Go language version string of the form "go1.%d"
// where d is the minor version number. goVersion strings don't
// contain release numbers ("go1.20.1" is not a valid goVersion).
type goVersion string

func (v version) equal(u version) bool {
return v.major == u.major && v.minor == u.minor
// asGoVersion returns v as a goVersion (e.g., "go1.20.1" becomes "go1.20").
// If v is not a valid Go version, the result is the empty string.
func asGoVersion(v string) goVersion {
return goVersion(version.Lang(v))
}

func (v version) before(u version) bool {
return v.major < u.major || v.major == u.major && v.minor < u.minor
// isValid reports whether v is a valid Go version.
func (v goVersion) isValid() bool {
return v != ""
}

func (v version) after(u version) bool {
return v.major > u.major || v.major == u.major && v.minor > u.minor
// cmp returns -1, 0, or +1 depending on whether x < y, x == y, or x > y,
// interpreted as Go versions.
func (x goVersion) cmp(y goVersion) int {
return version.Compare(string(x), string(y))
}

// Go versions that introduced language changes.
var (
go0_0 = version{0, 0} // no version specified
go1_9 = version{1, 9}
go1_13 = version{1, 13}
go1_14 = version{1, 14}
go1_17 = version{1, 17}
go1_18 = version{1, 18}
go1_20 = version{1, 20}
go1_21 = version{1, 21}
)
// Go versions that introduced language changes
go1_9 = asGoVersion("go1.9")
go1_13 = asGoVersion("go1.13")
go1_14 = asGoVersion("go1.14")
go1_17 = asGoVersion("go1.17")
go1_18 = asGoVersion("go1.18")
go1_20 = asGoVersion("go1.20")
go1_21 = asGoVersion("go1.21")

// parseGoVersion parses a Go version string (such as "go1.12")
// and returns the version, or an error. If s is the empty
// string, the version is 0.0.
func parseGoVersion(s string) (v version, err error) {
bad := func() (version, error) {
return version{}, fmt.Errorf("invalid Go version syntax %q", s)
}
if s == "" {
return
}
if !strings.HasPrefix(s, "go") {
return bad()
}
s = s[len("go"):]
i := 0
for ; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
if i >= 10 || i == 0 && s[i] == '0' {
return bad()
}
v.major = 10*v.major + int(s[i]) - '0'
}
if i > 0 && i == len(s) {
return
}
if i == 0 || s[i] != '.' {
return bad()
}
s = s[i+1:]
if s == "0" {
// We really should not accept "go1.0",
// but we didn't reject it from the start
// and there are now programs that use it.
// So accept it.
return
}
i = 0
for ; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
if i >= 10 || i == 0 && s[i] == '0' {
return bad()
}
v.minor = 10*v.minor + int(s[i]) - '0'
}
// Accept any suffix after the minor number.
// We are only looking for the language version (major.minor)
// but want to accept any valid Go version, like go1.21.0
// and go1.21rc2.
return
}
// current (deployed) Go version
go_current = asGoVersion(fmt.Sprintf("go1.%d", goversion.Version))
)

// langCompat reports an error if the representation of a numeric
// literal is not compatible with the current language version.
Expand Down Expand Up @@ -121,30 +77,30 @@ func (check *Checker) langCompat(lit *syntax.BasicLit) {
}
}

// allowVersion reports whether the given package
// is allowed to use version major.minor.
func (check *Checker) allowVersion(pkg *Package, at poser, v version) bool {
// allowVersion reports whether the given package is allowed to use version v.
func (check *Checker) allowVersion(pkg *Package, at poser, v goVersion) bool {
// We assume that imported packages have all been checked,
// so we only have to check for the local package.
if pkg != check.pkg {
return true
}

// If the source file declares its Go version, use that to decide.
if check.posVers != nil {
if src, ok := check.posVers[base(at.Pos())]; ok && src.major >= 1 {
return !src.before(v)
}
}

// Otherwise fall back to the version in the checker.
return check.version.equal(go0_0) || !check.version.before(v)
// If no explicit file version is specified,
// fileVersion corresponds to the module version.
var fileVersion goVersion
if pos := at.Pos(); pos.IsKnown() {
// We need version.Lang below because file versions
// can be (unaltered) Config.GoVersion strings that
// may contain dot-release information.
fileVersion = asGoVersion(check.versions[base(pos)])
}
return !fileVersion.isValid() || fileVersion.cmp(v) >= 0
}

// verifyVersionf is like allowVersion but also accepts a format string and arguments
// which are used to report a version error if allowVersion returns false. It uses the
// current package.
func (check *Checker) verifyVersionf(at poser, v version, format string, args ...interface{}) bool {
func (check *Checker) verifyVersionf(at poser, v goVersion, format string, args ...interface{}) bool {
if !check.allowVersion(check.pkg, at, v) {
check.versionErrorf(at, v, format, args...)
return false
Expand All @@ -154,7 +110,9 @@ func (check *Checker) verifyVersionf(at poser, v version, format string, args ..

// base finds the underlying PosBase of the source file containing pos,
// skipping over intermediate PosBase layers created by //line directives.
// The positions must be known.
func base(pos syntax.Pos) *syntax.PosBase {
assert(pos.IsKnown())
b := pos.Base()
for {
bb := b.Pos().Base()
Expand Down
Loading

0 comments on commit 56c91c0

Please sign in to comment.