gopls/internal/lsp/source: more ITV filtering

This change does additional filtering of intermediate
test variants for the cases where we invoke type
checking indirectly (through filecached derivatives)
not via Snapshot.TypeCheck.

Also:
- document the head-spinning subtlety of ITVs.
- change RemoveIntermediateTestVariants to use a pointer
  to a slice so that it is impossible to forget to use
  the result. D'oh!

Fixes golang/go#59458

Change-Id: I156385456cefeb9dc3a84f23db3b2fa5ef9f244a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/487136
Auto-Submit: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
diff --git a/gopls/internal/lsp/source/rename.go b/gopls/internal/lsp/source/rename.go
index 317de2f..1b3440b 100644
--- a/gopls/internal/lsp/source/rename.go
+++ b/gopls/internal/lsp/source/rename.go
@@ -290,10 +290,11 @@
 func renameOrdinary(ctx context.Context, snapshot Snapshot, f FileHandle, pp protocol.Position, newName string) (map[span.URI][]diff.Edit, error) {
 	// Type-check the referring package and locate the object(s).
 	//
-	// Unlike PackageForFile, we choose the widest variant as,
-	// for non-exported identifiers, it is the only package we need.
-	// (In case you're wondering why 'references' doesn't also want
-	// the widest variant: it computes the union across all variants.)
+	// Unlike NarrowestPackageForFile, this operation prefers the
+	// widest variant as, for non-exported identifiers, it is the
+	// only package we need. (In case you're wondering why
+	// 'references' doesn't also want the widest variant: it
+	// computes the union across all variants.)
 	var targets map[types.Object]ast.Node
 	var pkg Package
 	{
@@ -301,7 +302,7 @@
 		if err != nil {
 			return nil, err
 		}
-		RemoveIntermediateTestVariants(metas)
+		RemoveIntermediateTestVariants(&metas)
 		if len(metas) == 0 {
 			return nil, fmt.Errorf("no package metadata for file %s", f.URI())
 		}