compiler: emit error for non-byte subslices

They are not supported for now, as VM has only
`SUBSTR` opcode for Buffers (`[]byte` in Go).
This commit is contained in:
Evgenii Stratonikov 2020-09-15 09:52:56 +03:00
parent 3f27cf5901
commit 78948ef7af
2 changed files with 15 additions and 0 deletions

View file

@ -517,6 +517,10 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
return nil return nil
case *ast.SliceExpr: 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 name := n.X.(*ast.Ident).Name
c.emitLoadVar("", name) c.emitLoadVar("", name)

View file

@ -316,6 +316,17 @@ func TestSliceOperations(t *testing.T) {
runTestCases(t, sliceTestCases) 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) { func TestJumps(t *testing.T) {
src := ` src := `
package foo package foo