Skip to content

Incorrect error when polling non-pinned future from future wrapper #125661

Closed
@Urhengulas

Description

@Urhengulas

Code

When polling future from a wrapping future, the error message when forgetting to pin the wrapped future is wrong. Take this as an example (playground):

use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

struct FutureWrapper<F> {
    fut: F,
}

impl<F> Future for FutureWrapper<F>
where
    F: Future,
{
    type Output = F::Output;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let res = self.fut.poll(cx);
        res
    }
}

Current output

The error is wrong because of the double let: let res = let mut pinned = std::pin::pin!(self.fut);.

error[E0599]: no method named `poll` found for type parameter `F` in the current scope
  --> src/lib.rs:18:28
   |
11 | impl<F> Future for FutureWrapper<F>
   |      - method `poll` not found for this type parameter
...
18 |         let res = self.fut.poll(cx);
   |                            ^^^^ method not found in `F`
   |
help: consider pinning the expression
   |
18 ~         let res = let mut pinned = std::pin::pin!(self.fut);
19 ~         pinned.as_mut().poll(cx);
   |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (lib) due to 1 previous error

Desired output

error[E0599]: no method named `poll` found for type parameter `F` in the current scope
  --> src/lib.rs:18:28
   |
11 | impl<F> Future for FutureWrapper<F>
   |      - method `poll` not found for this type parameter
...
18 |         let res = self.fut.poll(cx);
   |                            ^^^^ method not found in `F`
   |
help: consider pinning the expression
   |
18 ~         let mut pinned = std::pin::pin!(self.fut);
19 ~         let res = pinned.as_mut().poll(cx);
   |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (lib) due to 1 previous error

Rationale and extra context

No response

Other cases

No response

Rust Version

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2

Anything else?

Credit to @skade for discovering the bug.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions