diff --git a/_test/closure7.go b/_test/closure7.go new file mode 100644 index 00000000..9ec239b9 --- /dev/null +++ b/_test/closure7.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" +) + +type Config struct { + A string +} + +var conf *Config + +func SetConfig() func(*Config) { + return func(cf *Config) { + conf = cf + } +} + +func main() { + conf := &Config{ + A: "foo", + } + + fmt.Println(conf.A) +} + +// Output: +// foo diff --git a/interp/cfg.go b/interp/cfg.go index 5a332b1b..8b38fa3b 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -949,7 +949,10 @@ func (interp *Interpreter) Cfg(root *Node) ([]*Node, error) { case ValueSpec: l := len(n.child) - 1 if n.typ = n.child[l].typ; n.typ == nil { - n.typ = scope.getType(n.child[l].ident) + n.typ, err = nodeType(interp, scope, n.child[l]) + if err != nil { + return + } } for _, c := range n.child[:l] { c.typ = n.typ diff --git a/interp/interp_test.go b/interp/interp_test.go index 2d40067e..d454da19 100644 --- a/interp/interp_test.go +++ b/interp/interp_test.go @@ -1367,6 +1367,45 @@ func main() { // 9 } +func Example_closure7() { + src := ` +package main + +import ( + "fmt" +) + +type Config struct { + A string +} + +var conf *Config + +func SetConfig() func(*Config) { + return func(cf *Config) { + conf = cf + } +} + +func main() { + conf := &Config{ + A: "foo", + } + + fmt.Println(conf.A) +} +` + i := interp.New(interp.Opt{Entry: "main"}) + i.Use(stdlib.Value, stdlib.Type) + _, err := i.Eval(src) + if err != nil { + panic(err) + } + + // Output: + // foo +} + func Example_comp0() { src := ` package main