Skip to content

Commit 3449242

Browse files
findleyrgopherbot
authored andcommitted
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>
1 parent 0809ec2 commit 3449242

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

go/types/objectpath/objectpath.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,13 @@ func (enc *Encoder) concreteMethod(meth *types.Func) (Path, bool) {
418418
}
419419
}
420420

421-
panic(fmt.Sprintf("couldn't find method %s on type %s", meth, named))
421+
// Due to golang/go#59944, go/types fails to associate the receiver with
422+
// certain methods on cgo types.
423+
//
424+
// TODO(rfindley): replace this panic once golang/go#59944 is fixed in all Go
425+
// versions gopls supports.
426+
return "", false
427+
// panic(fmt.Sprintf("couldn't find method %s on type %s; methods: %#v", meth, named, enc.namedMethods(named)))
422428
}
423429

424430
// find finds obj within type T, returning the path to it, or nil if not found.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
This test verifies that gopls does not panic when encountering the go/types
2+
bug described in golang/go#59944: the Bindingf function is not included in
3+
the methodset of its receiver type.
4+
5+
Adapted from the code in question from the issue.
6+
7+
-- go.mod --
8+
module example.com
9+
10+
go 1.12
11+
12+
-- cgo.go --
13+
package x
14+
15+
import "fmt"
16+
17+
/*
18+
struct layout {
19+
int field;
20+
};
21+
*/
22+
import "C"
23+
24+
type Layout = C.struct_layout
25+
26+
// Bindingf is a printf wrapper. This was necessary to trigger the panic in
27+
// objectpath while encoding facts.
28+
func (l *Layout) Bindingf(format string, args ...interface{}) {
29+
fmt.Printf(format, args...)
30+
}

0 commit comments

Comments
 (0)