From f6d0cf95fd46d8f9aaadd472a7c7c6b6af340533 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Thu, 8 Jul 2021 12:30:12 +0200 Subject: [PATCH] interp: fix array declaration with a typed constant size In parsing array type declaration, The type check of array size was restricted to `int`. Broaden the test to accept any valid integer kind. Fixes #1175. --- _test/issue-1175.go | 18 ++++++++++++++++++ interp/type.go | 12 ++---------- 2 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 _test/issue-1175.go diff --git a/_test/issue-1175.go b/_test/issue-1175.go new file mode 100644 index 000000000..d6d151441 --- /dev/null +++ b/_test/issue-1175.go @@ -0,0 +1,18 @@ +package main + +type Level int8 + +const ( + a Level = -1 + b Level = 5 + d = b - a + 1 +) + +type counters [d]int + +func main() { + println(len(counters{})) +} + +// Output: +// 7 diff --git a/interp/type.go b/interp/type.go index 797dc2535..9c7970f37 100644 --- a/interp/type.go +++ b/interp/type.go @@ -201,19 +201,11 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) { if sym.kind != constSym { return nil, c0.cfgErrorf("non-constant array bound %q", c0.ident) } - if sym.typ == nil || sym.typ.cat != intT || !sym.rval.IsValid() { + if sym.typ == nil || !isInt(sym.typ.TypeOf()) || !sym.rval.IsValid() { t.incomplete = true break } - if v, ok := sym.rval.Interface().(int); ok { - t.length = v - break - } - if c, ok := sym.rval.Interface().(constant.Value); ok { - t.length = constToInt(c) - break - } - t.incomplete = true + t.length = int(vInt(sym.rval)) default: // Size is defined by a numeric constant expression. if _, err = interp.cfg(c0, sc.pkgID); err != nil {