From b8301f10a8977fd103ce8643a780814d55887606 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 12 Sep 2022 19:40:08 +0200 Subject: [PATCH] interp: add missing conversion for non integer array dimension Fixes #1451. --- _test/issue-1451.go | 19 +++++++++++++++++++ interp/type.go | 11 +++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 _test/issue-1451.go diff --git a/_test/issue-1451.go b/_test/issue-1451.go new file mode 100644 index 000000000..917403840 --- /dev/null +++ b/_test/issue-1451.go @@ -0,0 +1,19 @@ +package main + +type t1 uint8 + +const ( + n1 t1 = iota + n2 +) + +type T struct { + elem [n2 + 1]int +} + +func main() { + println(len(T{}.elem)) +} + +// Output: +// 2 diff --git a/interp/type.go b/interp/type.go index 9cc7191c9..91d6b25d4 100644 --- a/interp/type.go +++ b/interp/type.go @@ -446,12 +446,19 @@ func nodeType2(interp *Interpreter, sc *scope, n *node, seen []*node) (t *itype, ) switch v := c0.rval; { case v.IsValid(): - // Size if defined by a constant litteral value. + // Size if defined by a constant literal value. if isConstantValue(v.Type()) { c := v.Interface().(constant.Value) length = constToInt(c) } else { - length = int(v.Int()) + switch v.Type().Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + length = int(v.Int()) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + length = int(v.Uint()) + default: + return nil, c0.cfgErrorf("non integer constant %v", v) + } } case c0.kind == ellipsisExpr: // [...]T expression, get size from the length of composite array.