diff --git a/_test/type15.go b/_test/type15.go new file mode 100644 index 00000000..b39c8cfd --- /dev/null +++ b/_test/type15.go @@ -0,0 +1,9 @@ +package main + +func main() { + err := error(nil) + println(err == nil) +} + +// Output: +// true diff --git a/interp/cfg.go b/interp/cfg.go index 8d1523b4..fc6889cb 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -743,7 +743,7 @@ func (interp *Interpreter) cfg(root *node, pkgID string) ([]*node, error) { if isInt(n.child[0].typ.TypeOf()) && n.child[1].kind == basicLit && isFloat(n.child[1].typ.TypeOf()) { err = n.cfgErrorf("truncated to integer") } - if isInterface(n.child[0].typ) { + if isInterface(n.child[0].typ) && !n.child[1].isNil() { // Convert to interface: just check that all required methods are defined by concrete type. c0, c1 := n.child[0], n.child[1] if !c1.typ.implements(c0.typ) { @@ -1695,6 +1695,9 @@ func (n *node) isNatural() bool { return false } +// isNil returns true if node is a literal nil value, false otherwise +func (n *node) isNil() bool { return n.kind == basicLit && !n.rval.IsValid() } + // fieldType returns the nth parameter field node (type) of a fieldList node func (n *node) fieldType(m int) *node { k := 0 diff --git a/interp/run.go b/interp/run.go index 204cc9aa..7c21285a 100644 --- a/interp/run.go +++ b/interp/run.go @@ -216,7 +216,7 @@ func convert(n *node) { typ := n.child[0].typ.TypeOf() next := getExec(n.tnext) - if c.kind == basicLit && !c.rval.IsValid() { // convert nil to type + if c.isNil() { // convert nil to type n.exec = func(f *frame) bltn { dest(f).Set(reflect.New(typ).Elem()) return next