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.
This commit is contained in:
Marc Vertes
2021-07-08 12:30:12 +02:00
committed by GitHub
parent 25b570d7e9
commit f6d0cf95fd
2 changed files with 20 additions and 10 deletions

18
_test/issue-1175.go Normal file
View File

@@ -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

View File

@@ -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 {