From e06c10712bbb0471577e2d87bedd8e1e07fd7162 Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Wed, 8 Dec 2021 11:47:09 -0500 Subject: [PATCH] internal/lsp: normalize interface{} to any in test comparisons The switch to use any in standard library signatures breaks many of our tests that match signature strings exactly. Fix this by normalizing strings to use 'any' in place of interface{}, before comparing. Updates golang/go#49884 Change-Id: If18ce1035b3206f37d7de6e584cf2c2cae9481c5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/370294 Trust: Robert Findley Run-TryBot: Robert Findley gopls-CI: kokoro TryBot-Result: Gopher Robot Reviewed-by: Russ Cox --- internal/lsp/cmd/test/signature.go | 3 ++- internal/lsp/tests/util.go | 14 +++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/internal/lsp/cmd/test/signature.go b/internal/lsp/cmd/test/signature.go index 0c77da1b553..f6bdaebf312 100644 --- a/internal/lsp/cmd/test/signature.go +++ b/internal/lsp/cmd/test/signature.go @@ -9,6 +9,7 @@ import ( "testing" "golang.org/x/tools/internal/lsp/protocol" + "golang.org/x/tools/internal/lsp/tests" "golang.org/x/tools/internal/span" ) @@ -27,7 +28,7 @@ func (r *runner) SignatureHelp(t *testing.T, spn span.Span, want *protocol.Signa expect := string(r.data.Golden(goldenTag, filename, func() ([]byte, error) { return []byte(got), nil })) - if expect != got { + if tests.NormalizeAny(expect) != tests.NormalizeAny(got) { t.Errorf("signature failed for %s expected:\n%q\ngot:\n%q'", filename, expect, got) } } diff --git a/internal/lsp/tests/util.go b/internal/lsp/tests/util.go index f75677a0e27..11dda1f8edd 100644 --- a/internal/lsp/tests/util.go +++ b/internal/lsp/tests/util.go @@ -252,7 +252,7 @@ func DiffSignatures(spn span.Span, want, got *protocol.SignatureHelp) (string, e } g := got.Signatures[0] w := want.Signatures[0] - if w.Label != g.Label { + if NormalizeAny(w.Label) != NormalizeAny(g.Label) { wLabel := w.Label + "\n" d, err := myers.ComputeEdits("", wLabel, g.Label+"\n") if err != nil { @@ -271,6 +271,14 @@ func DiffSignatures(spn span.Span, want, got *protocol.SignatureHelp) (string, e return "", nil } +// NormalizeAny replaces occurrences of interface{} in input with any. +// +// In Go 1.18, standard library functions were changed to use the 'any' +// alias in place of interface{}, which affects their type string. +func NormalizeAny(input string) string { + return strings.ReplaceAll(input, "interface{}", "any") +} + // DiffCallHierarchyItems returns the diff between expected and actual call locations for incoming/outgoing call hierarchies func DiffCallHierarchyItems(gotCalls []protocol.CallHierarchyItem, expectedCalls []protocol.CallHierarchyItem) string { expected := make(map[protocol.Location]bool) @@ -369,7 +377,7 @@ func CheckCompletionOrder(want, got []protocol.CompletionItem, strictScores bool for _, w := range want { var found bool for i, g := range got { - if w.Label == g.Label && w.Detail == g.Detail && w.Kind == g.Kind { + if w.Label == g.Label && NormalizeAny(w.Detail) == NormalizeAny(g.Detail) && w.Kind == g.Kind { matchedIdxs = append(matchedIdxs, i) found = true @@ -444,7 +452,7 @@ func DiffCompletionItems(want, got []protocol.CompletionItem) string { if w.Label != g.Label { return summarizeCompletionItems(i, want, got, "incorrect Label got %v want %v", g.Label, w.Label) } - if w.Detail != g.Detail { + if NormalizeAny(w.Detail) != NormalizeAny(g.Detail) { return summarizeCompletionItems(i, want, got, "incorrect Detail got %v want %v", g.Detail, w.Detail) } if w.Documentation != "" && !strings.HasPrefix(w.Documentation, "@") {