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

The $:expr case keeps temporaries alive longer than needed #186

Closed
dtolnay opened this issue Nov 4, 2021 · 0 comments · Fixed by #187
Closed

The $:expr case keeps temporaries alive longer than needed #186

dtolnay opened this issue Nov 4, 2021 · 0 comments · Fixed by #187

Comments

@dtolnay
Copy link
Owner

dtolnay commented Nov 4, 2021

This can lead to non-Sync futures for no good reason, if non-Sync temporaries inside an anyhow! expression are live across an await point.

use anyhow::anyhow;
use std::cell::Cell;
use std::future;

fn assert_sync<T: Sync>(_: T) {}

fn message(cell: Cell<&str>) -> &str {
    cell.get()
}

fn main() {
    assert_sync(async {
        future::ready(anyhow!(message(Cell::new("...")))).await;
    });
}
error: future cannot be shared between threads safely
  --> src/main.rs:12:5
   |
12 |     assert_sync(async {
   |     ^^^^^^^^^^^ future created by async block is not `Sync`
   |
   = help: within `impl Future`, the trait `Sync` is not implemented for `Cell<&str>`
note: future is not `Sync` as this value is used across an await
  --> src/main.rs:13:9
   |
13 |         future::ready(anyhow!(message(Cell::new("...")))).await;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ first, await occurs here, with `Cell::new("...")` maybe used later...
note: `Cell::new("...")` is later dropped here
  --> src/main.rs:13:64
   |
13 |         future::ready(anyhow!(message(Cell::new("...")))).await;
   |                                       ----------------         ^
   |                                       |
   |                                       has type `Cell<&str>` which is not `Sync`
note: required by a bound in `assert_sync`
  --> src/main.rs:5:19
   |
5  | fn assert_sync<T: Sync>(_: T) {}
   |                   ^^^^ required by this bound in `assert_sync`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant