fix a compiler bug with dead code optimization (#284)
This commit is contained in:
parent
5b449b37d4
commit
50d0ba75d5
1 changed files with 3 additions and 0 deletions
|
@ -1192,6 +1192,7 @@ func (c *Compiler) optimizeFunc(node parser.Node) {
|
||||||
var lastOp parser.Opcode
|
var lastOp parser.Opcode
|
||||||
var appendReturn bool
|
var appendReturn bool
|
||||||
endPos := len(c.scopes[c.scopeIndex].Instructions)
|
endPos := len(c.scopes[c.scopeIndex].Instructions)
|
||||||
|
newEndPost := len(newInsts)
|
||||||
iterateInstructions(newInsts,
|
iterateInstructions(newInsts,
|
||||||
func(pos int, opcode parser.Opcode, operands []int) bool {
|
func(pos int, opcode parser.Opcode, operands []int) bool {
|
||||||
switch opcode {
|
switch opcode {
|
||||||
|
@ -1204,6 +1205,8 @@ func (c *Compiler) optimizeFunc(node parser.Node) {
|
||||||
} else if endPos == operands[0] {
|
} else if endPos == operands[0] {
|
||||||
// there's a jump instruction that jumps to the end of
|
// there's a jump instruction that jumps to the end of
|
||||||
// function compiler should append "return".
|
// function compiler should append "return".
|
||||||
|
copy(newInsts[pos:],
|
||||||
|
MakeInstruction(opcode, newEndPost))
|
||||||
appendReturn = true
|
appendReturn = true
|
||||||
} else {
|
} else {
|
||||||
panic(fmt.Errorf("invalid jump position: %d", newDst))
|
panic(fmt.Errorf("invalid jump position: %d", newDst))
|
||||||
|
|
Loading…
Reference in a new issue