1
0
mirror of https://github.com/golang/go synced 2024-11-17 17:54:48 -07:00

cmd/compile/internal/types2: better position for invalid slice indices error

Report the error at the first place (which is to say, latest index)
causing the error.

Change-Id: I31cf0a4d243fc66cfab84b7fec98055f4eb60ddf
Reviewed-on: https://go-review.googlesource.com/c/go/+/363671
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2021-11-13 13:57:48 -08:00
parent 42fa03a88c
commit fda9261504
2 changed files with 15 additions and 12 deletions

View File

@ -309,9 +309,12 @@ func (check *Checker) sliceExpr(x *operand, e *syntax.SliceExpr) {
L:
for i, x := range ind[:len(ind)-1] {
if x > 0 {
for _, y := range ind[i+1:] {
if y >= 0 && x > y {
check.errorf(e, "invalid slice indices: %d > %d", x, y)
for j, y := range ind[i+1:] {
if y >= 0 && y < x {
// The value y corresponds to the expression e.Index[i+1+j].
// Because y >= 0, it must have been set from the expression
// when checking indices and thus e.Index[i+1+j] is not nil.
check.errorf(e.Index[i+1+j], "invalid slice indices: %d < %d", y, x)
break L // only report one error, ok to continue
}
}

View File

@ -45,9 +45,9 @@ func indexes() {
_ = a[:10:10]
_ = a[:11 /* ERROR "index .* out of bounds" */ :10]
_ = a[:10:11 /* ERROR "index .* out of bounds" */ ]
_ = a[10:0:10] /* ERROR "invalid slice indices" */
_ = a[0:10:0] /* ERROR "invalid slice indices" */
_ = a[10:0:0] /* ERROR "invalid slice indices" */
_ = a[10:0 /* ERROR "invalid slice indices" */ :10]
_ = a[0:10:0 /* ERROR "invalid slice indices" */ ]
_ = a[10:0 /* ERROR "invalid slice indices" */:0]
_ = &a /* ERROR "cannot take address" */ [:10]
pa := &a
@ -63,9 +63,9 @@ func indexes() {
_ = pa[:10:10]
_ = pa[:11 /* ERROR "index .* out of bounds" */ :10]
_ = pa[:10:11 /* ERROR "index .* out of bounds" */ ]
_ = pa[10:0:10] /* ERROR "invalid slice indices" */
_ = pa[0:10:0] /* ERROR "invalid slice indices" */
_ = pa[10:0:0] /* ERROR "invalid slice indices" */
_ = pa[10:0 /* ERROR "invalid slice indices" */ :10]
_ = pa[0:10:0 /* ERROR "invalid slice indices" */ ]
_ = pa[10:0 /* ERROR "invalid slice indices" */ :0]
_ = &pa /* ERROR "cannot take address" */ [:10]
var b [0]int
@ -90,9 +90,9 @@ func indexes() {
_ = s[1 /* ERROR "overflows" */ <<100 : 1 /* ERROR "overflows" */ <<100]
_ = s[: /* ERROR "middle index required" */ : /* ERROR "final index required" */ ]
_ = s[:10:10]
_ = s[10:0:10] /* ERROR "invalid slice indices" */
_ = s[0:10:0] /* ERROR "invalid slice indices" */
_ = s[10:0:0] /* ERROR "invalid slice indices" */
_ = s[10:0 /* ERROR "invalid slice indices" */ :10]
_ = s[0:10:0 /* ERROR "invalid slice indices" */ ]
_ = s[10:0 /* ERROR "invalid slice indices" */ :0]
_ = &s /* ERROR "cannot take address" */ [:10]
var m map[string]int