Skip to content

Commit 38a7331

Browse files
authored
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.
1 parent 1378388 commit 38a7331

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

_test/time14.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
var t time.Time
9+
10+
func f() time.Time {
11+
time := t
12+
return time
13+
}
14+
15+
func main() {
16+
fmt.Println(f())
17+
}
18+
19+
// Output:
20+
// 0001-01-01 00:00:00 +0000 UTC

interp/cfg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
13811381
n.val = sc.def
13821382
for i, c := range n.child {
13831383
var typ *itype
1384-
typ, err = nodeType(interp, sc, returnSig.child[1].fieldType(i))
1384+
typ, err = nodeType(interp, sc.upperLevel(), returnSig.child[1].fieldType(i))
13851385
if err != nil {
13861386
return
13871387
}

interp/scope.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ func (s *scope) pop() *scope {
113113
return s.anc
114114
}
115115

116+
func (s *scope) upperLevel() *scope {
117+
level := s.level
118+
for s != nil && s.level == level {
119+
s = s.anc
120+
}
121+
return s
122+
}
123+
116124
// lookup searches for a symbol in the current scope, and upper ones if not found
117125
// it returns the symbol, the number of indirections level from the current scope
118126
// and status (false if no result).

0 commit comments

Comments
 (0)