Skip to content

Commit 3b51f1f

Browse files
authored
Merge pull request github#16683 from owen-mc/go/refactor-extractor
Go: Refactor findMethodWithGivenReceiver
2 parents e267031 + fcf06c5 commit 3b51f1f

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

go/extractor/trap/labels.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,9 @@ func (l *Labeler) ScopedObjectID(object types.Object, getTypeLabel func() Label)
152152
panic(fmt.Sprintf("Object has no scope: %v :: %v.\n", object,
153153
l.tw.Package.Fset.Position(object.Pos())))
154154
} else {
155-
// associate method receiver objects to special keys, because those can be
156-
// referenced from other files via their method
157-
meth := findMethodWithGivenReceiver(object.Type(), object)
158-
if meth == nil {
159-
if pointerType, ok := object.Type().(*types.Pointer); ok {
160-
meth = findMethodWithGivenReceiver(pointerType.Elem(), object)
161-
}
162-
}
163-
164-
if meth != nil {
155+
if meth := findMethodWithGivenReceiver(object); meth != nil {
156+
// associate method receiver objects to special keys, because those can be
157+
// referenced from other files via their method
165158
methlbl, _ := l.MethodID(meth, getTypeLabel())
166159
label, _ = l.ReceiverObjectID(object, methlbl)
167160
} else {
@@ -174,7 +167,20 @@ func (l *Labeler) ScopedObjectID(object types.Object, getTypeLabel func() Label)
174167
return label, exists
175168
}
176169

177-
func findMethodWithGivenReceiver(tp types.Type, object types.Object) *types.Func {
170+
// findMethodWithGivenReceiver finds a method with `object` as its receiver, if one exists
171+
func findMethodWithGivenReceiver(object types.Object) *types.Func {
172+
meth := findMethodOnTypeWithGivenReceiver(object.Type(), object)
173+
if meth != nil {
174+
return meth
175+
}
176+
if pointerType, ok := object.Type().(*types.Pointer); ok {
177+
meth = findMethodOnTypeWithGivenReceiver(pointerType.Elem(), object)
178+
}
179+
return meth
180+
}
181+
182+
// findMethodWithGivenReceiver finds a method on type `tp` with `object` as its receiver, if one exists
183+
func findMethodOnTypeWithGivenReceiver(tp types.Type, object types.Object) *types.Func {
178184
if namedType, ok := tp.(*types.Named); ok {
179185
for i := 0; i < namedType.NumMethods(); i++ {
180186
meth := namedType.Method(i)

0 commit comments

Comments
 (0)