Merge pull request #619 from nspcc-dev/feature/arraylit

compiler: support variables in slice literals
This commit is contained in:
Roman Khimov 2020-01-23 16:11:21 +03:00 committed by GitHub
commit 2d2a6463e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View file

@ -365,7 +365,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
return nil return nil
} }
for i := ln - 1; i >= 0; i-- { for i := ln - 1; i >= 0; i-- {
c.emitLoadConst(c.typeInfo.Types[n.Elts[i]]) ast.Walk(c, n.Elts[i])
} }
emitInt(c.prog.BinWriter, int64(ln)) emitInt(c.prog.BinWriter, int64(ln))
emitOpcode(c.prog.BinWriter, opcode.PACK) emitOpcode(c.prog.BinWriter, opcode.PACK)

View file

@ -45,9 +45,32 @@ var sliceTestCases = []testCase{
`, `,
big.NewInt(9), big.NewInt(9),
}, },
{
"slice literals with variables",
`
package foo
func Main() int {
elem := 7
a := []int{6, elem, 8}
return a[1]
}
`,
big.NewInt(7),
},
{
"slice literals with expressions",
`
package foo
func Main() int {
elem := []int{3, 7}
a := []int{6, elem[1]*2+1, 24}
return a[1]
}
`,
big.NewInt(15),
},
} }
func TestSliceOperations(t *testing.T) { func TestSliceOperations(t *testing.T) {
runTestCases(t, sliceTestCases) runTestCases(t, sliceTestCases)
} }