fix: correct convert nil to type pointer (#166)

This commit is contained in:
Marc Vertes
2019-04-23 11:04:47 +02:00
committed by Ludovic Fernandez
parent 37f93f0392
commit e8dfded66b
3 changed files with 32 additions and 8 deletions

14
_test/type8.go Normal file
View File

@@ -0,0 +1,14 @@
package main
import (
"fmt"
"time"
)
func main() {
v := (*time.Time)(nil)
fmt.Println(v)
}
// Output:
// <nil>

View File

@@ -1075,10 +1075,10 @@ func (interp *Interpreter) Cfg(root *Node) ([]*Node, error) {
case n.anc.kind == Field:
// pointer type expression in a field expression (arg or struct field)
n.gen = nop
case n.anc.kind == ParenExpr && n.child[0].sym != nil && n.child[0].sym.kind == Typ:
case n.child[0].isType(scope):
// pointer type expression
n.gen = nop
n.typ = &Type{cat: PtrT, val: n.child[0].sym.typ}
n.typ = &Type{cat: PtrT, val: n.child[0].typ}
default:
// dereference expression
wireChild(n)

View File

@@ -171,15 +171,25 @@ func typeAssert2(n *Node) {
func convert(n *Node) {
i := n.findex
var value func(*Frame) reflect.Value
if n.child[1].typ.cat == FuncT {
value = genNodeWrapper(n.child[1])
} else {
value = genValue(n.child[1])
}
c := n.child[1]
typ := n.child[0].typ.TypeOf()
next := getExec(n.tnext)
if c.kind == BasicLit && c.val == nil { // convert nil to type
n.exec = func(f *Frame) Builtin {
f.data[i] = reflect.New(typ).Elem()
return next
}
return
}
var value func(*Frame) reflect.Value
if c.typ.cat == FuncT {
value = genNodeWrapper(c)
} else {
value = genValue(c)
}
n.exec = func(f *Frame) Builtin {
f.data[i] = value(f).Convert(typ)
return next