Skip to content
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

expected signature of `for<'r, 's> fn(&'r mut std::task::Context<'s>) #58639

Closed
kpp opened this issue Feb 22, 2019 · 5 comments
Closed

expected signature of `for<'r, 's> fn(&'r mut std::task::Context<'s>) #58639

kpp opened this issue Feb 22, 2019 · 5 comments

Comments

@kpp
Copy link
Contributor

@kpp kpp commented Feb 22, 2019

MRE:

futures-preview = =0.3.0-alpha.13
rustc -vV = 1.34.0-nightly (146aa60f3 2019-02-18) x86_64-unknown-linux-gnu LLVM 8.0

use futures::stream::Stream;
use core::pin::Pin;

pub async fn next<St>(stream: &mut St) -> Option<St::Item>
    where St: Stream + Unpin,
{
    use futures::future::poll_fn;
    let poll_next = |waker| Pin::new(&mut *stream).poll_next(waker);
    let future_next = poll_fn(poll_next);
    await!(future_next)
}

error[E0308]: mismatched types
  --> src/stream.rs:11:5
   |
11 |     await!(future_next)
   |     ^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected type `for<'r> std::ops::FnMut<(&'r std::task::Waker,)>`
              found type `std::ops::FnMut<(&std::task::Waker,)>`
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error

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

Local fix:

- let poll_next = |waker| Pin::new(&mut *stream).poll_next(waker);
+ let poll_next = |waker: &_| Pin::new(&mut *stream).poll_next(waker);

Is it a bug?

@talchas
Copy link

@talchas talchas commented Feb 26, 2019

This is typical closure inference not inferring a for<'a> when being assigned to a variable rather than being directly passed to a function.

@kpp
Copy link
Contributor Author

@kpp kpp commented Feb 26, 2019

You are right. This one worked:

-    let poll_next = |waker: &_| Pin::new(&mut *stream).poll_next(waker);
-    let future_next = poll_fn(poll_next);
+    let future_next = poll_fn(|waker| Pin::new(&mut *stream).poll_next(waker));

Still is it a bug or a normal behavior?

@talchas
Copy link

@talchas talchas commented Feb 26, 2019

I'd say it's at most "feature request", and I'm sure there's an issue in here since around 1.0 for it :P

@nikomatsakis
Copy link
Contributor

@nikomatsakis nikomatsakis commented Mar 5, 2019

Er, I'm going to close this issue, since it's (at least at present) expected behavior, and it's also not particularly related to async-await.

@kpp kpp changed the title async/await: expected type for<'r> std::ops::FnMut<(&'r std::task::Waker,)> expected signature of `for<'r, 's> fn(&'r mut std::task::Context<'s>) Apr 27, 2019
@kpp
Copy link
Contributor Author

@kpp kpp commented Apr 27, 2019

Futures API moved from &Waker to &mut Context, so if previously this code worked, the new one fails to inference the type of context:

executor::block_on(async {
            let read_line = |_context| -> Poll<String> {
                Poll::Ready("Hello, World!".into())
            };

            let read_future = futures::future::poll_fn(read_line);
            assert_eq!(await!(read_future), "Hello, World!".to_owned());
        });

With error:

error[E0631]: type mismatch in closure arguments
   --> src/future.rs:298:31
    |
294 |             let mut read_line = |_context| -> Poll<String> {
    |                                 -------------------------- found signature of `fn(_) -> _`
...
298 |             let read_future = futures::future::poll_fn(read_line);
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^ expected signature of `for<'r, 's> fn(&'r mut std::task::Context<'s>) -> _`
    |
    = note: required by `futures_util::future::poll_fn::poll_fn`

This one does not help either:

- let read_line = |_context| -> Poll<String> {
+ let read_line = |_context: &mut _| -> Poll<String> {

So I have to explicitly set the type of the arg:

- let read_line = |_context| -> Poll<String> {
+ let read_line = |_context: &mut Context| -> Poll<String> {

since it's (at least at present) expected behavior

Is it an expected behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants