From 38a7331bf9d7c53eea91ec8a1c99d266227d258a Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 13 Nov 2020 18:02:04 +0100 Subject: [PATCH] interp: fix type check on function signature Perform function declaration type check from the upper level scope (the scope where the function is declared), to avoid possible collisions of local variables with package names. Fixes #957. --- _test/time14.go | 20 ++++++++++++++++++++ interp/cfg.go | 2 +- interp/scope.go | 8 ++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 _test/time14.go diff --git a/_test/time14.go b/_test/time14.go new file mode 100644 index 000000000..9f28c57d0 --- /dev/null +++ b/_test/time14.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "time" +) + +var t time.Time + +func f() time.Time { + time := t + return time +} + +func main() { + fmt.Println(f()) +} + +// Output: +// 0001-01-01 00:00:00 +0000 UTC diff --git a/interp/cfg.go b/interp/cfg.go index b8dbd29ab..714f6f67b 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -1381,7 +1381,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) { n.val = sc.def for i, c := range n.child { var typ *itype - typ, err = nodeType(interp, sc, returnSig.child[1].fieldType(i)) + typ, err = nodeType(interp, sc.upperLevel(), returnSig.child[1].fieldType(i)) if err != nil { return } diff --git a/interp/scope.go b/interp/scope.go index 4a8e728f8..7dcbe60ad 100644 --- a/interp/scope.go +++ b/interp/scope.go @@ -113,6 +113,14 @@ func (s *scope) pop() *scope { return s.anc } +func (s *scope) upperLevel() *scope { + level := s.level + for s != nil && s.level == level { + s = s.anc + } + return s +} + // lookup searches for a symbol in the current scope, and upper ones if not found // it returns the symbol, the number of indirections level from the current scope // and status (false if no result).