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.
This commit is contained in:
20
_test/time14.go
Normal file
20
_test/time14.go
Normal file
@@ -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
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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).
|
||||
|
||||
Reference in New Issue
Block a user