From 50d0ba75d5a47b5901838c80b87e3ce1fa61c493 Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 19 May 2020 07:00:53 -0700 Subject: [PATCH] fix a compiler bug with dead code optimization (#284) --- compiler.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler.go b/compiler.go index eb686ed6..6ecc9869 100644 --- a/compiler.go +++ b/compiler.go @@ -1192,6 +1192,7 @@ func (c *Compiler) optimizeFunc(node parser.Node) { var lastOp parser.Opcode var appendReturn bool endPos := len(c.scopes[c.scopeIndex].Instructions) + newEndPost := len(newInsts) iterateInstructions(newInsts, func(pos int, opcode parser.Opcode, operands []int) bool { switch opcode { @@ -1204,6 +1205,8 @@ func (c *Compiler) optimizeFunc(node parser.Node) { } else if endPos == operands[0] { // there's a jump instruction that jumps to the end of // function compiler should append "return". + copy(newInsts[pos:], + MakeInstruction(opcode, newEndPost)) appendReturn = true } else { panic(fmt.Errorf("invalid jump position: %d", newDst))