Skip to content

Commit

Permalink
fix: handle nil function closure
Browse files Browse the repository at this point in the history
  • Loading branch information
mvertes authored and traefiker committed Nov 25, 2019
1 parent e506969 commit 786ea36
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
19 changes: 19 additions & 0 deletions _test/fun10.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import "fmt"

func f() func() {
return nil
}

func main() {
g := f()
fmt.Printf("%T %v\n", g, g)
if g == nil {
fmt.Println("nil func")
}
}

// Output:
// func() <nil>
// nil func
7 changes: 6 additions & 1 deletion interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,12 @@ func (interp *Interpreter) cfg(root *node) ([]*node, error) {
if typ, err = nodeType(interp, sc, f.child[2].child[1].child[i].lastChild()); err != nil {
return
}
c.rval = reflect.New(typ.TypeOf()).Elem()
if typ.cat == funcT {
// Wrap the typed nil value in a node, as per other interpreter functions
c.rval = reflect.ValueOf(&node{kind: basicLit, rval: reflect.New(typ.TypeOf()).Elem()})
} else {
c.rval = reflect.New(typ.TypeOf()).Elem()
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions interp/interp_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ func TestEvalNil(t *testing.T) {
src: "Hello()",
res: "<nil>",
},
{
desc: "return nil func",
pre: func() {
eval(t, i, `func Bar() func() { return nil }`)
},
src: "Bar()",
res: "<nil>",
},
})
}

Expand Down
20 changes: 18 additions & 2 deletions interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ func _panic(n *node) {
func genFunctionWrapper(n *node) func(*frame) reflect.Value {
var def *node
var ok bool

if n.kind == basicLit {
return func(f *frame) reflect.Value { return n.rval }
}
if def, ok = n.val.(*node); !ok {
return genValueAsFunctionWrapper(n)
}
Expand Down Expand Up @@ -1350,6 +1354,8 @@ func _return(n *node) {
} else {
values[i] = genValue(c)
}
case funcT:
values[i] = genValue(c)
case interfaceT:
values[i] = genValueInterface(c)
default:
Expand Down Expand Up @@ -2407,7 +2413,12 @@ func slice0(n *node) {
}

func isNil(n *node) {
value := genValue(n.child[0])
var value func(*frame) reflect.Value
if n.child[0].typ.cat == funcT {
value = genValueAsFunctionWrapper(n.child[0])
} else {
value = genValue(n.child[0])
}
tnext := getExec(n.tnext)

if n.fnext != nil {
Expand All @@ -2428,7 +2439,12 @@ func isNil(n *node) {
}

func isNotNil(n *node) {
value := genValue(n.child[0])
var value func(*frame) reflect.Value
if n.child[0].typ.cat == funcT {
value = genValueAsFunctionWrapper(n.child[0])
} else {
value = genValue(n.child[0])
}
tnext := getExec(n.tnext)

if n.fnext != nil {
Expand Down
10 changes: 8 additions & 2 deletions interp/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ func genValueRecv(n *node) func(*frame) reflect.Value {
}

func genValueAsFunctionWrapper(n *node) func(*frame) reflect.Value {
v := genValue(n)
value := genValue(n)
typ := n.typ.TypeOf()

return func(f *frame) reflect.Value {
return genFunctionWrapper(v(f).Interface().(*node))(f)
v := value(f)
if v.IsNil() {
return reflect.New(typ).Elem()
}
return genFunctionWrapper(v.Interface().(*node))(f)
}
}

Expand Down

0 comments on commit 786ea36

Please sign in to comment.