1
0
mirror of https://github.com/golang/go synced 2024-11-26 08:27:56 -07:00

go/types: suppress index-out-of-bounds error on Unknown constants

Follow up to CL 312591, which was stumping rfindley and I for a
while. Credit to him for figuring out a repro and explaining the
correct solution.

Change-Id: Ib8578bba05f60fc41d382c34c5266d815441e7a1
Reviewed-on: https://go-review.googlesource.com/c/go/+/312790
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Trust: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Matthew Dempsky 2021-04-22 12:42:33 -07:00
parent f7afdfd483
commit 74059685fd
2 changed files with 9 additions and 3 deletions

View File

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

View File

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