From a87002e662bb57c206106e932b403099e6151899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 8 Dec 2019 22:44:00 +0000 Subject: [PATCH] don't break types implementing interfaces --- main.go | 4 ++++ testdata/scripts/implement.txt | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 testdata/scripts/implement.txt diff --git a/main.go b/main.go index 6fd4493..b83eb60 100644 --- a/main.go +++ b/main.go @@ -305,6 +305,10 @@ func transformGo(node ast.Node, info *types.Info) ast.Node { case *types.Const: case *types.TypeName: case *types.Func: + sign := obj.Type().(*types.Signature) + if obj.Exported() && sign.Recv() != nil { + return true // might implement an interface + } switch node.Name { case "main", "init": return true // don't break them diff --git a/testdata/scripts/implement.txt b/testdata/scripts/implement.txt new file mode 100644 index 0000000..c6c6992 --- /dev/null +++ b/testdata/scripts/implement.txt @@ -0,0 +1,28 @@ +garble build main.go +exec ./main +cmp stdout main.stdout + +! grep 'unexportedMethod' main + +-- main.go -- +package main + +import "fmt" + +type T string + +func (t T) String() string { + return "String method for " + string(t) +} + +func (t T) unexportedMethod() string { + return "unexported method for " + string(t) +} + +func main() { + fmt.Println(T("foo")) + fmt.Println(T("foo").unexportedMethod()) +} +-- main.stdout -- +String method for foo +unexported method for foo