cmd/gc: implement 'for range x {'
Fixes #6102.
LGTM=gri
R=ken, r, gri
CC=golang-codereviews
https://golang.org/cl/113120043
diff --git a/test/bench/shootout/chameneosredux.go b/test/bench/shootout/chameneosredux.go
index 3395798..72ce7dd 100644
--- a/test/bench/shootout/chameneosredux.go
+++ b/test/bench/shootout/chameneosredux.go
@@ -123,7 +123,7 @@
fmt.Println(msg)
tot := 0
// wait for all results
- for _ = range cols {
+ for range cols {
result := <-ended
tot += result.met
fmt.Printf("%v%v\n", result.met, spell(result.same, true))
diff --git a/test/chan/perm.go b/test/chan/perm.go
index 7e152c5..919fa30 100644
--- a/test/chan/perm.go
+++ b/test/chan/perm.go
@@ -56,6 +56,9 @@
for _ = range cs {// ERROR "receive"
}
+ for range cs {// ERROR "receive"
+ }
+
close(c)
close(cs)
close(cr) // ERROR "receive"
diff --git a/test/fixedbugs/bug173.go b/test/fixedbugs/bug173.go
index 6479bb2..3515c64 100644
--- a/test/fixedbugs/bug173.go
+++ b/test/fixedbugs/bug173.go
@@ -18,4 +18,6 @@
}
for _ = range t {
}
+ for range t {
+ }
}
diff --git a/test/fixedbugs/bug406.go b/test/fixedbugs/bug406.go
index c6f8534..6df3c5c 100644
--- a/test/fixedbugs/bug406.go
+++ b/test/fixedbugs/bug406.go
@@ -14,6 +14,8 @@
func (a matrix) equal() bool {
for _ = range a.e {
}
+ for range a.e {
+ }
return true
}
diff --git a/test/range.go b/test/range.go
index 8effbe9..af89eda 100644
--- a/test/range.go
+++ b/test/range.go
@@ -32,6 +32,13 @@
println("Wanted lowercase alphabet; got", s)
panic("fail")
}
+ n := 0
+ for range seq('a', 'z') {
+ n++
+ }
+ if n != 26 {
+ println("testchan wrong count", n, "want 26")
+ }
}
// test that range over slice only evaluates
@@ -87,6 +94,22 @@
}
}
+func testslice2() {
+ n := 0
+ nmake = 0
+ for range makeslice() {
+ n++
+ }
+ if nmake != 1 {
+ println("range called makeslice", nmake, "times")
+ panic("fail")
+ }
+ if n != 5 {
+ println("wrong count ranging over makeslice", n)
+ panic("fail")
+ }
+}
+
// test that range over array only evaluates
// the expression after "range" once.
@@ -127,6 +150,22 @@
}
}
+func testarray2() {
+ n := 0
+ nmake = 0
+ for range makearray() {
+ n++
+ }
+ if nmake != 1 {
+ println("range called makearray", nmake, "times")
+ panic("fail")
+ }
+ if n != 5 {
+ println("wrong count ranging over makearray", n)
+ panic("fail")
+ }
+}
+
func makearrayptr() *[5]int {
nmake++
return &[5]int{1, 2, 3, 4, 5}
@@ -176,6 +215,22 @@
}
}
+func testarrayptr2() {
+ n := 0
+ nmake = 0
+ for range makearrayptr() {
+ n++
+ }
+ if nmake != 1 {
+ println("range called makearrayptr", nmake, "times")
+ panic("fail")
+ }
+ if n != 5 {
+ println("wrong count ranging over makearrayptr", n)
+ panic("fail")
+ }
+}
+
// test that range over string only evaluates
// the expression after "range" once.
@@ -216,6 +271,22 @@
}
}
+func teststring2() {
+ n := 0
+ nmake = 0
+ for range makestring() {
+ n++
+ }
+ if nmake != 1 {
+ println("range called makestring", nmake, "times")
+ panic("fail")
+ }
+ if n != 5 {
+ println("wrong count ranging over makestring", n)
+ panic("fail")
+ }
+}
+
// test that range over map only evaluates
// the expression after "range" once.
@@ -256,6 +327,22 @@
}
}
+func testmap2() {
+ n := 0
+ nmake = 0
+ for range makemap() {
+ n++
+ }
+ if nmake != 1 {
+ println("range called makemap", nmake, "times")
+ panic("fail")
+ }
+ if n != 5 {
+ println("wrong count ranging over makemap", n)
+ panic("fail")
+ }
+}
+
// test that range evaluates the index and value expressions
// exactly once per iteration.
@@ -298,13 +385,18 @@
testchan()
testarray()
testarray1()
+ testarray2()
testarrayptr()
testarrayptr1()
+ testarrayptr2()
testslice()
testslice1()
+ testslice2()
teststring()
teststring1()
+ teststring2()
testmap()
testmap1()
+ testmap2()
testcalls()
}
diff --git a/test/stress/maps.go b/test/stress/maps.go
index d022e19..fc5ab05 100644
--- a/test/stress/maps.go
+++ b/test/stress/maps.go
@@ -97,6 +97,8 @@
func (m intMap) RangeAll() {
for _ = range m {
}
+ for range m {
+ }
}
func stressMaps() {