diff --git a/_test/a44.go b/_test/a44.go new file mode 100644 index 00000000..9c978e36 --- /dev/null +++ b/_test/a44.go @@ -0,0 +1,12 @@ +package main + +var a = [max]int{} + +const max = 32 + +func main() { + println(len(a)) +} + +// Output: +// 32 diff --git a/interp/type.go b/interp/type.go index b58c8532..bae61624 100644 --- a/interp/type.go +++ b/interp/type.go @@ -185,30 +185,33 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) { } else { t.size = int(v.Int()) } - case n.child[0].kind == ellipsisExpr: + case c0.kind == ellipsisExpr: // [...]T expression, get size from the length of composite array. t.size = arrayTypeLen(n.anc) - default: - if sym, _, ok := sc.lookup(c0.ident); ok { - // Size is defined by a symbol which must be a constant integer. - if sym.kind != constSym { - return nil, c0.cfgErrorf("non-constant array bound %q", c0.ident) - } - if sym.typ == nil || sym.typ.cat != intT { - t.incomplete = true - break - } - if v, ok := sym.rval.Interface().(int); ok { - t.size = v - break - } - if c, ok := sym.rval.Interface().(constant.Value); ok { - t.size = constToInt(c) - break - } + case c0.kind == identExpr: + sym, _, ok := sc.lookup(c0.ident) + if !ok { t.incomplete = true break } + // Size is defined by a symbol which must be a constant integer. + if sym.kind != constSym { + return nil, c0.cfgErrorf("non-constant array bound %q", c0.ident) + } + if sym.typ == nil || sym.typ.cat != intT { + t.incomplete = true + break + } + if v, ok := sym.rval.Interface().(int); ok { + t.size = v + break + } + if c, ok := sym.rval.Interface().(constant.Value); ok { + t.size = constToInt(c) + break + } + t.incomplete = true + default: // Size is defined by a numeric constant expression. if _, err = interp.cfg(c0, sc.pkgID); err != nil { return nil, err