Open p0lunin opened 3 months ago
Here's minimized code that causes a different but similarly confusing error message. Unsure if this still preserves the essence of the original code.
async fn listen() {
let things: Vec<Vec<i32>> = vec![];
for _ in things.iter().map(|n| n.iter()).flatten() {
// comment this line and everything compiles
async {}.await;
}
}
fn require_send<T: Send>(_x: T) {}
fn foo() {
let future = listen();
require_send(future);
}
Error message:
Compiling playground v0.0.1 (/playground)
error: implementation of `FnOnce` is not general enough
--> src/lib.rs:13:5
|
13 | require_send(future);
| ^^^^^^^^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
|
= note: closure with signature `fn(&'0 Vec<i32>) -> std::slice::Iter<'_, i32>` must implement `FnOnce<(&'1 Vec<i32>,)>`, for any two lifetimes `'0` and `'1`...
= note: ...but it actually implements `FnOnce<(&Vec<i32>,)>`
error: could not compile `playground` (lib) due to 1 previous error
Both fn iter<T>(v: &Vec<T>) -> Iter<'_, T> { v.iter() }
replacing closure and .flat_map(|n| n.iter())
make the code compile. The difference between FlatMap
and Flatten
is that the first one unifies lifetimes of returned U: IntoIterator
s, and the other tries to unify Map::<I, F: Fn(_) -> _>::Item
s, but F isn't higher ranked closure, and as such it fails to do so. Feel free to correct me if I'm wrong.
@rustbot label -needs-triage +A-diagnostics +A-Async-Await +A-auto-traits +A-traits +D-confusing
This doesn't use closures and has the same issue.
async fn listen() {
let things: Vec<Vec<i32>> = vec![];
for _ in things.iter().map(id).flatten() {
// comment this line and everything compiles
async {}.await;
}
}
fn id<T>(x: T) -> T { x }
fn require_send<T: Send>(_x: T) {}
fn foo() {
let future = listen();
require_send(future);
}
Compiling playground v0.0.1 (/playground)
error: implementation of `FnOnce` is not general enough
--> src/lib.rs:15:5
|
15 | require_send(future);
| ^^^^^^^^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
|
= note: `fn(&'0 Vec<i32>) -> &'0 Vec<i32> {id::<&'0 Vec<i32>>}` must implement `FnOnce<(&'1 Vec<i32>,)>`, for any two lifetimes `'0` and `'1`...
= note: ...but it actually implements `FnOnce<(&Vec<i32>,)>`
error: could not compile `playground` (lib) due to 1 previous error
@rustbot labels +AsyncAwait-Triaged
We discussed this in the async meeting today. We think this is probably related to:
I tried this code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f560d75b3eb32ab6d2e0e51590b0337f
I expected to see this happen: code compiles
Instead, this happened:
error: implementation of std::marker::Send is not general enough
Meta
rustc --version --verbose
:Bug exists on
beta
andnightly
versions also.Backtrace
``` error: implementation of `std::marker::Send` is not general enough --> src/main.rs:63:5 | 63 | tokio::spawn(join_with_cancellation(f, cancellation.clone())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `std::marker::Send` is not general enough | = note: `std::marker::Send` would have to be implemented for the type `&CancellationToken` = note: ...but `std::marker::Send` is actually implemented for the type `&'0 CancellationToken`, for some specific lifetime `'0` ```
I have working example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=19183aa875e3dadf501f3b6bdfb61ce4.
It seems like the reason is combination of
.flatten()
method that hasItem = &'a T
when calling async function inside loop. If you try to remove.await
from the line 33, it compiles. If you try to remove.flatten()
as in second link - it compiles.Also, the error seems kinda random. The pattern is as follows:
But every time I change the code, the compiler shows another
T
type in error hint.