From 456f1ffe12062f99cf800dd20d81e12e4131cb69 Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Wed, 8 Jun 2022 11:09:08 +0000 Subject: [PATCH] Suggest using `iter()` or `into_iter()` for `Vec` We cannot do that for `&Vec` because `#[rustc_on_unimplemented]` is limited (it does not clean generic instantiation for references, only for ADTs). --- library/core/src/iter/traits/iterator.rs | 4 ++++ src/test/ui/iterators/vec-on-unimplemented.rs | 4 ++++ .../ui/iterators/vec-on-unimplemented.stderr | 20 +++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 src/test/ui/iterators/vec-on-unimplemented.rs create mode 100644 src/test/ui/iterators/vec-on-unimplemented.stderr diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 69f06fb06ef5d..1cc9133fc3dc4 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -40,6 +40,10 @@ fn _assert_is_object_safe(_: &dyn Iterator) {} label = "`{Self}` is not an iterator; try calling `.into_iter()` or `.iter()`" ), on(_Self = "&[]", label = "`{Self}` is not an iterator; try calling `.iter()`"), + on( + _Self = "std::vec::Vec", + label = "`{Self}` is not an iterator; try calling `.into_iter()` or `.iter()`" + ), on( _Self = "&str", label = "`{Self}` is not an iterator; try calling `.chars()` or `.bytes()`" diff --git a/src/test/ui/iterators/vec-on-unimplemented.rs b/src/test/ui/iterators/vec-on-unimplemented.rs new file mode 100644 index 0000000000000..42b5d36bfad4a --- /dev/null +++ b/src/test/ui/iterators/vec-on-unimplemented.rs @@ -0,0 +1,4 @@ +fn main() { + vec![true, false].map(|v| !v).collect::>(); + //~^ ERROR `Vec` is not an iterator +} diff --git a/src/test/ui/iterators/vec-on-unimplemented.stderr b/src/test/ui/iterators/vec-on-unimplemented.stderr new file mode 100644 index 0000000000000..afcce5c30ca02 --- /dev/null +++ b/src/test/ui/iterators/vec-on-unimplemented.stderr @@ -0,0 +1,20 @@ +error[E0599]: `Vec` is not an iterator + --> $DIR/vec-on-unimplemented.rs:2:23 + | +LL | vec![true, false].map(|v| !v).collect::>(); + | ^^^ `Vec` is not an iterator; try calling `.into_iter()` or `.iter()` + | + ::: $SRC_DIR/alloc/src/vec/mod.rs:LL:COL + | +LL | pub struct Vec { + | ------------------------------------------------------------------------------------------------ doesn't satisfy `Vec: Iterator` + | + = note: the following trait bounds were not satisfied: + `Vec: Iterator` + which is required by `&mut Vec: Iterator` + `[bool]: Iterator` + which is required by `&mut [bool]: Iterator` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0599`.