]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: suppress index-out-of-bounds error on Unknown constants
authorMatthew Dempsky <mdempsky@google.com>
Thu, 22 Apr 2021 19:42:33 +0000 (12:42 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 22 Apr 2021 22:01:47 +0000 (22:01 +0000)
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>
src/go/types/expr.go
src/go/types/testdata/expr3.src

index b4eea229b8e2886a3d578a1742ecbb5180ebc675..4023362a4ece6c783fc2ad2da1c1c00052c7a463 100644 (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
        }
 
index c3158e6cf412c23a733ec9e01590356a9ef9a1e5..0525a5a33ae09012954ff7aa7c8b0d1e89733356 100644 (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]