Skip to content

Incorrect "implementation of Send is not general enough" error with lifetimed Send impl used in async fn #96865

Open

Description

@SabrinaJewson

I tried this code:

struct Foo<'a, F> {
    func: F,
    data: &'a u32,
}
unsafe impl<'a, F: Send + Callback<&'a u32>> Send for Foo<'a, F> {}

trait Callback<I> {}

struct Implementor<'a>(&'a u32);
impl<'a> Callback<&'a u32> for Implementor<'a> {}

fn assert_send<T: Send>(v: T) -> T { v }

async fn async_function<'a>(data: &'a u32) {
    let data = assert_send(Foo {
        func: Implementor(data),
        data,
    });
    std::future::ready(5).await;
}

fn wrapper(data: &u32) {
    assert_send(async_function(data));
}

I expected to see this happen: No error

Instead, this happened:

error: implementation of `std::marker::Send` is not general enough
  --> src/main.rs:23:5
   |
23 |     assert_send(async_function(data));
   |     ^^^^^^^^^^^ implementation of `std::marker::Send` is not general enough
   |
   = note: `std::marker::Send` would have to be implemented for the type `Foo<'0, Implementor<'1>>`, for any two lifetimes `'0` and `'1`...
   = note: ...but `std::marker::Send` is actually implemented for the type `Foo<'2, Implementor<'_>>`, for some specific lifetime `'2`

Meta

rustc --version --verbose:

rustc 1.60.0 (7737e0b5c 2022-04-04)
binary: rustc
commit-hash: 7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c
commit-date: 2022-04-04
host: x86_64-unknown-linux-gnu
release: 1.60.0
LLVM version: 14.0.0

Also happens with Nightly:

rustc 1.62.0-nightly (bed05e996 2022-05-02)
binary: rustc
commit-hash: bed05e996e37e44b1a3980b84754af621fd3c4ce
commit-date: 2022-05-02
host: x86_64-unknown-linux-gnu
release: 1.62.0-nightly
LLVM version: 14.0.1

Activity

  1. added
    C-bug
    on May 9, 2022
  2. aliemjay commented on May 10, 2022

    @aliemjay
    Contributor

    Minimized: https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=87d7f794697776f806158ef6b9880bdb

    struct Foo<'a>(&'a str);
    unsafe impl Send for Foo<'static> {}
    
    fn assert_send_future() -> impl Send {
        async {
            let data: Foo<'static> = Foo("");
            std::future::ready(()).await;
        }
    }

    @rustbot label A-generators A-async-await T-compiler

  3. added
    A-async-await
    A-coroutines
    T-compiler
    on May 10, 2022
  4. SabrinaJewson commented on May 11, 2022

    @SabrinaJewson
    ContributorAuthor

    Another instance of what I think is the same bug, this time with dyn Traits and associated types: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=6d632d153fdfebd3cb7348a0ec15326a (discovered by @LegionMammal978 on Discord)

    trait Funnel {
        type This;
    }
    impl<T: 'static> Funnel for T {
        type This = T;
    }
    
    async fn bar<T: Funnel>() {
        let _x: T::This = todo!();
        std::future::ready(()).await;
    }
    
    pub fn test() -> impl Send {
        async { bar::<Box<dyn Send>>().await }
    }

    The fix there is to wrap the dyn Trait in another type:

    #[repr(transparent)]
    struct DynSendWrapper(dyn Send);
    
    pub fn test() -> impl Send {
        async { bar::<Box<DynSendWrapper>>().await }
    }
  5. eholk commented on May 23, 2022

    @eholk
    Contributor

    It looks like we are incorrectly replacing regions with inference variables. In generator_interior, it looks like we replace regions with variables in a couple of places for convenience, so maybe we are being sloppy with that? Here are a couple of places that might be relevant:

    1. // Replace all regions inside the generator interior with late bound regions.
      // Note that each region slot in the types gets a new fresh late bound region,
      // which means that none of the regions inside relate to any other, even if
      // typeck had previously found constraints that would cause them to be related.
    2. https://cs.github.com/rust-lang/rust/blob/653463731a7f01f519cf85f444869def27f00395/compiler/rustc_typeck/src/check/generator_interior.rs#L249

    @rustbot label AsyncAwait-Triaged

  6. added
    AsyncAwait-Triaged
    on May 23, 2022
  7. carols10cents commented on Aug 17, 2022

    @carols10cents
    Member

    Is this related to/duplicate of #64552?

  8. moved this to On deck in wg-async workon Dec 8, 2022
  9. emersonford commented on Apr 12, 2023

    @emersonford

    @carols10cents @eholk yeah this is definitely the same issue as #102211. fixing that bug should fix all related issues.

  10. zertyz commented on Aug 19, 2023

    @zertyz

    Please, bump up this bug report.
    All the problematic code presented here still show the same errors in Rust 1.71, 1.3 years after.
    This bug bit me nowadays, preventing my progress on a nice & flexible generic async code using references for zero-copying things around.
    I'll either have to write inneficient code or chase an as-efficient-as-possible workaround for my specific case for hours and hours...

  11. zertyz commented on Aug 21, 2023

    @zertyz

    I ended up using the alternative notation -- described here: #102211 (comment) -- to avoid this type inference compiler bug when GATs are involved.
    Other zero-cost workarounds are possible as well:

    Other workarounds exist in the first link, but they involve paying extra costs for boxing / dynamic dispatching.

  12. 16 remaining items

  13. Kulasko commented on Nov 29, 2026

    @Kulasko

    @rvolosatovs my project has been stalled for about a year due to this bug, your solution has at least solved the problem that I'm hitting after a complete rewrite, I cannot thank you enough!

    Also another interesting tidbit, at least my variation of this bug was fixed by using the "higher ranked assumptions" flag on the nightly compiler some 9-12 months ago. That said, this no longer works, prompting said rewrite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitA-coroutinesAsyncAwait-TriagedC-bugT-compiler

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions