compiler: do not emit code for unused imported functions

Our current algorithm marks function as used if it is called
at least ones, even if the callee function is itself unused.
This commit implements more clever traversal to collect usage
information more precisely.

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2021-09-30 11:48:18 +03:00
parent a340e11b0b
commit 0bc81aecf4
6 changed files with 174 additions and 10 deletions

View file

@ -324,3 +324,30 @@ func TestFunctionUnusedParameters(t *testing.T) {
}`
eval(t, src, big.NewInt(101))
}
func TestUnusedFunctions(t *testing.T) {
t.Run("only variable", func(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/nestedcall"
func Main() int {
return nestedcall.X
}`
b, err := compiler.Compile("foo", strings.NewReader(src))
require.NoError(t, err)
require.Equal(t, 3, len(b)) // PUSHINT8 (42) + RET
eval(t, src, big.NewInt(42))
})
t.Run("imported function", func(t *testing.T) {
// Check that import map is set correctly during package traversal.
src := `package foo
import inner "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/nestedcall"
func Main() int {
return inner.N()
}`
_, err := compiler.Compile("foo", strings.NewReader(src))
require.NoError(t, err)
eval(t, src, big.NewInt(65))
})
}