compiler: support nested slice element assignment

This commit is contained in:
Evgenii Stratonikov 2020-05-20 17:53:01 +03:00
parent 5b0e73ddf0
commit 8cf7871c26
2 changed files with 11 additions and 2 deletions

View file

@ -432,8 +432,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
if !isAssignOp {
ast.Walk(c, n.Rhs[i])
}
name := t.X.(*ast.Ident).Name
c.emitLoadVar(name)
ast.Walk(c, t.X)
ast.Walk(c, t.Index)
emit.Opcode(c.prog.BinWriter, opcode.ROT)
emit.Opcode(c.prog.BinWriter, opcode.SETITEM)

View file

@ -212,6 +212,16 @@ var sliceTestCases = []testCase{
}`,
[]byte{1, 2},
},
{
"nested slice assignment",
`package foo
func Main() int {
a := [][]int{[]int{1, 2}, []int{3, 4}}
a[1][0] = 42
return a[1][0]
}`,
big.NewInt(42),
},
}
func TestSliceOperations(t *testing.T) {