Skip to content

Confusing error: Immutable reference to future is not a future #87211

@Darksonn

Description

@Darksonn

Given the following code:

async fn my_async_fn() {}

async fn foo() {
    tokio::select! {
        _ = &my_async_fn() => {},
    }
}

playground

The current output is:

error[E0599]: no method named `poll` found for struct `Pin<&mut &impl Future>` in the current scope
 --> src/lib.rs:5:5
  |
5 | /     tokio::select! {
6 | |         _ = &my_async_fn() => {}
7 | |     }
  | |_____^ method not found in `Pin<&mut &impl Future>`
  |
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

Given the following code:

async fn my_async_fn() {}

async fn foo() {
    let fut = &my_async_fn();
    tokio::pin!(fut);
    
    fut.await;
}

playground

The current output is:

error[E0277]: `&impl Future` is not a future
 --> src/lib.rs:8:5
  |
8 |     fut.await;
  |     ^^^^^^^^^ `&impl Future` is not a future
  |
  = help: the trait `Future` is not implemented for `&impl Future`
  = note: required because of the requirements on the impl of `Future` for `Pin<&mut &impl Future>`
  = note: required by `poll`

Given the following code:

async fn my_async_fn() {}

async fn foo() {
    let fut = &my_async_fn();
    
    fut.await;
}

playground

The current output is:

error[E0277]: `&impl Future` is not a future
 --> src/lib.rs:7:5
  |
7 |     fut.await;
  |     ^^^^^^^^^ `&impl Future` is not a future
  |
  = help: the trait `Future` is not implemented for `&impl Future`
  = note: required by `poll`

In all three cases, it would be very helpful if the compiler could explicitly call out that an immutable reference to a future is not a future. I see this relatively often in the Tokio discord where people put some extra ampersands in random places and get really confused when the error says "impl future is not future". The ampersand in the error is not enough to clue them in to what the issue is. Especially for the first case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.E-help-wantedCall for participation: Help is requested to fix this issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions