Skip to content

Commit

Permalink
go/internal/gcimporter: fix test for Go 1.18 any
Browse files Browse the repository at this point in the history
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 <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
rsc committed Dec 7, 2021
1 parent feb39d0 commit d3358c1
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion go/internal/gcimporter/gcimporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package gcimporter
import (
"bytes"
"fmt"
"go/build"
"go/constant"
"go/types"
"io/ioutil"
Expand Down Expand Up @@ -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)}"},
Expand All @@ -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.
Expand All @@ -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)
}
Expand Down

0 comments on commit d3358c1

Please sign in to comment.