fix: compute type of slice expression globally

This commit is contained in:
Marc Vertes
2019-10-05 19:14:04 +02:00
committed by Traefiker Bot
parent 1cf327bd7d
commit 7d19108f01
3 changed files with 25 additions and 12 deletions

11
_test/var9.go Normal file
View File

@@ -0,0 +1,11 @@
package main
var a = "sdofjsdfj"
var z = a[0:2]
func main() {
println(z)
}
// Output:
// sd

View File

@@ -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)

View File

@@ -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