[go] go/types: better position for invalid slice indices error

12 views
Skip to first unread message

Robert Findley (Gerrit)

unread,
Nov 17, 2021, 9:19:58 PM11/17/21
to goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Robert Griesemer, Go Bot, golang-co...@googlegroups.com

Robert Findley submitted this change.

View Change


Approvals: Robert Griesemer: Looks good to me, approved Robert Findley: Trusted; Run TryBots Go Bot: TryBots succeeded
go/types: better position for invalid slice indices error

This is a port of CL 363671 from types2 to go/types. Also adjust the
error message to match types2 ("invalid" vs "swapped").

Change-Id: I662a73c915814fea14bfcb1ebde0fbf39589f022
Reviewed-on: https://go-review.googlesource.com/c/go/+/364902
Trust: Robert Findley <rfin...@google.com>
Run-TryBot: Robert Findley <rfin...@google.com>
TryBot-Result: Go Bot <go...@golang.org>
Reviewed-by: Robert Griesemer <g...@golang.org>
---
M src/go/types/index.go
M src/go/types/testdata/check/expr3.src
2 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/src/go/types/index.go b/src/go/types/index.go
index 6ea25bc..db4732c 100644
--- a/src/go/types/index.go
+++ b/src/go/types/index.go
@@ -310,9 +310,13 @@
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(inNode(e, e.Rbrack), _SwappedSliceIndices, "swapped 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.
+ at := []ast.Expr{e.Low, e.High, e.Max}[i+1+j]
+ check.errorf(at, _SwappedSliceIndices, "invalid slice indices: %d < %d", y, x)
break L // only report one error, ok to continue
}
}
diff --git a/src/go/types/testdata/check/expr3.src b/src/go/types/testdata/check/expr3.src
index a63542b..5117a03 100644
--- a/src/go/types/testdata/check/expr3.src
+++ b/src/go/types/testdata/check/expr3.src
@@ -44,9 +44,9 @@
_ = a[:10:10]
_ = a[:11 /* ERROR "index .* out of bounds" */ :10]
_ = a[:10:11 /* ERROR "index .* out of bounds" */ ]
- _ = a[10:0:10] /* ERROR swapped slice indices" */
- _ = a[0:10:0] /* ERROR "swapped slice indices" */
- _ = a[10:0:0] /* ERROR "swapped 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
@@ -62,9 +62,9 @@
_ = pa[:10:10]
_ = pa[:11 /* ERROR "index .* out of bounds" */ :10]
_ = pa[:10:11 /* ERROR "index .* out of bounds" */ ]
- _ = pa[10:0:10] /* ERROR "swapped slice indices" */
- _ = pa[0:10:0] /* ERROR "swapped slice indices" */
- _ = pa[10:0:0] /* ERROR "swapped 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
@@ -82,16 +82,16 @@
_ = s[: - /* ERROR "negative" */ 1]
_ = s[0]
_ = s[1:2]
- _ = s[2:1] /* ERROR "swapped slice indices" */
+ _ = s[2:1 /* ERROR "invalid slice indices" */ ]
_ = s[2:]
_ = s[: 1 /* ERROR "overflows" */ <<100]
_ = s[1 /* ERROR "overflows" */ <<100 :]
_ = s[1 /* ERROR "overflows" */ <<100 : 1 /* ERROR "overflows" */ <<100]
_ = s[: /* ERROR "2nd index required" */ : /* ERROR "3rd index required" */ ]
_ = s[:10:10]
- _ = s[10:0:10] /* ERROR "swapped slice indices" */
- _ = s[0:10:0] /* ERROR "swapped slice indices" */
- _ = s[10:0:0] /* ERROR "swapped 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

To view, visit change 364902. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I662a73c915814fea14bfcb1ebde0fbf39589f022
Gerrit-Change-Number: 364902
Gerrit-PatchSet: 2
Gerrit-Owner: Robert Findley <rfin...@google.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Robert Findley <rfin...@google.com>
Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
Gerrit-MessageType: merged
Reply all
Reply to author
Forward
0 new messages