Merge pull request #2280 from nspcc-dev/compiler-struct-cast
compiler: allow to slice struct field
This commit is contained in:
commit
dbe184a335
2 changed files with 13 additions and 3 deletions
|
@ -644,12 +644,12 @@ 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()) {
|
if isCompoundSlice(c.typeOf(n.X).Underlying()) {
|
||||||
c.prog.Err = errors.New("subslices are supported only for []byte")
|
c.prog.Err = errors.New("subslices are supported only for []byte")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
name := n.X.(*ast.Ident).Name
|
|
||||||
c.emitLoadVar("", name)
|
ast.Walk(c, n.X)
|
||||||
|
|
||||||
if n.Low != nil {
|
if n.Low != nil {
|
||||||
ast.Walk(c, n.Low)
|
ast.Walk(c, n.Low)
|
||||||
|
|
|
@ -432,6 +432,16 @@ func TestSubsliceCompound(t *testing.T) {
|
||||||
require.Error(t, err)
|
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) {
|
func TestRemove(t *testing.T) {
|
||||||
t.Run("Valid", func(t *testing.T) {
|
t.Run("Valid", func(t *testing.T) {
|
||||||
src := `package foo
|
src := `package foo
|
||||||
|
|
Loading…
Reference in a new issue