Skip to content

Commit

Permalink
interp: add missing conversion for non integer array dimension
Browse files Browse the repository at this point in the history
Fixes #1451.
  • Loading branch information
mvertes authored Sep 12, 2022
1 parent 2e88083 commit b8301f1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
19 changes: 19 additions & 0 deletions _test/issue-1451.go
Original file line number Diff line number Diff line change
@@ -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
11 changes: 9 additions & 2 deletions interp/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b8301f1

Please sign in to comment.