Skip to content

Commit

Permalink
interp: fix spurious variable declaration loop
Browse files Browse the repository at this point in the history
The variable dependency check function was confused by a dependency
variable with the same name but in an external package.

This change is necessary to address #1427.
  • Loading branch information
mvertes committed Aug 10, 2022
1 parent ae725fb commit b2aa636
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
3 changes: 3 additions & 0 deletions _test/p4/p4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package p4

var Value1 = "value1"
10 changes: 10 additions & 0 deletions _test/p5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import "github.com/traefik/yaegi/_test/p5"

func main() {
println(*p5.Value1)
}

// Output:
// value1
8 changes: 8 additions & 0 deletions _test/p5/p5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package p5

import "github.com/traefik/yaegi/_test/p4"

var (
Value1 = &val1
val1 = p4.Value1
)
23 changes: 15 additions & 8 deletions interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2420,15 +2420,22 @@ func genGlobalVarDecl(nodes []*node, sc *scope) (*node, error) {

func getVarDependencies(nod *node, sc *scope) (deps []*node) {
nod.Walk(func(n *node) bool {
if n.kind == identExpr {
if sym, _, ok := sc.lookup(n.ident); ok {
if sym.kind != varSym || !sym.global || sym.node == nod {
return false
}
deps = append(deps, sym.node)
}
if n.kind != identExpr {
return true
}
return true
// Process ident nodes, and avoid false dependencies.
if n.anc.kind == selectorExpr && childPos(n) == 1 {
return false
}
sym, _, ok := sc.lookup(n.ident)
if !ok {
return false
}
if sym.kind != varSym || !sym.global || sym.node == nod {
return false
}
deps = append(deps, sym.node)
return false
}, nil)
return deps
}
Expand Down

0 comments on commit b2aa636

Please sign in to comment.