From e1ac83f7d89b2cc3fc0bf1c028b6373780e029ae Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Tue, 17 Dec 2019 10:18:06 +0100 Subject: [PATCH] fix: correct type extraction for returned value --- _test/nil0.go | 15 +++++++++++++++ interp/cfg.go | 25 ++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 _test/nil0.go diff --git a/_test/nil0.go b/_test/nil0.go new file mode 100644 index 000000000..f631a14f9 --- /dev/null +++ b/_test/nil0.go @@ -0,0 +1,15 @@ +package main + +import "fmt" + +func f() (host, port string, err error) { + return "", "", nil +} + +func main() { + h, p, err := f() + fmt.Println(h, p, err) +} + +// Output: +// diff --git a/interp/cfg.go b/interp/cfg.go index 801ff5b11..0446e6ad3 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -1068,7 +1068,7 @@ func (interp *Interpreter) cfg(root *node) ([]*node, error) { // nil: Set node value to zero of return type f := sc.def var typ *itype - if typ, err = nodeType(interp, sc, f.child[2].child[1].child[i].lastChild()); err != nil { + if typ, err = nodeType(interp, sc, f.child[2].child[1].fieldType(i)); err != nil { return } if typ.cat == funcT { @@ -1643,6 +1643,29 @@ func (n *node) isNatural() bool { return false } +// fieldType returns the nth parameter field node (type) of a fieldList node +func (n *node) fieldType(m int) *node { + k := 0 + l := len(n.child) + for i := 0; i < l; i++ { + cl := len(n.child[i].child) + if cl < 2 { + if k == m { + return n.child[i].lastChild() + } + k++ + continue + } + for j := 0; j < cl-1; j++ { + if k == m { + return n.child[i].lastChild() + } + k++ + } + } + return nil +} + // lastChild returns the last child of a node func (n *node) lastChild() *node { return n.child[len(n.child)-1] }