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:
committed by
Ludovic Fernandez
parent
2059d58b0e
commit
49c8c905d2
28
_test/closure7.go
Normal file
28
_test/closure7.go
Normal 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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user