Skip to content

Format_args temporary across await point leads to a non-Sync future #182

@dtolnay

Description

@dtolnay

This looks like our version of rust-lang/rust#64856. I expect the same fix will work.

use anyhow::anyhow;
use std::future;

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

fn main() {
    assert_sync(async {
        future::ready(anyhow!("...")).await;
    });
}

This is fine with the current most recent published version of anyhow (1.0.45) but on master due to #180 it fails as follows:

error: future cannot be shared between threads safely
 --> src/main.rs:7:5
  |
7 |     assert_sync(async {
  |     ^^^^^^^^^^^ future created by async block is not `Sync`
  |
  = help: within `impl Future`, the trait `Sync` is not implemented for `core::fmt::Opaque`
note: future is not `Sync` as this value is used across an await
 --> src/main.rs:8:9
  |
8 |         future::ready(anyhow!("...")).await;
  |         ^^^^^^^^^^^^^^--------------^^^^^^^- `$crate::private::format_args!($msg)` is later dropped here
  |         |             |
  |         |             has type `[ArgumentV1<'_>; 0]` which is not `Sync`
  |         await occurs here, with `$crate::private::format_args!($msg)` maybe used later
note: required by a bound in `assert_sync`
 --> src/main.rs:4:19
  |
4 | fn assert_sync<T: Sync>(_: T) {}
  |                   ^^^^ required by this bound in `assert_sync`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions