interp: add missing conversion for non integer array dimension

Fixes #1451.
This commit is contained in:
Marc Vertes
2022-09-12 19:40:08 +02:00
committed by GitHub
parent 2e8808317f
commit b8301f10a8
2 changed files with 28 additions and 2 deletions

19
_test/issue-1451.go Normal file
View File

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

View File

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