]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types: fix version downgrade bug without Config.GoVersion
authorRuss Cox <rsc@golang.org>
Wed, 19 Apr 2023 20:05:37 +0000 (16:05 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 20 Apr 2023 14:56:22 +0000 (14:56 +0000)
The gVisor team reported a regression in their checkers,
which don't set Config.GoVersion, processing files that say
//go:build go1.13 but still use 'any' (which happened in Go 1.18).
That situation should continue to work, since it worked before,
so add a special case for not knowing the GoVersion.

Change-Id: I8820d8ccbdf76d304e2c7e45f6aaa993ff3d16a6
Reviewed-on: https://go-review.googlesource.com/c/go/+/486398
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/api.go
src/cmd/compile/internal/types2/check.go
src/go/types/api.go
src/go/types/check.go

index 24131192f8fd0401866fb81f3c2d025d999f03e9..bd87945295be6777c0d958379f1e0504d5d321a6 100644 (file)
@@ -112,7 +112,7 @@ type Config struct {
 
        // GoVersion describes the accepted Go language version. The string
        // must follow the format "go%d.%d" (e.g. "go1.12") or ist must be
-       // empty; an empty string indicates the latest language version.
+       // empty; an empty string disables Go language version checks.
        // If the format is invalid, invoking the type checker will cause a
        // panic.
        GoVersion string
index 5f0c521a2a488abd67dc51931dc1c7098f2b9d8b..550fb1cafdaa046b3317bf665816288485204bae 100644 (file)
@@ -299,7 +299,10 @@ func (check *Checker) initFiles(files []*syntax.File) {
                        // 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 v.before(check.version) && check.version.before(version{1, 21}) {
+                       // 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(version{1, 21})) || check.version.equal(version{0, 0}) {
                                continue
                        }
                        if check.posVers == nil {
index a144462968896145867d032052ca80e70361427b..7af84fd244a0cea115d145486ae17e7a4ab3c8f0 100644 (file)
@@ -116,7 +116,7 @@ type Config struct {
 
        // GoVersion describes the accepted Go language version. The string
        // must follow the format "go%d.%d" (e.g. "go1.12") or it must be
-       // empty; an empty string indicates the latest language version.
+       // empty; an empty string disables Go language version checks.
        // If the format is invalid, invoking the type checker will cause a
        // panic.
        GoVersion string
index 58cf6d060cb5e78794a60c91f68fb42c3098f8fb..5381b5db68765c1f01a4b1a4ec2e7ce3ea316483 100644 (file)
@@ -302,7 +302,10 @@ func (check *Checker) initFiles(files []*ast.File) {
                        // 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 v.before(check.version) && check.version.before(version{1, 21}) {
+                       // 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(version{1, 21})) || check.version.equal(version{0, 0}) {
                                continue
                        }
                        if check.posVers == nil {