From 66a4245befc93cac44e9c84a676691f706cdc3c7 Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Tue, 30 Nov 2021 12:55:51 +0300 Subject: [PATCH] compiler: allow to slice struct field It makes no sense to restrict to identifiers. --- pkg/compiler/codegen.go | 6 +++--- pkg/compiler/slice_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index ab66620ed..d6fc6707b 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -641,12 +641,12 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { return nil case *ast.SliceExpr: - if isCompoundSlice(c.typeOf(n.X.(*ast.Ident)).Underlying()) { + if isCompoundSlice(c.typeOf(n.X).Underlying()) { c.prog.Err = errors.New("subslices are supported only for []byte") return nil } - name := n.X.(*ast.Ident).Name - c.emitLoadVar("", name) + + ast.Walk(c, n.X) if n.Low != nil { ast.Walk(c, n.Low) diff --git a/pkg/compiler/slice_test.go b/pkg/compiler/slice_test.go index 82cb9c72c..4ce08291d 100644 --- a/pkg/compiler/slice_test.go +++ b/pkg/compiler/slice_test.go @@ -432,6 +432,16 @@ func TestSubsliceCompound(t *testing.T) { require.Error(t, err) } +func TestSubsliceFromStructField(t *testing.T) { + src := `package foo + type pair struct { key, value []byte } + func Main() []byte { + p := pair{ []byte{1}, []byte{4, 8, 15, 16, 23, 42} } + return p.value[2:4] + }` + eval(t, src, []byte{15, 16}) +} + func TestRemove(t *testing.T) { t.Run("Valid", func(t *testing.T) { src := `package foo