interp: fix parsing of late binding consts

When a const is late binding and specified with a type, the GTA defineStmt was creating the symbol with the current scopes `iota` which is incorrect. The symbol should be created with the source nodes `rval`.

Related to #1158
This commit is contained in:
Nicholas Wiersma
2021-09-06 17:30:12 +02:00
committed by GitHub
parent c33caeb573
commit 45d569c215
2 changed files with 29 additions and 2 deletions

28
_test/const26.go Normal file
View File

@@ -0,0 +1,28 @@
package main
import (
"fmt"
)
func init() {
fmt.Println(constString)
fmt.Println(const2)
fmt.Println(varString)
}
const constString string = "hello"
const (
const1 = iota + 10
const2
const3
)
var varString string = "test"
func main() {}
// Output:
// hello
// 11
// test

View File

@@ -3,7 +3,6 @@ package interp
import (
"path"
"path/filepath"
"reflect"
)
// gta performs a global types analysis on the AST, registering types,
@@ -53,7 +52,7 @@ func (interp *Interpreter) gta(root *node, rpath, importPath, pkgName string) ([
for i := 0; i < n.nleft; i++ {
dest, src := n.child[i], n.child[sbase+i]
val := reflect.ValueOf(sc.iota)
val := src.rval
if n.anc.kind == constDecl {
if _, err2 := interp.cfg(n, importPath, pkgName); err2 != nil {
// Constant value can not be computed yet.