Skip to content

Commit 5beeb53

Browse files
committed
Reword Range*/[Range*]: Iterator E0277 messages
1 parent c71228e commit 5beeb53

File tree

5 files changed

+113
-10
lines changed

5 files changed

+113
-10
lines changed

src/libcore/iter/iterator.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@ fn _assert_is_object_safe(_: &dyn Iterator<Item=()>) {}
4040
_Self="[std::ops::RangeFrom<Idx>; 1]",
4141
label="if you meant to iterate from a value onwards, remove the square brackets",
4242
note="`[start..]` is an array of one `RangeFrom`; you might have meant to have a \
43-
`RangeFrom` without the brackets: `start..`"
43+
`RangeFrom` without the brackets: `start..`, keeping in mind that iterating over an \
44+
unbounded iterator will run forever unless you `break` or `return` from within the \
45+
loop"
4446
),
4547
on(
4648
_Self="[std::ops::RangeTo<Idx>; 1]",
47-
label="if you meant to iterate until a value, remove the square brackets",
48-
note="`[..end]` is an array of one `RangeTo`; you might have meant to have a \
49-
`RangeTo` without the brackets: `..end`"
49+
label="if you meant to iterate until a value, remove the square brackets and add a \
50+
starting value",
51+
note="`[..end]` is an array of one `RangeTo`; you might have meant to have a bounded \
52+
`Range` without the brackets: `0..end`"
5053
),
5154
on(
5255
_Self="[std::ops::RangeInclusive<Idx>; 1]",
@@ -56,9 +59,22 @@ fn _assert_is_object_safe(_: &dyn Iterator<Item=()>) {}
5659
),
5760
on(
5861
_Self="[std::ops::RangeToInclusive<Idx>; 1]",
59-
label="if you meant to iterate until a value, remove the square brackets",
62+
label="if you meant to iterate until a value (including it), remove the square brackets \
63+
and add a starting value",
6064
note="`[..=end]` is an array of one `RangeToInclusive`; you might have meant to have a \
61-
`RangeToInclusive` without the brackets: `..=end`"
65+
bounded `RangeInclusive` without the brackets: `0..=end`"
66+
),
67+
on(
68+
_Self="std::ops::RangeTo<Idx>",
69+
label="if you meant to iterate until a value, add a starting value",
70+
note="`..end` is a `RangeTo`, which cannot be iterated on; you might have meant to have a \
71+
bounded `Range`: `0..end`"
72+
),
73+
on(
74+
_Self="std::ops::RangeToInclusive<Idx>",
75+
label="if you meant to iterate until a value (including it), add a starting value",
76+
note="`..=end` is a `RangeToInclusive`, which cannot be iterated on; you might have meant \
77+
to have a bounded `RangeInclusive`: `0..=end`"
6278
),
6379
on(
6480
_Self="&str",
+5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
fn main() {
22
for _ in [0..1] {}
3+
for _ in [0..=1] {}
4+
for _ in [0..] {}
5+
for _ in [..1] {}
6+
for _ in [..=1] {}
37
let start = 0;
48
let end = 0;
59
for _ in [start..end] {}
610
let array_of_range = [start..end];
711
for _ in array_of_range {}
812
for _ in [0..1, 2..3] {}
13+
for _ in [0..=1] {}
914
}

src/test/ui/iterators/array-of-ranges.stderr

+54-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,49 @@ LL | for _ in [0..1] {}
88
= note: `[start..end]` is an array of one `Range`; you might have meant to have a `Range` without the brackets: `start..end`
99
= note: required by `std::iter::IntoIterator::into_iter`
1010

11-
error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator
11+
error[E0277]: `[std::ops::RangeInclusive<{integer}>; 1]` is not an iterator
12+
--> $DIR/array-of-ranges.rs:3:14
13+
|
14+
LL | for _ in [0..=1] {}
15+
| ^^^^^^^ if you meant to iterate between two values, remove the square brackets
16+
|
17+
= help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeInclusive<{integer}>; 1]`
18+
= note: `[start..=end]` is an array of one `RangeInclusive`; you might have meant to have a `RangeInclusive` without the brackets: `start..=end`
19+
= note: required by `std::iter::IntoIterator::into_iter`
20+
21+
error[E0277]: `[std::ops::RangeFrom<{integer}>; 1]` is not an iterator
22+
--> $DIR/array-of-ranges.rs:4:14
23+
|
24+
LL | for _ in [0..] {}
25+
| ^^^^^ if you meant to iterate from a value onwards, remove the square brackets
26+
|
27+
= help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeFrom<{integer}>; 1]`
28+
= note: `[start..]` is an array of one `RangeFrom`; you might have meant to have a `RangeFrom` without the brackets: `start..`, keeping in mind that iterating over an unbounded iterator will run forever unless you `break` or `return` from within the loop
29+
= note: required by `std::iter::IntoIterator::into_iter`
30+
31+
error[E0277]: `[std::ops::RangeTo<{integer}>; 1]` is not an iterator
1232
--> $DIR/array-of-ranges.rs:5:14
1333
|
34+
LL | for _ in [..1] {}
35+
| ^^^^^ if you meant to iterate until a value, remove the square brackets and add a starting value
36+
|
37+
= help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeTo<{integer}>; 1]`
38+
= note: `[..end]` is an array of one `RangeTo`; you might have meant to have a bounded `Range` without the brackets: `0..end`
39+
= note: required by `std::iter::IntoIterator::into_iter`
40+
41+
error[E0277]: `[std::ops::RangeToInclusive<{integer}>; 1]` is not an iterator
42+
--> $DIR/array-of-ranges.rs:6:14
43+
|
44+
LL | for _ in [..=1] {}
45+
| ^^^^^^ if you meant to iterate until a value (including it), remove the square brackets and add a starting value
46+
|
47+
= help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeToInclusive<{integer}>; 1]`
48+
= note: `[..=end]` is an array of one `RangeToInclusive`; you might have meant to have a bounded `RangeInclusive` without the brackets: `0..=end`
49+
= note: required by `std::iter::IntoIterator::into_iter`
50+
51+
error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator
52+
--> $DIR/array-of-ranges.rs:9:14
53+
|
1454
LL | for _ in [start..end] {}
1555
| ^^^^^^^^^^^^ if you meant to iterate between two values, remove the square brackets
1656
|
@@ -19,7 +59,7 @@ LL | for _ in [start..end] {}
1959
= note: required by `std::iter::IntoIterator::into_iter`
2060

