]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: better position for invalid slice indices error
authorRobert Griesemer <gri@golang.org>
Sat, 13 Nov 2021 21:57:48 +0000 (13:57 -0800)
committerRobert Griesemer <gri@golang.org>
Mon, 15 Nov 2021 21:22:15 +0000 (21:22 +0000)
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>
src/cmd/compile/internal/types2/index.go
src/cmd/compile/internal/types2/testdata/check/expr3.src

index c773ae8ad3eb3f1f461e00642f924db1af15cfb0..4995d2d730329cd4673de385ce3a5f28733c62e6 100644 (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
                                }
                        }
index 523214461f20ccdb6e2c7fa6ed57be8e885c2558..0d7bbae9f9533fdb3ec1830e6c5bc04c08d99e6d 100644 (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