forked from TrueCloudLab/neoneo-go
compiler: create new locals for range loops when needed, fix #2855
This commit is contained in:
parent
5ad1fcd321
commit
92acc71c80
2 changed files with 36 additions and 2 deletions
|
@ -1313,7 +1313,11 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
|
|||
} else {
|
||||
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
|
||||
}
|
||||
c.emitStoreVar("", n.Key.(*ast.Ident).Name)
|
||||
keyIdent := n.Key.(*ast.Ident)
|
||||
if n.Tok == token.DEFINE {
|
||||
c.scope.newLocal(keyIdent.Name)
|
||||
}
|
||||
c.emitStoreVar("", keyIdent.Name)
|
||||
}
|
||||
if needValue {
|
||||
if !isMap || !keyLoaded {
|
||||
|
@ -1327,7 +1331,11 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
|
|||
opcode.SWAP, // key should be on top
|
||||
opcode.PICKITEM)
|
||||
}
|
||||
c.emitStoreVar("", n.Value.(*ast.Ident).Name)
|
||||
valIdent := n.Value.(*ast.Ident)
|
||||
if n.Tok == token.DEFINE {
|
||||
c.scope.newLocal(valIdent.Name)
|
||||
}
|
||||
c.emitStoreVar("", valIdent.Name)
|
||||
}
|
||||
|
||||
ast.Walk(c, n.Body)
|
||||
|
|
|
@ -714,6 +714,32 @@ var forLoopTestCases = []testCase{
|
|||
`,
|
||||
big.NewInt(6),
|
||||
},
|
||||
{
|
||||
"shadow range key",
|
||||
`func F%d() int {
|
||||
i := 10
|
||||
ints := []int{1, 2, 3, 4, 5}
|
||||
for i := range ints {
|
||||
_ = i
|
||||
}
|
||||
return i
|
||||
}
|
||||
`,
|
||||
big.NewInt(10),
|
||||
},
|
||||
{
|
||||
"shadow range value",
|
||||
`func F%d() int {
|
||||
i := 10
|
||||
ints := []int{1, 2, 3, 4, 5}
|
||||
for _, i := range ints {
|
||||
_ = i
|
||||
}
|
||||
return i
|
||||
}
|
||||
`,
|
||||
big.NewInt(10),
|
||||
},
|
||||
}
|
||||
|
||||
func TestForLoop(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue