about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-02-09 23:29:55 +0100
committerGitHub <noreply@github.com>2022-02-09 23:29:55 +0100
commit9634559599537e334cbfa854446d048e6ebe5ee9 (patch)
treeccbdfb0d1c274fff3ac3077f85356e112145c2a0 /src
parente7aca895980f25f6d2d3c48e10fd04656764d1e4 (diff)
parentfea0015f9320f59c46821957a01f688f79a02546 (diff)
Rollup merge of #91443 - compiler-errors:bad_collect_into_slice, r=wesleywiser
Better suggestions when user tries to collect into an unsized `[_]`

1. Extend the predicate on `rustc_on_unimplemented` to support substitutions like note, label, etc (i.e. treat it as a `OnUnimplementedFormatString`) so we can have slightly more general `rustc_on_unimplemented` special-cases.
2. Add a `rustc_on_unimplemented` if we fail on `FromIterator<A> for [A]` which happens when we don't explicitly collect into a `vec<A>`, but then pass the return from a `.collect` call into something that takes a slice.

Fixes #91423
Diffstat (limited to 'src')
-rw-r--r--src/test/ui/iterators/collect-into-slice.rs15
-rw-r--r--src/test/ui/iterators/collect-into-slice.stderr26
2 files changed, 41 insertions, 0 deletions
diff --git a/src/test/ui/iterators/collect-into-slice.rs b/src/test/ui/iterators/collect-into-slice.rs
new file mode 100644
index 00000000000..905752dec74
--- /dev/null
+++ b/src/test/ui/iterators/collect-into-slice.rs
@@ -0,0 +1,15 @@
+fn process_slice(data: &[i32]) {
+    //~^ NOTE required by a bound in this
+    todo!()
+}
+
+fn main() {
+    let some_generated_vec = (0..10).collect();
+    //~^ ERROR the size for values of type `[i32]` cannot be known at compilation time
+    //~| ERROR a value of type `[i32]` cannot be built since `[i32]` has no definite size
+    //~| NOTE try explicitly collecting into a `Vec<{integer}>`
+    //~| NOTE required by a bound in `collect`
+    //~| NOTE all local variables must have a statically known size
+    //~| NOTE doesn't have a size known at compile-time
+    process_slice(&some_generated_vec);
+}
diff --git a/src/test/ui/iterators/collect-into-slice.stderr b/src/test/ui/iterators/collect-into-slice.stderr
new file mode 100644
index 00000000000..521f239451d
--- /dev/null
+++ b/src/test/ui/iterators/collect-into-slice.stderr
@@ -0,0 +1,26 @@
+error[E0277]: the size for values of type `[i32]` cannot be known at compilation time
+  --> $DIR/collect-into-slice.rs:7:9
+   |
+LL |     let some_generated_vec = (0..10).collect();
+   |         ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `[i32]`
+   = note: all local variables must have a statically known size
+   = help: unsized locals are gated as an unstable feature
+
+error[E0277]: a value of type `[i32]` cannot be built since `[i32]` has no definite size
+  --> $DIR/collect-into-slice.rs:7:38
+   |
+LL |     let some_generated_vec = (0..10).collect();
+   |                                      ^^^^^^^ try explicitly collecting into a `Vec<{integer}>`
+   |
+   = help: the trait `FromIterator<{integer}>` is not implemented for `[i32]`
+note: required by a bound in `collect`
+  --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
+   |
+LL |     fn collect<B: FromIterator<Self::Item>>(self) -> B
+   |                   ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `collect`
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0277`.