From c16c816ac486df69471674bcaf6f1a8f76f2bdcf Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 2 May 2024 19:00:58 -0400 Subject: [PATCH] go/analysis/passes/stdversion: test *.go < go.mod version This CL adds two tests for when the file's Go version (set by a //go:build constraint) is lower than the module's Go version. With a go.mod version < go1.21, the start of the "extended forward compatibility" regime, stdversion is silent. But with go1.21+, the build constraint in the file overrides the Go module version, even when lower. Fixes golang/go#67123 Change-Id: Iba37ff2d2c6d0e42a5bd0fe80bbd8dc6b1a25ac9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/582936 Reviewed-by: Tim King LUCI-TryBot-Result: Go LUCI --- .../passes/stdversion/stdversion_test.go | 1 + .../passes/stdversion/testdata/test.txtar | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/go/analysis/passes/stdversion/stdversion_test.go b/go/analysis/passes/stdversion/stdversion_test.go index 91865278ae9..d6a2e4556cd 100644 --- a/go/analysis/passes/stdversion/stdversion_test.go +++ b/go/analysis/passes/stdversion/stdversion_test.go @@ -23,5 +23,6 @@ func Test(t *testing.T) { analysistest.Run(t, dir, stdversion.Analyzer, "example.com/a", "example.com/sub", + "example.com/sub20", "example.com/old") } diff --git a/go/analysis/passes/stdversion/testdata/test.txtar b/go/analysis/passes/stdversion/testdata/test.txtar index 9e66cee8c63..0d27f112b04 100644 --- a/go/analysis/passes/stdversion/testdata/test.txtar +++ b/go/analysis/passes/stdversion/testdata/test.txtar @@ -13,6 +13,7 @@ go 1.21 use . use sub +use sub20 use old -- go.mod -- @@ -106,3 +107,42 @@ package old import "go/types" var _ types.Alias // no diagnostic: go.mod is too old for us to care + +-- sub/oldtagged.go -- +// The file Go version (1.16) overrides the go.mod Go version (1.21), +// even when this means a downgrade (#67123). +// (stdversion is silent for go.mod versions before 1.21: +// before the forward compatibility regime, the meaning +// of the go.mod version was not clearly defined.) + +//go:build go1.16 + +package sub + +import "bytes" +import "go/types" + +var _ = bytes.Clone // want `bytes.Clone requires go1.20 or later \(file is go1.16\)` +var _ = types.Alias // want `types.Alias requires go1.22 or later \(file is go1.16\)` + +-- sub20/go.mod -- +module example.com/sub20 + +go 1.20 + +-- sub20/oldtagged.go -- +// Same test again, but with a go1.20 mod, +// before the forward compatibility regime: +// The file's build tag effects selection, but +// not language semantics, so stdversion is silent. + +//go:build go1.16 + +package sub + +import "bytes" +import "go/types" + +var _ = bytes.Clone +var _ = types.Alias +