Skip to content

Commit

Permalink
go/analysis/passes/stdversion: test *.go < go.mod version
Browse files Browse the repository at this point in the history
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 <taking@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
adonovan committed May 3, 2024
1 parent 629a7be commit c16c816
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions go/analysis/passes/stdversion/stdversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
40 changes: 40 additions & 0 deletions go/analysis/passes/stdversion/testdata/test.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go 1.21

use .
use sub
use sub20
use old

-- go.mod --
Expand Down Expand Up @@ -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

0 comments on commit c16c816

Please sign in to comment.