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
Assignees
Labels
A-async-await Area: Async & Await A-inference Area: Type inference A-typesystem Area: The type system AsyncAwait-Polish Async-await issues that are part of the "polish" area T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kpp
Copy link
Contributor

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?

@Centril Centril added A-typesystem Area: The type system A-inference Area: Type inference A-async-await Area: Async & Await labels Feb 22, 2019
@talchas
Copy link

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 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 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 nikomatsakis added the AsyncAwait-Polish Async-await issues that are part of the "polish" area label Mar 5, 2019
@nikomatsakis nikomatsakis self-assigned this Mar 5, 2019
@nikomatsakis nikomatsakis added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 5, 2019
@nikomatsakis
Copy link
Contributor

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 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
Labels
A-async-await Area: Async & Await A-inference Area: Type inference A-typesystem Area: The type system AsyncAwait-Polish Async-await issues that are part of the "polish" area T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants