Skip to content

Commit

Permalink
go/types/objectpath: don't panic when receiver is missing a method
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
findleyr authored and gopherbot committed May 9, 2023
1 parent 0809ec2 commit 3449242
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion go/types/objectpath/objectpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 30 additions & 0 deletions gopls/internal/regtest/marker/testdata/fixedbugs/issue59944.txt
Original file line number Diff line number Diff line change
@@ -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...)
}

0 comments on commit 3449242

Please sign in to comment.