Skip to content

Commit

Permalink
interp: fix array declaration with a typed constant size
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvertes authored Jul 8, 2021
1 parent 25b570d commit f6d0cf9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
18 changes: 18 additions & 0 deletions _test/issue-1175.go
Original file line number Diff line number Diff line change
@@ -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
12 changes: 2 additions & 10 deletions interp/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f6d0cf9

Please sign in to comment.