Skip to content

Commit fea0015

Browse files
Suggest collecting into Vec<_> when collecting into [_]
1 parent f43e3a8 commit fea0015

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

library/core/src/iter/traits/collect.rs

+26
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@
8181
/// ```
8282
#[stable(feature = "rust1", since = "1.0.0")]
8383
#[rustc_on_unimplemented(
84+
on(
85+
_Self = "[{A}]",
86+
message = "a value of type `{Self}` cannot be built since `{Self}` has no definite size",
87+
label = "try explicitly collecting into a `Vec<{A}>`",
88+
),
89+
on(
90+
all(
91+
A = "{integer}",
92+
any(
93+
_Self = "[i8]",
94+
_Self = "[i16]",
95+
_Self = "[i32]",
96+
_Self = "[i64]",
97+
_Self = "[i128]",
98+
_Self = "[isize]",
99+
_Self = "[u8]",
100+
_Self = "[u16]",
101+
_Self = "[u32]",
102+
_Self = "[u64]",
103+
_Self = "[u128]",
104+
_Self = "[usize]"
105+
)
106+
),
107+
message = "a value of type `{Self}` cannot be built since `{Self}` has no definite size",
108+
label = "try explicitly collecting into a `Vec<{A}>`",
109+
),
84110
message = "a value of type `{Self}` cannot be built from an iterator \
85111
over elements of type `{A}`",
86112
label = "value of type `{Self}` cannot be built from `std::iter::Iterator<Item={A}>`"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
fn process_slice(data: &[i32]) {
2+
//~^ NOTE required by a bound in this
3+
todo!()
4+
}
5+
6+
fn main() {
7+
let some_generated_vec = (0..10).collect();
8+
//~^ ERROR the size for values of type `[i32]` cannot be known at compilation time
9+
//~| ERROR a value of type `[i32]` cannot be built since `[i32]` has no definite size
10+
//~| NOTE try explicitly collecting into a `Vec<{integer}>`
11+
//~| NOTE required by a bound in `collect`
12+
//~| NOTE all local variables must have a statically known size
13+
//~| NOTE doesn't have a size known at compile-time
14+
process_slice(&some_generated_vec);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0277]: the size for values of type `[i32]` cannot be known at compilation time
2+
--> $DIR/collect-into-slice.rs:7:9
3+
|
4+
LL | let some_generated_vec = (0..10).collect();
5+
| ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `[i32]`
8+
= note: all local variables must have a statically known size
9+
= help: unsized locals are gated as an unstable feature
10+
11+
error[E0277]: a value of type `[i32]` cannot be built since `[i32]` has no definite size
12+
--> $DIR/collect-into-slice.rs:7:38
13+
|
14+
LL | let some_generated_vec = (0..10).collect();
15+
| ^^^^^^^ try explicitly collecting into a `Vec<{integer}>`
16+
|
17+
= help: the trait `FromIterator<{integer}>` is not implemented for `[i32]`
18+
note: required by a bound in `collect`
19+
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
20+
|
21+
LL | fn collect<B: FromIterator<Self::Item>>(self) -> B
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `collect`
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)