diff --git a/_test/issue-1640.go b/_test/issue-1640.go new file mode 100644 index 00000000..426dd864 --- /dev/null +++ b/_test/issue-1640.go @@ -0,0 +1,23 @@ +package main + +import ( + "errors" +) + +func ShortVariableDeclarations() (i int, err error) { + r, err := 1, errors.New("test") + i = r + return +} + +func main() { + _, er := ShortVariableDeclarations() + if er != nil { + println("ShortVariableDeclarations ok") + } else { + println("ShortVariableDeclarations not ok") + } +} + +// Output: +// ShortVariableDeclarations ok diff --git a/interp/cfg.go b/interp/cfg.go index f557fc55..6da734d1 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -681,7 +681,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string if dest.typ.incomplete { return } - if sc.global { + if sc.global || sc.isRedeclared(dest) { // Do not overload existing symbols (defined in GTA) in global scope. sym, _, _ = sc.lookup(dest.ident) } diff --git a/interp/run.go b/interp/run.go index 9ff81f95..ba98be75 100644 --- a/interp/run.go +++ b/interp/run.go @@ -1400,6 +1400,13 @@ func call(n *node) { } runCfg(def.child[3].start, nf, def, n) + // Set return values + for i, v := range rvalues { + if v != nil { + v(f).Set(nf.data[i]) + } + } + // Handle branching according to boolean result if fnext != nil && !nf.data[0].Bool() { return fnext diff --git a/interp/scope.go b/interp/scope.go index 52087aee..b3a9e109 100644 --- a/interp/scope.go +++ b/interp/scope.go @@ -145,6 +145,14 @@ func (s *scope) lookup(ident string) (*symbol, int, bool) { return nil, 0, false } +func (s *scope) isRedeclared(n *node) bool { + if !isNewDefine(n, s) { + return false + } + // Existing symbol in the scope indicates a redeclaration. + return s.sym[n.ident] != nil +} + func (s *scope) rangeChanType(n *node) *itype { if sym, _, found := s.lookup(n.child[1].ident); found { if t := sym.typ; len(n.child) == 3 && t != nil && (t.cat == chanT || t.cat == chanRecvT) {