go/types/objectpath: don't panic when receiver is missing a method

Due to a long-standing bug in go/types, the objectpath package cannot
find methods declared on aliases of cgo types.

We should fix the bug in go/types, but also must avoid the panic in
gopls as this fix will not be back-ported to all supported Go versions.

Updates golang/go#59944

Change-Id: I42d94e610ad92d258c8034d59ea3b0ef312ddebb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/492315
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Findley <rfindley@google.com>
diff --git a/go/types/objectpath/objectpath.go b/go/types/objectpath/objectpath.go
index e064a1a..aa7dfac 100644
--- a/go/types/objectpath/objectpath.go
+++ b/go/types/objectpath/objectpath.go
@@ -418,7 +418,13 @@
 		}
 	}
 
-	panic(fmt.Sprintf("couldn't find method %s on type %s", meth, named))
+	// Due to golang/go#59944, go/types fails to associate the receiver with
+	// certain methods on cgo types.
+	//
+	// TODO(rfindley): replace this panic once golang/go#59944 is fixed in all Go
+	// versions gopls supports.
+	return "", false
+	// panic(fmt.Sprintf("couldn't find method %s on type %s; methods: %#v", meth, named, enc.namedMethods(named)))
 }
 
 // find finds obj within type T, returning the path to it, or nil if not found.
diff --git a/gopls/internal/regtest/marker/testdata/fixedbugs/issue59944.txt b/gopls/internal/regtest/marker/testdata/fixedbugs/issue59944.txt
new file mode 100644
index 0000000..118c2df
--- /dev/null
+++ b/gopls/internal/regtest/marker/testdata/fixedbugs/issue59944.txt
@@ -0,0 +1,30 @@
+This test verifies that gopls does not panic when encountering the go/types
+bug described in golang/go#59944: the Bindingf function is not included in
+the methodset of its receiver type.
+
+Adapted from the code in question from the issue.
+
+-- go.mod --
+module example.com
+
+go 1.12
+
+-- cgo.go --
+package x
+
+import "fmt"
+
+/*
+struct layout {
+	int field;
+};
+*/
+import "C"
+
+type Layout = C.struct_layout
+
+// Bindingf is a printf wrapper. This was necessary to trigger the panic in
+// objectpath while encoding facts.
+func (l *Layout) Bindingf(format string, args ...interface{}) {
+	fmt.Printf(format, args...)
+}