Skip to content

Commit 91b7a8e

Browse files
committed
go/ssa: use origin method in source lookup
Use the origin method when looking for an enclosing source function. The *types.Func coming from MethodSets.MethodSet may not be in ssa.Package.objects while the origin will be. Fixes golang/go#59428 Change-Id: I0f16e60db69b9b79d1cd1f405134c7f0deda1eeb Reviewed-on: https://go-review.googlesource.com/c/tools/+/491155 Run-TryBot: Tim King <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Alan Donovan <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent ba892bb commit 91b7a8e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

go/ssa/source.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ func findNamedFunc(pkg *Package, pos token.Pos) *Function {
121121
// Don't call Program.Method: avoid creating wrappers.
122122
obj := mset.At(i).Obj().(*types.Func)
123123
if obj.Pos() == pos {
124-
return pkg.objects[obj].(*Function)
124+
// obj from MethodSet may not be the origin type.
125+
m := typeparams.OriginMethod(obj)
126+
return pkg.objects[m].(*Function)
125127
}
126128
}
127129
}

go/ssa/source_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"golang.org/x/tools/go/loader"
2525
"golang.org/x/tools/go/ssa"
2626
"golang.org/x/tools/go/ssa/ssautil"
27+
"golang.org/x/tools/internal/typeparams"
2728
)
2829

2930
func TestObjValueLookup(t *testing.T) {
@@ -384,6 +385,19 @@ func TestEnclosingFunction(t *testing.T) {
384385
func init() { println(func(){print(900)}) }`,
385386
"900", "main.init#1$1"},
386387
}
388+
if typeparams.Enabled {
389+
tests = append(tests, struct {
390+
input string
391+
substr string
392+
fn string
393+
}{
394+
`package main
395+
type S[T any] struct{}
396+
func (*S[T]) Foo() { println(1000) }
397+
type P[T any] struct{ *S[T] }`,
398+
"1000", "(*main.S[T]).Foo",
399+
})
400+
}
387401
for _, test := range tests {
388402
conf := loader.Config{Fset: token.NewFileSet()}
389403
f, start, end := findInterval(t, conf.Fset, test.input, test.substr)

0 commit comments

Comments
 (0)