Skip to content

Compile success when generic parameter is possibly not "Send", but compile fail if said parameter is in a tuple #280

@david0u0

Description

@david0u0

async_trait ersion: 0.1.83
rustc version: rustc 1.84.0-beta.5 (0857a8e32 2024-12-27)


Below is the minimum code that can reproduce this. With the function "test_success", async trait doesn't care about "S" being Send or not, but when I put it into a tuple (bool, &mut S), suddenly it starts to complain that "S" cannot be send between threads.

use async_trait::async_trait;

#[async_trait]
trait Test<S> {
    async fn test_success(&self, _s: &mut S) {}
    // async fn test_fail(&self, _s: (bool, &mut S)) {}
}

Here's the error message:

   Compiling playground v0.0.1 (/playground)
error[E0277]: `S` cannot be sent between threads safely
 --> src/lib.rs:6:51
  |
6 |     async fn test_fail(&self, _s: (bool, &mut S)) {}
  |                                                   ^^
  |                                                   |
  |                                                   `S` cannot be sent between threads safely
  |                                                   within this `{async block@src/lib.rs:6:51: 6:53}`
  |
  = note: required because it appears within the type `&mut S`
  = note: required because it appears within the type `(bool, &mut S)`
note: required because it's used within this `async` block
 --> src/lib.rs:6:51
  |
6 |     async fn test_fail(&self, _s: (bool, &mut S)) {}
  |                                                   ^^
  = note: required for the cast from `Pin<Box<{async block@src/lib.rs:6:51: 6:53}>>` to `Pin<Box<(dyn Future<Output = ()> + Send + 'async_trait)>>`
help: consider restricting type parameter `S`
  |
4 | trait Test<S: std::marker::Send> {
  |             +++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 1 previous error

I'm not sure what should be the correct behavior, but these two scenario seem logically the same. So I think they should either both compile fail or pass.

Thanks for this amazing crate!

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