fix: ensure type computation in ValueSpec statement (#64)

Use nodeType in global var declaration to infer the type,
otherwise it may not be set properly, and failure will occur at use of variable.
This commit is contained in:
Marc Vertes
2019-01-29 13:39:34 +01:00
committed by Ludovic Fernandez
parent 2059d58b0e
commit 49c8c905d2
3 changed files with 71 additions and 1 deletions

28
_test/closure7.go Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -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