fix a compiler bug with dead code optimization (#284)

This commit is contained in:
daniel 2020-05-19 07:00:53 -07:00 committed by GitHub
parent 5b449b37d4
commit 50d0ba75d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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))