Skip to content

Commit a5242cb

Browse files
authored
interp: retry type definition if an array size is undefined
In case of forward definition of a constant, a symbol may be undefined when attempting to compute the array size in type analysis. Just mark the type as incomplete instead of aborting directly, to allow resolution at a second pass. Fixes #1470.
1 parent c4d1bf5 commit a5242cb

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

_test/issue-1470.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
type T struct {
4+
num [tnum + 2]int
5+
}
6+
7+
const tnum = 23
8+
9+
func main() {
10+
t := T{}
11+
println(len(t.num))
12+
}
13+
14+
// Output:
15+
// 25

interp/type.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,11 @@ func nodeType2(interp *Interpreter, sc *scope, n *node, seen []*node) (t *itype,
483483
length = int(vInt(sym.rval))
484484
default:
485485
// Size is defined by a numeric constant expression.
486-
if _, err = interp.cfg(c0, sc, sc.pkgID, sc.pkgName); err != nil {
486+
if _, err := interp.cfg(c0, sc, sc.pkgID, sc.pkgName); err != nil {
487+
if strings.Contains(err.Error(), " undefined: ") {
488+
incomplete = true
489+
break
490+
}
487491
return nil, err
488492
}
489493
v, ok := c0.rval.Interface().(constant.Value)

0 commit comments

Comments
 (0)