Suggest collecting into Vec<_>
when collecting into [_]
This commit is contained in:
parent
f43e3a86a7
commit
fea0015f93
3 changed files with 67 additions and 0 deletions
|
@ -81,6 +81,32 @@
|
|||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[rustc_on_unimplemented(
|
||||
on(
|
||||
_Self = "[{A}]",
|
||||
message = "a value of type `{Self}` cannot be built since `{Self}` has no definite size",
|
||||
label = "try explicitly collecting into a `Vec<{A}>`",
|
||||
),
|
||||
on(
|
||||
all(
|
||||
A = "{integer}",
|
||||
any(
|
||||
_Self = "[i8]",
|
||||
_Self = "[i16]",
|
||||
_Self = "[i32]",
|
||||
_Self = "[i64]",
|
||||
_Self = "[i128]",
|
||||
_Self = "[isize]",
|
||||
_Self = "[u8]",
|
||||
_Self = "[u16]",
|
||||
_Self = "[u32]",
|
||||
_Self = "[u64]",
|
||||
_Self = "[u128]",
|
||||
_Self = "[usize]"
|
||||
)
|
||||
),
|
||||
message = "a value of type `{Self}` cannot be built since `{Self}` has no definite size",
|
||||
label = "try explicitly collecting into a `Vec<{A}>`",
|
||||
),
|
||||
message = "a value of type `{Self}` cannot be built from an iterator \
|
||||
over elements of type `{A}`",
|
||||
label = "value of type `{Self}` cannot be built from `std::iter::Iterator<Item={A}>`"
|
||||
|
|
15
src/test/ui/iterators/collect-into-slice.rs
Normal file
15
src/test/ui/iterators/collect-into-slice.rs
Normal file
|
@ -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);
|
||||
}
|
26
src/test/ui/iterators/collect-into-slice.stderr
Normal file
26
src/test/ui/iterators/collect-into-slice.stderr
Normal file
|
@ -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`.
|
Loading…
Add table
Reference in a new issue