diff --git a/src/go/types/expr.go b/src/go/types/expr.go index b4eea229b8e..4023362a4ec 100644 --- a/src/go/types/expr.go +++ b/src/go/types/expr.go @@ -1020,9 +1020,14 @@ func (check *Checker) index(index ast.Expr, max int64) (typ Type, val int64) { return x.typ, -1 } - v, valid := constant.Int64Val(constant.ToInt(x.val)) - if !valid || max >= 0 && v >= max { - check.errorf(&x, _InvalidIndex, "index %s is out of bounds", &x) + if x.val.Kind() == constant.Unknown { + return + } + + v, ok := constant.Int64Val(x.val) + assert(ok) + if max >= 0 && v >= max { + check.invalidArg(&x, _InvalidIndex, "index %s is out of bounds", &x) return } diff --git a/src/go/types/testdata/expr3.src b/src/go/types/testdata/expr3.src index c3158e6cf41..0525a5a33ae 100644 --- a/src/go/types/testdata/expr3.src +++ b/src/go/types/testdata/expr3.src @@ -34,6 +34,7 @@ func indexes() { _ = a[9] _ = a[10 /* ERROR "index .* out of bounds" */ ] _ = a[1 /* ERROR "overflows" */ <<100] + _ = a[1<< /* ERROR "constant shift overflow" */ 1000] // no out-of-bounds follow-on error _ = a[10:] _ = a[:10] _ = a[10:10]