diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 896349972..4c98a30f8 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -1594,13 +1594,24 @@ func (c *codegen) emitConvert(typ stackitem.Type) { func (c *codegen) convertByteArray(lit *ast.CompositeLit) { buf := make([]byte, len(lit.Elts)) + varIndices := []int{} for i := 0; i < len(lit.Elts); i++ { t := c.typeAndValueOf(lit.Elts[i]) - val, _ := constant.Int64Val(t.Value) - buf[i] = byte(val) + if t.Value != nil { + val, _ := constant.Int64Val(t.Value) + buf[i] = byte(val) + } else { + varIndices = append(varIndices, i) + } } emit.Bytes(c.prog.BinWriter, buf) c.emitConvert(stackitem.BufferT) + for _, i := range varIndices { + emit.Opcode(c.prog.BinWriter, opcode.DUP) + emit.Int(c.prog.BinWriter, int64(i)) + ast.Walk(c, lit.Elts[i]) + emit.Opcode(c.prog.BinWriter, opcode.SETITEM) + } } func (c *codegen) convertMap(lit *ast.CompositeLit) { diff --git a/pkg/compiler/slice_test.go b/pkg/compiler/slice_test.go index e4bc8c6e9..858666b70 100644 --- a/pkg/compiler/slice_test.go +++ b/pkg/compiler/slice_test.go @@ -310,6 +310,17 @@ var sliceTestCases = []testCase{ `, big.NewInt(2), }, + { + "literal byte-slice with variable values", + `package foo + const sym1 = 's' + func Main() []byte { + sym2 := byte('t') + sym4 := byte('i') + return []byte{sym1, sym2, 'r', sym4, 'n', 'g'} + }`, + []byte("string"), + }, } func TestSliceOperations(t *testing.T) {