Skip to content

Commit

Permalink
interp: fix type check on function signature
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvertes authored Nov 13, 2020
1 parent 1378388 commit 38a7331
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
20 changes: 20 additions & 0 deletions _test/time14.go
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions interp/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit 38a7331

Please sign in to comment.