Skip to content

Commit

Permalink
Reword Range*/[Range*]: Iterator E0277 messages
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Oct 11, 2018
1 parent c71228e commit 5beeb53
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 10 deletions.
28 changes: 22 additions & 6 deletions src/libcore/iter/iterator.rs
Expand Up @@ -40,13 +40,16 @@ fn _assert_is_object_safe(_: &dyn Iterator<Item=()>) {}
_Self="[std::ops::RangeFrom<Idx>; 1]",
label="if you meant to iterate from a value onwards, remove the square brackets",
note="`[start..]` is an array of one `RangeFrom`; you might have meant to have a \
`RangeFrom` without the brackets: `start..`"
`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"
),
on(
_Self="[std::ops::RangeTo<Idx>; 1]",
label="if you meant to iterate until a value, remove the square brackets",
note="`[..end]` is an array of one `RangeTo`; you might have meant to have a \
`RangeTo` without the brackets: `..end`"
label="if you meant to iterate until a value, remove the square brackets and add a \
starting value",
note="`[..end]` is an array of one `RangeTo`; you might have meant to have a bounded \
`Range` without the brackets: `0..end`"
),
on(
_Self="[std::ops::RangeInclusive<Idx>; 1]",
Expand All @@ -56,9 +59,22 @@ fn _assert_is_object_safe(_: &dyn Iterator<Item=()>) {}
),
on(
_Self="[std::ops::RangeToInclusive<Idx>; 1]",
label="if you meant to iterate until a value, remove the square brackets",
label="if you meant to iterate until a value (including it), remove the square brackets \
and add a starting value",
note="`[..=end]` is an array of one `RangeToInclusive`; you might have meant to have a \
`RangeToInclusive` without the brackets: `..=end`"
bounded `RangeInclusive` without the brackets: `0..=end`"
),
on(
_Self="std::ops::RangeTo<Idx>",
label="if you meant to iterate until a value, add a starting value",
note="`..end` is a `RangeTo`, which cannot be iterated on; you might have meant to have a \
bounded `Range`: `0..end`"
),
on(
_Self="std::ops::RangeToInclusive<Idx>",
label="if you meant to iterate until a value (including it), add a starting value",
note="`..=end` is a `RangeToInclusive`, which cannot be iterated on; you might have meant \
to have a bounded `RangeInclusive`: `0..=end`"
),
on(
_Self="&str",
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/iterators/array-of-ranges.rs
@@ -1,9 +1,14 @@
fn main() {
for _ in [0..1] {}
for _ in [0..=1] {}
for _ in [0..] {}
for _ in [..1] {}
for _ in [..=1] {}
let start = 0;
let end = 0;
for _ in [start..end] {}
let array_of_range = [start..end];
for _ in array_of_range {}
for _ in [0..1, 2..3] {}
for _ in [0..=1] {}
}
58 changes: 54 additions & 4 deletions src/test/ui/iterators/array-of-ranges.stderr
Expand Up @@ -8,9 +8,49 @@ LL | for _ in [0..1] {}
= note: `[start..end]` is an array of one `Range`; you might have meant to have a `Range` without the brackets: `start..end`
= note: required by `std::iter::IntoIterator::into_iter`

error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator
error[E0277]: `[std::ops::RangeInclusive<{integer}>; 1]` is not an iterator
--> $DIR/array-of-ranges.rs:3:14
|
LL | for _ in [0..=1] {}
| ^^^^^^^ if you meant to iterate between two values, remove the square brackets
|
= help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeInclusive<{integer}>; 1]`
= note: `[start..=end]` is an array of one `RangeInclusive`; you might have meant to have a `RangeInclusive` without the brackets: `start..=end`
= note: required by `std::iter::IntoIterator::into_iter`

error[E0277]: `[std::ops::RangeFrom<{integer}>; 1]` is not an iterator
--> $DIR/array-of-ranges.rs:4:14
|
LL | for _ in [0..] {}
| ^^^^^ if you meant to iterate from a value onwards, remove the square brackets
|
= help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeFrom<{integer}>; 1]`
= 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
= note: required by `std::iter::IntoIterator::into_iter`

error[E0277]: `[std::ops::RangeTo<{integer}>; 1]` is not an iterator
--> $DIR/array-of-ranges.rs:5:14
|
LL | for _ in [..1] {}
| ^^^^^ if you meant to iterate until a value, remove the square brackets and add a starting value
|
= help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeTo<{integer}>; 1]`
= note: `[..end]` is an array of one `RangeTo`; you might have meant to have a bounded `Range` without the brackets: `0..end`
= note: required by `std::iter::IntoIterator::into_iter`

error[E0277]: `[std::ops::RangeToInclusive<{integer}>; 1]` is not an iterator
--> $DIR/array-of-ranges.rs:6:14
|
LL | for _ in [..=1] {}
| ^^^^^^ if you meant to iterate until a value (including it), remove the square brackets and add a starting value
|
= help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeToInclusive<{integer}>; 1]`
= note: `[..=end]` is an array of one `RangeToInclusive`; you might have meant to have a bounded `RangeInclusive` without the brackets: `0..=end`
= note: required by `std::iter::IntoIterator::into_iter`

error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator
--> $DIR/array-of-ranges.rs:9:14
|
LL | for _ in [start..end] {}
| ^^^^^^^^^^^^ if you meant to iterate between two values, remove the square brackets
|
Expand All @@ -19,7 +59,7 @@ LL | for _ in [start..end] {}
= note: required by `std::iter::IntoIterator::into_iter`

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

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

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

error: aborting due to 9 previous errors

For more information about this error, try `rustc --explain E0277`.
9 changes: 9 additions & 0 deletions src/test/ui/iterators/ranges.rs
@@ -0,0 +1,9 @@
fn main() {
for _ in ..10 {}
//~^ ERROR E0277
for _ in ..=10 {}
//~^ ERROR E0277
for _ in 0..10 {}
for _ in 0..=10 {}
for _ in 0.. {}
}
23 changes: 23 additions & 0 deletions src/test/ui/iterators/ranges.stderr
@@ -0,0 +1,23 @@
error[E0277]: `std::ops::RangeTo<{integer}>` is not an iterator
--> $DIR/ranges.rs:2:14
|
LL | for _ in ..10 {}
| ^^^^ if you meant to iterate until a value, add a starting value
|
= help: the trait `std::iter::Iterator` is not implemented for `std::ops::RangeTo<{integer}>`
= note: `..end` is a `RangeTo`, which cannot be iterated on; you might have meant to have a bounded `Range`: `0..end`
= note: required by `std::iter::IntoIterator::into_iter`

error[E0277]: `std::ops::RangeToInclusive<{integer}>` is not an iterator
--> $DIR/ranges.rs:4:14
|
LL | for _ in ..=10 {}
| ^^^^^ if you meant to iterate until a value (including it), add a starting value
|
= help: the trait `std::iter::Iterator` is not implemented for `std::ops::RangeToInclusive<{integer}>`
= note: `..=end` is a `RangeToInclusive`, which cannot be iterated on; you might have meant to have a bounded `RangeInclusive`: `0..=end`
= note: required by `std::iter::IntoIterator::into_iter`

error: aborting due to 2 previous errors

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

0 comments on commit 5beeb53

Please sign in to comment.