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.
This commit is contained in:
Marc Vertes
2022-10-24 10:44:06 +02:00
committed by GitHub
parent c4d1bf5029
commit a5242cbb9e
2 changed files with 20 additions and 1 deletions

15
_test/issue-1470.go Normal file
View File

@@ -0,0 +1,15 @@
package main
type T struct {
num [tnum + 2]int
}
const tnum = 23
func main() {
t := T{}
println(len(t.num))
}
// Output:
// 25

View File

@@ -483,7 +483,11 @@ 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.
if _, err = interp.cfg(c0, sc, sc.pkgID, sc.pkgName); err != nil {
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)