2161
error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator
22-
--> $DIR/array-of-ranges.rs:7:14
62+
--> $DIR/array-of-ranges.rs:11:14
2363
|
2464
LL | for _ in array_of_range {}
2565
| ^^^^^^^^^^^^^^ if you meant to iterate between two values, remove the square brackets
@@ -29,7 +69,7 @@ LL | for _ in array_of_range {}
2969
= note: required by `std::iter::IntoIterator::into_iter`
3070

3171
error[E0277]: `[std::ops::Range<{integer}>; 2]` is not an iterator
32-
--> $DIR/array-of-ranges.rs:8:14
72+
--> $DIR/array-of-ranges.rs:12:14
3373
|
3474
LL | for _ in [0..1, 2..3] {}
3575
| ^^^^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
@@ -38,6 +78,16 @@ LL | for _ in [0..1, 2..3] {}
3878
= note: arrays are not an iterators, but slices like the following are: `&[1, 2, 3]`
3979
= note: required by `std::iter::IntoIterator::into_iter`
4080

41-
error: aborting due to 4 previous errors
81+
error[E0277]: `[std::ops::RangeInclusive<{integer}>; 1]` is not an iterator
82+
--> $DIR/array-of-ranges.rs:13:14
83+
|
84+
LL | for _ in [0..=1] {}
85+
| ^^^^^^^ if you meant to iterate between two values, remove the square brackets
86+
|
87+
= help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeInclusive<{integer}>; 1]`
88+
= note: `[start..=end]` is an array of one `RangeInclusive`; you might have meant to have a `RangeInclusive` without the brackets: `start..=end`
89+
= note: required by `std::iter::IntoIterator::into_iter`
90+
91+
error: aborting due to 9 previous errors
4292

4393
For more information about this error, try `rustc --explain E0277`.

src/test/ui/iterators/ranges.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
for _ in ..10 {}
3+
//~^ ERROR E0277
4+
for _ in ..=10 {}
5+
//~^ ERROR E0277
6+
for _ in 0..10 {}
7+
for _ in 0..=10 {}
8+
for _ in 0.. {}
9+
}

src/test/ui/iterators/ranges.stderr

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0277]: `std::ops::RangeTo<{integer}>` is not an iterator
2+
--> $DIR/ranges.rs:2:14
3+
|
4+
LL | for _ in ..10 {}
5+
| ^^^^ if you meant to iterate until a value, add a starting value
6+
|
7+
= help: the trait `std::iter::Iterator` is not implemented for `std::ops::RangeTo<{integer}>`
8+
= note: `..end` is a `RangeTo`, which cannot be iterated on; you might have meant to have a bounded `Range`: `0..end`
9+
= note: required by `std::iter::IntoIterator::into_iter`
10+
11+
error[E0277]: `std::ops::RangeToInclusive<{integer}>` is not an iterator
12+
--> $DIR/ranges.rs:4:14
13+
|
14+
LL | for _ in ..=10 {}
15+
| ^^^^^ if you meant to iterate until a value (including it), add a starting value
16+
|
17+
= help: the trait `std::iter::Iterator` is not implemented for `std::ops::RangeToInclusive<{integer}>`
18+
= note: `..=end` is a `RangeToInclusive`, which cannot be iterated on; you might have meant to have a bounded `RangeInclusive`: `0..=end`
19+
= note: required by `std::iter::IntoIterator::into_iter`
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)