diff --git a/_test/var9.go b/_test/var9.go new file mode 100644 index 00000000..5f27f816 --- /dev/null +++ b/_test/var9.go @@ -0,0 +1,11 @@ +package main + +var a = "sdofjsdfj" +var z = a[0:2] + +func main() { + println(z) +} + +// Output: +// sd diff --git a/interp/cfg.go b/interp/cfg.go index c1410f8e..565090e5 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -1309,18 +1309,8 @@ func (interp *Interpreter) cfg(root *node) ([]*node, error) { case sliceExpr: wireChild(n) - ctyp := n.child[0].typ - if ctyp.cat == ptrT { - ctyp = ctyp.val - } - if ctyp.size != 0 { - // Create a slice type from an array type - n.typ = &itype{} - *n.typ = *ctyp - n.typ.size = 0 - n.typ.rtype = nil - } else { - n.typ = ctyp + if n.typ, err = nodeType(interp, sc, n); err != nil { + return } n.findex = sc.add(n.typ) diff --git a/interp/type.go b/interp/type.go index 1ec9ead1..9b1940ce 100644 --- a/interp/type.go +++ b/interp/type.go @@ -486,6 +486,18 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) { } } + case sliceExpr: + t, err = nodeType(interp, sc, n.child[0]) + if t.cat == ptrT { + t = t.val + } + if err == nil && t.size != 0 { + t1 := *t + t1.size = 0 + t1.rtype = nil + t = &t1 + } + case structType: t.cat = structT var incomplete bool