Skip to content

Commit

Permalink
interp: fix computation of array size from constant expression
Browse files Browse the repository at this point in the history
The result of the expression giving the size of an array may be an `int` instead of `constant.Value` in case of an out of order declaration. Handle both cases.

Fixes #1536.
  • Loading branch information
mvertes authored Apr 11, 2023
1 parent 8de3add commit d124954
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
15 changes: 15 additions & 0 deletions _test/issue-1536.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

var a [len(prefix+path) + 2]int

const (
prefix = "/usr/"
path = prefix + "local/bin"
)

func main() {
println(len(a))
}

// Output:
// 21
13 changes: 8 additions & 5 deletions interp/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,19 +484,22 @@ func nodeType2(interp *Interpreter, sc *scope, n *node, seen []*node) (t *itype,
length = int(vInt(sym.rval))
default:
// Size is defined by a numeric constant expression.
ok := false
if _, err := interp.cfg(c0, sc, sc.pkgID, sc.pkgName); err != nil {
if strings.Contains(err.Error(), " undefined: ") {
incomplete = true
break
}
return nil, err
}
v, ok := c0.rval.Interface().(constant.Value)
if !ok {
incomplete = true
break
if length, ok = c0.rval.Interface().(int); !ok {
v, ok := c0.rval.Interface().(constant.Value)
if !ok {
incomplete = true
break
}
length = constToInt(v)
}
length = constToInt(v)
}
val, err := nodeType2(interp, sc, n.child[1], seen)
if err != nil {
Expand Down

0 comments on commit d124954

Please sign in to comment.