gopls/internal/lsp/source: include ITVs in global references

The global search for references should include intermediate
test variants (ITVs), because the rdeps of every variant are disjoint.
The existing comment was mistaken.

The local search does not need them for the valid reason given in
the existing comment.

The previous call to RemoveIntermediateTestVariants had not
quite the intended effect because we forgot to use its result. (D'oh.)
The effect of the change in the test was to expose the bug
by creating an ordinary test variant.

Updates golang/go#57795

Change-Id: I1852aebf81ae0d169191834eb9a880d0ba90bc6e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/462497
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
diff --git a/gopls/internal/lsp/source/references2.go b/gopls/internal/lsp/source/references2.go
index f24c07f..87347d4 100644
--- a/gopls/internal/lsp/source/references2.go
+++ b/gopls/internal/lsp/source/references2.go
@@ -282,18 +282,6 @@
 		return nil, err
 	}
 
-	// We want the ordinary importable package,
-	// plus any test-augmented variants, since
-	// declarations in _test.go files may change
-	// the reference of a selection, or even a
-	// field into a method or vice versa.
-	//
-	// But we don't need intermediate test variants,
-	// as both their local and global references
-	// will be covered already by equivalent (though
-	// not types.Identical) objects in other variants.
-	RemoveIntermediateTestVariants(metas)
-
 	// The search functions will call report(loc) for each hit.
 	var (
 		refsMu sync.Mutex
@@ -337,6 +325,18 @@
 
 		// local
 		group.Go(func() error {
+			// We want the ordinary importable package,
+			// plus any test-augmented variants, since
+			// declarations in _test.go files may change
+			// the reference of a selection, or even a
+			// field into a method or vice versa.
+			//
+			// But we don't need intermediate test variants,
+			// as their local references will be covered
+			// already by other variants.
+			if m.IsIntermediateTestVariant() {
+				return nil
+			}
 			return localReferences(ctx, snapshot, declURI, declPosn.Offset, m, report)
 		})
 
@@ -346,9 +346,9 @@
 
 		// global
 		group.Go(func() error {
-			// Compute the global-scope query for each variant
-			// of the declaring package in parallel.
-			// We may assume the rdeps of each variant are disjoint.
+			// Compute the global-scope query for every variant
+			// of the declaring package in parallel,
+			// as the rdeps of each variant are disjoint.
 			rdeps, err := snapshot.ReverseDependencies(ctx, m.ID, transitive)
 			if err != nil {
 				return err
diff --git a/gopls/internal/regtest/misc/references_test.go b/gopls/internal/regtest/misc/references_test.go
index e5ee019..bdaf610 100644
--- a/gopls/internal/regtest/misc/references_test.go
+++ b/gopls/internal/regtest/misc/references_test.go
@@ -200,6 +200,9 @@
 	_ = bar.Blah
 }
 
+-- foo/foo_test.go --
+package foo
+
 -- bar/bar.go --
 package bar