mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-04 19:19:44 +00:00
compiler: support using return
in some branches
When `return` is used in one codepath, but not the other, we do not clean altstack properly. This commit fixes it.
This commit is contained in:
parent
2a6be8ceef
commit
b2a3cee451
3 changed files with 37 additions and 12 deletions
|
@ -133,16 +133,13 @@ func (f funcUsage) funcUsed(name string) bool {
|
|||
return ok
|
||||
}
|
||||
|
||||
// hasReturnStmt looks if the given FuncDecl has a return statement.
|
||||
func hasReturnStmt(decl ast.Node) (b bool) {
|
||||
ast.Inspect(decl, func(node ast.Node) bool {
|
||||
if _, ok := node.(*ast.ReturnStmt); ok {
|
||||
b = true
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return
|
||||
// lastStmtIsReturn checks if last statement of the declaration was return statement..
|
||||
func lastStmtIsReturn(decl *ast.FuncDecl) (b bool) {
|
||||
if l := len(decl.Body.List); l != 0 {
|
||||
_, ok := decl.Body.List[l-1].(*ast.ReturnStmt)
|
||||
return ok
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func analyzeFuncUsage(pkgs map[*types.Package]*loader.PackageInfo) funcUsage {
|
||||
|
|
|
@ -261,8 +261,10 @@ func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl) {
|
|||
|
||||
ast.Walk(c, decl.Body)
|
||||
|
||||
// If this function returns the void (no return stmt) we will cleanup its junk on the stack.
|
||||
if !hasReturnStmt(decl) {
|
||||
// If we have reached the end of the function without encountering `return` statement,
|
||||
// we should clean alt.stack manually.
|
||||
// This can be the case with void and named-return functions.
|
||||
if !lastStmtIsReturn(decl) {
|
||||
c.saveSequencePoint(decl.Body)
|
||||
emit.Opcode(c.prog.BinWriter, opcode.FROMALTSTACK)
|
||||
emit.Opcode(c.prog.BinWriter, opcode.DROP)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package compiler_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"testing"
|
||||
)
|
||||
|
@ -132,3 +133,28 @@ func TestFunctionWithVoidReturn(t *testing.T) {
|
|||
eval(t, src, big.NewInt(6))
|
||||
})
|
||||
}
|
||||
|
||||
func TestFunctionWithVoidReturnBranch(t *testing.T) {
|
||||
src := `
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := %t
|
||||
f(x)
|
||||
return 2
|
||||
}
|
||||
|
||||
func f(x bool) {
|
||||
if x {
|
||||
return
|
||||
}
|
||||
}
|
||||
`
|
||||
t.Run("ReturnBranch", func(t *testing.T) {
|
||||
src := fmt.Sprintf(src, true)
|
||||
eval(t, src, big.NewInt(2))
|
||||
})
|
||||
t.Run("NoReturn", func(t *testing.T) {
|
||||
src := fmt.Sprintf(src, false)
|
||||
eval(t, src, big.NewInt(2))
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue