Skip to content

Commit 4f237ff

Browse files
committed
cmd/compile: verify that rangefunc assigning to no vars works
This adds a test for for range seq2rangefunc { ... } and for onevar := range seq2rangefunc { ... } For #65236. Change-Id: I083f8e4c19eb4ba0d6024d5314ac29d941141778 Reviewed-on: https://go-review.googlesource.com/c/go/+/596135 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent d0a468e commit 4f237ff

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/cmd/compile/internal/rangefunc/rangefunc_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,26 @@ var fail []error = []error{
285285
errorString(CERR_MISSING),
286286
}
287287

288+
// TestNoVars ensures that versions of rangefunc that use zero or one
289+
// iteration variable (instead of two) run the proper number of times
290+
// and in the one variable case supply the proper values.
291+
// For #65236.
292+
func TestNoVars(t *testing.T) {
293+
i, k := 0, 0
294+
for range Check2(OfSliceIndex([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})) {
295+
i++
296+
}
297+
for j := range Check2(OfSliceIndex([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})) {
298+
k += j
299+
}
300+
if i != 10 {
301+
t.Errorf("Expected 10, got %d", i)
302+
}
303+
if k != 45 {
304+
t.Errorf("Expected 45, got %d", k)
305+
}
306+
}
307+
288308
func TestCheck(t *testing.T) {
289309
i := 0
290310
defer func() {

0 commit comments

Comments
 (0)