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

Invalid "future is not Send as this value is used across an await" #115437

Closed
nazar-pc opened this issue Sep 1, 2023 · 1 comment
Closed

Invalid "future is not Send as this value is used across an await" #115437

nazar-pc opened this issue Sep 1, 2023 · 1 comment
Labels
C-bug Category: This is a bug.

Comments

@nazar-pc
Copy link

nazar-pc commented Sep 1, 2023

I tried this code:

use std::future::{pending, Future};
use std::sync::Mutex;

fn foo() -> impl Future<Output = ()> + Send {
    async {
        let sync_mutex = Mutex::new(());

        let m = sync_mutex.lock().unwrap();
        drop(m);
        pending::<()>().await;
    }
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=bdb1d0f24fab8490d5487520cb050c93

I expected to see this happen: Should compile.

Instead, this happened:

error: future cannot be sent between threads safely
  --> src/lib.rs:4:13
   |
4  | fn foo() -> impl Future<Output = ()> + Send {
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ future created by async block is not `Send`
   |
   = help: within `[async block@src/lib.rs:5:5: 11:6]`, the trait `Send` is not implemented for `MutexGuard<'_, ()>`
note: future is not `Send` as this value is used across an await
  --> src/lib.rs:10:25
   |
8  |         let m = sync_mutex.lock().unwrap();
   |             - has type `MutexGuard<'_, ()>` which is not `Send`
9  |         drop(m);
10 |         pending::<()>().await;
   |                         ^^^^^ await occurs here, with `m` maybe used later
11 |     }
   |     - `m` is later dropped here

error: could not compile `playground` (lib) due to previous error

But it makes no sense, m is clearly dropped earlier than that explicitly.

Meta

rustc --version --verbose:

rustc 1.74.0-nightly (2f5df8a94 2023-08-31)
binary: rustc
commit-hash: 2f5df8a94bb3c5fae4e3fcbfc8ef20f1f976cb19
commit-date: 2023-08-31
host: x86_64-unknown-linux-gnu
release: 1.74.0-nightly
LLVM version: 17.0.0

@nazar-pc nazar-pc added the C-bug Category: This is a bug. label Sep 1, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 1, 2023
@nazar-pc
Copy link
Author

nazar-pc commented Sep 1, 2023

After additional search maybe duplicate of #57478?

@nazar-pc nazar-pc closed this as not planned Won't fix, can't repro, duplicate, stale Sep 4, 2023
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants