From def73db8e906b09ce674801095eaf5b8dbf315f3 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 23 Jan 2020 15:42:50 +0300 Subject: [PATCH] compiler: support variables in slice literals --- pkg/compiler/codegen.go | 2 +- pkg/compiler/slice_test.go | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index a6651e9f2..063bfc922 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -365,7 +365,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { return nil } 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)) emitOpcode(c.prog.BinWriter, opcode.PACK) diff --git a/pkg/compiler/slice_test.go b/pkg/compiler/slice_test.go index 84948433d..5b113ccdb 100644 --- a/pkg/compiler/slice_test.go +++ b/pkg/compiler/slice_test.go @@ -45,9 +45,32 @@ var sliceTestCases = []testCase{ `, 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) { runTestCases(t, sliceTestCases) } -