Skip to content

Give better error when collecting into &[T] #111612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
}
}
}

// `&[{integral}]` - `FromIterator` needs that.
if let ty::Ref(_, ref_ty, rustc_ast::Mutability::Not) = self_ty.kind()
&& let ty::Slice(sty) = ref_ty.kind()
&& sty.is_integral()
{
flags.push((sym::_Self, Some("&[{integral}]".to_owned())));
}
});

if let Ok(Some(command)) = OnUnimplementedDirective::of_item(self.tcx, def_id) {
Expand Down
10 changes: 10 additions & 0 deletions library/core/src/iter/traits/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented(
on(
_Self = "&[{A}]",
message = "a slice of type `{Self}` cannot be built since we need to store the elements somewhere",
label = "try explicitly collecting into a `Vec<{A}>`",
),
on(
all(A = "{integer}", any(_Self = "&[{integral}]",)),
message = "a slice of type `{Self}` cannot be built since we need to store the elements somewhere",
label = "try explicitly collecting into a `Vec<{A}>`",
),
on(
_Self = "[{A}]",
message = "a slice of type `{Self}` cannot be built since `{Self}` has no definite size",
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/iterators/collect-into-slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ fn main() {
//~| NOTE doesn't have a size known at compile-time
//~| NOTE doesn't have a size known at compile-time
process_slice(&some_generated_vec);

let some_generated_vec = (0..10).collect();
//~^ ERROR a slice of type `&[i32]` cannot be built since we need to store the elements somewhere
//~| NOTE try explicitly collecting into a `Vec<{integer}>`
//~| NOTE required by a bound in `collect`
process_slice(some_generated_vec);
}
12 changes: 11 additions & 1 deletion tests/ui/iterators/collect-into-slice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ LL | let some_generated_vec = (0..10).collect();
note: required by a bound in `collect`
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL

error: aborting due to 3 previous errors
error[E0277]: a slice of type `&[i32]` cannot be built since we need to store the elements somewhere
--> $DIR/collect-into-slice.rs:18: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

error: aborting due to 4 previous errors

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