Skip to content

Commit e6bef92

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/golang: downgrade assertion in methodsets index
A query for exported method T.M triggers a paradoxical methodset(T)={} assertion. But an ill-typed T may have both a field and a method named M causing the methodset to indeed be empty. + Test. Fixes golang/go#67978 Change-Id: Iecbf98ff53afab00ebb51f83ed261a0318b42771 Reviewed-on: https://go-review.googlesource.com/c/tools/+/605015 Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 7f262d6 commit e6bef92

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

gopls/internal/golang/references.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"golang.org/x/tools/gopls/internal/cache/parsego"
3232
"golang.org/x/tools/gopls/internal/file"
3333
"golang.org/x/tools/gopls/internal/protocol"
34-
"golang.org/x/tools/gopls/internal/util/bug"
3534
"golang.org/x/tools/gopls/internal/util/safetoken"
3635
"golang.org/x/tools/internal/event"
3736
)
@@ -508,7 +507,10 @@ func expandMethodSearch(ctx context.Context, snapshot *cache.Snapshot, workspace
508507
// Compute the method-set fingerprint used as a key to the global search.
509508
key, hasMethods := methodsets.KeyOf(recv)
510509
if !hasMethods {
511-
return bug.Errorf("KeyOf(%s)={} yet %s is a method", recv, method)
510+
// The query object was method T.m, but methodset(T)={}:
511+
// this indicates that ill-typed T has conflicting fields and methods.
512+
// Rather than bug-report (#67978), treat the empty method set at face value.
513+
return nil
512514
}
513515
// Search the methodset index of each package in the workspace.
514516
indexes, err := snapshot.MethodSets(ctx, workspaceIDs...)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
This test exercises a references query on an exported method that
3+
conflicts with a field name. This ill-typed input violates the
4+
assumption that if type T has a method, then the method set of T is
5+
nonempty, which led to a crash.
6+
7+
See https://github.com/golang/go/issues/67978.
8+
9+
-- a.go --
10+
package p
11+
12+
type E struct { X int } //@ diag(re"()X", re"field.*same name")
13+
14+
func (E) X() {} //@ loc(a, "X"), refs("X", a, b), diag(re"()X", re"method.*same name")
15+
16+
var _ = new(E).X //@ loc(b, "X")
17+
18+

0 commit comments

Comments
 (0)