Skip to content

Commit c16c816

Browse files
committed
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 629a7be commit c16c816

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

go/analysis/passes/stdversion/stdversion_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ func Test(t *testing.T) {
2323
analysistest.Run(t, dir, stdversion.Analyzer,
2424
"example.com/a",
2525
"example.com/sub",
26+
"example.com/sub20",
2627
"example.com/old")
2728
}

go/analysis/passes/stdversion/testdata/test.txtar

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ go 1.21
1313

1414
use .
1515
use sub
16+
use sub20
1617
use old
1718

1819
-- go.mod --
@@ -106,3 +107,42 @@ package old
106107
import "go/types"
107108

108109
var _ types.Alias // no diagnostic: go.mod is too old for us to care
110+
111+
-- sub/oldtagged.go --
112+
// The file Go version (1.16) overrides the go.mod Go version (1.21),
113+
// even when this means a downgrade (#67123).
114+
// (stdversion is silent for go.mod versions before 1.21:
115+
// before the forward compatibility regime, the meaning
116+
// of the go.mod version was not clearly defined.)
117+
118+
//go:build go1.16
119+
120+
package sub
121+
122+
import "bytes"
123+
import "go/types"
124+
125+
var _ = bytes.Clone // want `bytes.Clone requires go1.20 or later \(file is go1.16\)`
126+
var _ = types.Alias // want `types.Alias requires go1.22 or later \(file is go1.16\)`
127+
128+
-- sub20/go.mod --
129+
module example.com/sub20
130+
131+
go 1.20
132+
133+
-- sub20/oldtagged.go --
134+
// Same test again, but with a go1.20 mod,
135+
// before the forward compatibility regime:
136+
// The file's build tag effects selection, but
137+
// not language semantics, so stdversion is silent.
138+
139+
//go:build go1.16
140+
141+
package sub
142+
143+
import "bytes"
144+
import "go/types"
145+
146+
var _ = bytes.Clone
147+
var _ = types.Alias
148+

0 commit comments

Comments
 (0)