Skip to content

Commit

Permalink
interp: fix parsing of late binding consts
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nrwiersma committed Sep 6, 2021
1 parent c33caeb commit 45d569c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
28 changes: 28 additions & 0 deletions _test/const26.go
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions interp/gta.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package interp
import (
"path"
"path/filepath"
"reflect"
)

// gta performs a global types analysis on the AST, registering types,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 45d569c

Please sign in to comment.