-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Unhelpful error message using impl trait to return a closure. #38615
Comments
I ran into this too, but returning an iterator. In my case adding the lifetime is required in addition to marking the closure fn apply_permutation<'a, T>(
objects: &'a [T],
permutation: &'a [usize],
) -> impl Iterator<Item = T> + 'a
where
T: Copy,
{
permutation.iter().map(move |&i| objects[i])
} |
I ran into this too while trying to return a generator... #![feature(generators, generator_trait, conservative_impl_trait)]
use std::ops::{Generator};
struct Stack {
x: i32
}
impl Stack {
fn new(x: i32) -> Stack { Stack{x: x} }
fn gen(& mut self) -> impl Generator<Yield=i32,Return=()> {
// fn gen<'a>(&'a mut self) -> impl Generator<Yield=i32,Return=()> + 'a {
move || { for _i in 0..10 { self.x = self.x + 1; yield self.x; } return (); }
}
} (The commented out line is the correct signature) The error message is not useful, at all: Compiling playground v0.0.1 (file:///playground)
error[E0564]: only named lifetimes are allowed in `impl Trait`, but `` was found in the type `[generator@src/main.rs:13:9: 13:86 self:&mut Stack ((), std::ops::Range<i32>, i32, fn(std::ops::Range<i32>) -> <std::ops::Range<i32> as std::iter::IntoIterator>::IntoIter {<std::ops::Range<i32> as std::iter::IntoIterator>::into_iter}, Stack)]`
--> src/main.rs:11:27
|
11 | fn gen(& mut self) -> impl Generator<Yield=i32,Return=()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This error message is so bad that it should IMO block the stabilization of conservative impl trait (@withoutboats). Also, when I click the E0564 on the playground it sends me to a page that doesn't contain an explanation for it. Maybe that is a playground bug, and the playground should be sending me to some nightly page... Anyhow, this doesn't make this any better. |
The issues #43719 and #39722 seem to be duplicates of this one. I've just rechecked, and the playground does indeed have a bug (rust-lang/rust-playground#207) in that the playground always links to the stable error explanations and not to the nightly ones when compiling with nightly. However, the error at hand here, E0564, doesn't have an explanation at all. Trying to return closures/generators, and trying to return iterators, seem to be the most common cause of triggering this error in all the issues currently filled, so maybe an explanation should cover that. |
This has been fixed. |
Current output:
The suggestion is correct. #38615 (comment) now compiles. #38615 (comment) now suggests the correct fix:
|
The following code (Rust Playground link)
produces the error:
This error can be gotten rid of, by adding a lifetime constraint:
But that still leaves an error about borrowing the
log
reference:Fixing this (by making the closure a
move
, as is suggested) doesn't require the named lifetime, so really the error second error message should be the only one presented.The text was updated successfully, but these errors were encountered: