From 78948ef7af8fd7246d2c9c8536499787d0620a63 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 15 Sep 2020 09:52:56 +0300 Subject: [PATCH] compiler: emit error for non-byte subslices They are not supported for now, as VM has only `SUBSTR` opcode for Buffers (`[]byte` in Go). --- pkg/compiler/codegen.go | 4 ++++ pkg/compiler/slice_test.go | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index be9e8ec2c..7aa889e0a 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -517,6 +517,10 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { return nil case *ast.SliceExpr: + if isCompoundSlice(c.typeOf(n.X.(*ast.Ident)).Underlying()) { + c.prog.Err = errors.New("subslices are supported only for []byte") + return nil + } name := n.X.(*ast.Ident).Name c.emitLoadVar("", name) diff --git a/pkg/compiler/slice_test.go b/pkg/compiler/slice_test.go index ee776407f..7d14650d5 100644 --- a/pkg/compiler/slice_test.go +++ b/pkg/compiler/slice_test.go @@ -316,6 +316,17 @@ func TestSliceOperations(t *testing.T) { runTestCases(t, sliceTestCases) } +func TestSubsliceCompound(t *testing.T) { + src := `package foo + func Main() []int { + a := []int{0, 1, 2, 3} + b := a[1:3] + return b + }` + _, err := compiler.Compile("", strings.NewReader(src)) + require.Error(t, err) +} + func TestJumps(t *testing.T) { src := ` package foo