From a5242cbb9e29d625f577e7d55b62674bbc72a424 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 24 Oct 2022 10:44:06 +0200 Subject: [PATCH] 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. --- _test/issue-1470.go | 15 +++++++++++++++ interp/type.go | 6 +++++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 _test/issue-1470.go diff --git a/_test/issue-1470.go b/_test/issue-1470.go new file mode 100644 index 000000000..4852550cf --- /dev/null +++ b/_test/issue-1470.go @@ -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 diff --git a/interp/type.go b/interp/type.go index ded5c875e..e9b3d4515 100644 --- a/interp/type.go +++ b/interp/type.go @@ -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)