Skip to content

Explanation for why future is not Send is wrong #68112

Closed
@tmandry

Description

@tmandry

The following code (playground):

fn main() {
    let send_fut = async {
        let non_send_fut = make_non_send_future();
        let _ = non_send_fut.await;
        ready(0).await;
    };
    require_send(send_fut);
}

Gives an error message stating that non_send_fut can live until the end of the scope, and that's why send_fut is not Send:

error: future cannot be sent between threads safely
  --> src/main.rs:18:5
   |
6  | fn require_send(_: impl Send) {}
   |    ------------         ---- required by this bound in `require_send`
...
18 |     require_send(send_fut);
   |     ^^^^^^^^^^^^ future returned by `main` is not `Send`
   |
   = help: the trait `std::marker::Sync` is not implemented for `std::cell::RefCell<i32>`
note: future is not `Send` as this value is used across an await
  --> src/main.rs:16:9
   |
14 |         let non_send_fut = make_non_send_future();
   |             ------------ has type `impl core::future::future::Future`
15 |         let _ = non_send_fut.await;
16 |         ready(0).await;
   |         ^^^^^^^^^^^^^^ await occurs here, with `non_send_fut` maybe used later
17 |     };
   |     - `non_send_fut` is later dropped here

but it doesn't; it's consumed by value when it gets awaited. The problem is we're awaiting a non-Send future inside of send_fut, which has to be Send.

Also, the text

future returned by `main` is not `Send`

is wrong; main doesn't return anything.

Thanks to @kellerb for the reproducer and @JakeEhrlich for originally reporting this.

Metadata

Metadata

Assignees

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-bugCategory: This is a bug.C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions