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:
Evgenii Stratonikov 2020-05-06 15:39:25 +03:00
parent 770cff8b91
commit 156a2eddc5
3 changed files with 37 additions and 12 deletions

View file

@ -263,8 +263,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)