compiler: drop stack after inline

Some control-flow statements drop stack items, for example
`return` when it is used inside of `range` loop.
For inlined calls this `return` should drop only portion of stack
which belongs to inlined call.
This commit is contained in:
Evgeniy Stratonikov 2021-02-26 17:58:08 +03:00
parent 347212c0c5
commit b66b853285
4 changed files with 74 additions and 1 deletions

View file

@ -115,6 +115,56 @@ func TestInline(t *testing.T) {
})
}
func TestInlineInLoop(t *testing.T) {
t.Run("simple", func(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/interop/binary"
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/inline"
func Main() int {
sum := 0
values := []int{10, 11}
for _, v := range values {
binary.Itoa(v, 10)
sum += inline.VarSum(1, 2, 3, 4)
}
return sum
}`
eval(t, src, big.NewInt(20))
})
t.Run("check clean stack on return", func(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/interop/binary"
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/inline"
func Main() int {
values := []int{10, 11, 12}
for _, v := range values {
binary.Itoa(v, 10)
if v == 11 {
return inline.VarSum(2, 20, 200)
}
}
return 0
}`
eval(t, src, big.NewInt(222))
})
}
func TestInlineInSwitch(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/inline"
func Main() int {
switch inline.VarSum(1, 2) {
case inline.VarSum(3, 1):
return 10
case inline.VarSum(4, -1):
return 11
default:
return 12
}
}`
eval(t, src, big.NewInt(11))
}
func TestInlineGlobalVariable(t *testing.T) {
t.Run("simple", func(t *testing.T) {
src := `package foo