diff options
author | Keith Randall <khr@google.com> | 2018-07-02 15:21:35 -0700 |
---|---|---|
committer | Keith Randall <khr@golang.org> | 2018-07-09 18:23:39 +0000 |
commit | 58d287e5e863cd8d3c3525e1a04e424e748cf242 (patch) | |
tree | df0d5ffeab78593f18073b764bccded84e62a9a5 /test/loopbce.go | |
parent | 9b7a8aaaf3adbc330ef724fb581b3bfa72ab2a49 (diff) | |
download | go-58d287e5e863cd8d3c3525e1a04e424e748cf242.tar.gz go-58d287e5e863cd8d3c3525e1a04e424e748cf242.zip |
cmd/compile: ensure that loop condition is detected correctly
We need to make sure that the terminating comparison has the right
sense given the increment direction. If the increment is positive,
the terminating comparsion must be < or <=. If the increment is
negative, the terminating comparison must be > or >=.
Do a few cleanups, like constant-folding entry==0, adding comments,
removing unused "exported" fields.
Fixes #26116
Change-Id: I14230ee8126054b750e2a1f2b18eb8f09873dbd5
Reviewed-on: https://go-review.googlesource.com/121940
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
Diffstat (limited to 'test/loopbce.go')
-rw-r--r-- | test/loopbce.go | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/test/loopbce.go b/test/loopbce.go index c93bfc8f00..b4bf797497 100644 --- a/test/loopbce.go +++ b/test/loopbce.go @@ -86,7 +86,7 @@ func g0b(a string) int { func g0c(a string) int { x := 0 - for i := len(a); i > 0; i-- { // ERROR "Induction variable: limits \(0,\?\], increment -1$" + for i := len(a); i > 0; i-- { // ERROR "Induction variable: limits \(0,\?\], increment 1$" x += int(a[i-1]) // ERROR "Proved IsInBounds$" } return x @@ -94,7 +94,7 @@ func g0c(a string) int { func g0d(a string) int { x := 0 - for i := len(a); 0 < i; i-- { // ERROR "Induction variable: limits \(0,\?\], increment -1$" + for i := len(a); 0 < i; i-- { // ERROR "Induction variable: limits \(0,\?\], increment 1$" x += int(a[i-1]) // ERROR "Proved IsInBounds$" } return x @@ -102,7 +102,7 @@ func g0d(a string) int { func g0e(a string) int { x := 0 - for i := len(a) - 1; i >= 0; i-- { // ERROR "Induction variable: limits \[0,\?\], increment -1$" + for i := len(a) - 1; i >= 0; i-- { // ERROR "Induction variable: limits \[0,\?\], increment 1$" x += int(a[i]) // ERROR "Proved IsInBounds$" } return x @@ -110,7 +110,7 @@ func g0e(a string) int { func g0f(a string) int { x := 0 - for i := len(a) - 1; 0 <= i; i-- { // ERROR "Induction variable: limits \[0,\?\], increment -1$" + for i := len(a) - 1; 0 <= i; i-- { // ERROR "Induction variable: limits \[0,\?\], increment 1$" x += int(a[i]) // ERROR "Proved IsInBounds$" } return x @@ -223,7 +223,7 @@ func k3(a [100]int) [100]int { } func k3neg(a [100]int) [100]int { - for i := 89; i > -11; i-- { // ERROR "Induction variable: limits \(-11,89\], increment -1$" + for i := 89; i > -11; i-- { // ERROR "Induction variable: limits \(-11,89\], increment 1$" a[i+9] = i a[i+10] = i // ERROR "Proved IsInBounds$" a[i+11] = i @@ -232,7 +232,7 @@ func k3neg(a [100]int) [100]int { } func k3neg2(a [100]int) [100]int { - for i := 89; i >= -10; i-- { // ERROR "Induction variable: limits \[-10,89\], increment -1$" + for i := 89; i >= -10; i-- { // ERROR "Induction variable: limits \[-10,89\], increment 1$" a[i+9] = i a[i+10] = i // ERROR "Proved IsInBounds$" a[i+11] = i @@ -302,6 +302,16 @@ func nobce3(a [100]int64) [100]int64 { return a } +func issue26116a(a []int) { + // There is no induction variable here. The comparison is in the wrong direction. + for i := 3; i > 6; i++ { + a[i] = 0 + } + for i := 7; i < 3; i-- { + a[i] = 1 + } +} + //go:noinline func useString(a string) { } |