From 344924276c19e46abf4b452856ebf4a49004f37b Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Wed, 3 May 2023 16:53:12 -0400 Subject: [PATCH] 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 TryBot-Result: Gopher Robot Run-TryBot: Robert Findley Auto-Submit: Robert Findley --- go/types/objectpath/objectpath.go | 8 ++++- .../marker/testdata/fixedbugs/issue59944.txt | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 gopls/internal/regtest/marker/testdata/fixedbugs/issue59944.txt diff --git a/go/types/objectpath/objectpath.go b/go/types/objectpath/objectpath.go index e064a1a2926..aa7dfaccf56 100644 --- a/go/types/objectpath/objectpath.go +++ b/go/types/objectpath/objectpath.go @@ -418,7 +418,13 @@ func (enc *Encoder) concreteMethod(meth *types.Func) (Path, bool) { } } - 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 00000000000..118c2df5772 --- /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...) +}