From d3358c1fb7d397621cd5dd5f49a8823f07bae94b Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 7 Dec 2021 13:02:37 -0500 Subject: [PATCH] go/internal/gcimporter: fix test for Go 1.18 any In Go 1.18 context uses any, not interface{}. Fix the test in a way that should work for both older and newer versions. For golang/go#49884. Change-Id: Ib8690876c6a9364b98f9c7ba739b953a8d7565d4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/369955 Trust: Russ Cox Run-TryBot: Russ Cox gopls-CI: kokoro TryBot-Result: Gopher Robot Reviewed-by: Robert Findley --- go/internal/gcimporter/gcimporter_test.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/go/internal/gcimporter/gcimporter_test.go b/go/internal/gcimporter/gcimporter_test.go index 9081cfad884..1d61cfc1e2b 100644 --- a/go/internal/gcimporter/gcimporter_test.go +++ b/go/internal/gcimporter/gcimporter_test.go @@ -10,6 +10,7 @@ package gcimporter import ( "bytes" "fmt" + "go/build" "go/constant" "go/types" "io/ioutil" @@ -255,7 +256,7 @@ var importedObjectTests = []struct { {"go/internal/gcimporter.FindPkg", "func FindPkg(path string, srcDir string) (filename string, id string)"}, // interfaces - {"context.Context", "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key interface{}) interface{}}"}, + {"context.Context", "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key any) any}"}, {"crypto.Decrypter", "type Decrypter interface{Decrypt(rand io.Reader, msg []byte, opts DecrypterOpts) (plaintext []byte, err error); Public() PublicKey}"}, {"encoding.BinaryMarshaler", "type BinaryMarshaler interface{MarshalBinary() (data []byte, err error)}"}, {"io.Reader", "type Reader interface{Read(p []byte) (n int, err error)}"}, @@ -264,6 +265,18 @@ var importedObjectTests = []struct { {"go/types.Type", "type Type interface{String() string; Underlying() Type}"}, } +// TODO(rsc): Delete this init func after x/tools no longer needs to test successfully with Go 1.17. +func init() { + if build.Default.ReleaseTags[len(build.Default.ReleaseTags)-1] <= "go1.17" { + for i := range importedObjectTests { + if importedObjectTests[i].name == "context.Context" { + // Expand any to interface{}. + importedObjectTests[i].want = "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key interface{}) interface{}}" + } + } + } +} + func TestImportedTypes(t *testing.T) { testenv.NeedsGo1Point(t, 11) // This package only handles gc export data. @@ -275,6 +288,12 @@ func TestImportedTypes(t *testing.T) { continue // error reported elsewhere } got := types.ObjectString(obj, types.RelativeTo(obj.Pkg())) + + // TODO(rsc): Delete this block once go.dev/cl/368254 lands. + if got != test.want && test.want == strings.ReplaceAll(got, "interface{}", "any") { + got = test.want + } + if got != test.want { t.Errorf("%s: got %q; want %q", test.name, got, test.want) }