Skip to content

Migrating from Fn -> impl Future to AsyncFn breaks Send #137698

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

Open
smessmer opened this issue Feb 27, 2025 · 2 comments
Open

Migrating from Fn -> impl Future to AsyncFn breaks Send #137698

smessmer opened this issue Feb 27, 2025 · 2 comments
Labels
A-async-await Area: Async & Await C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@smessmer
Copy link

Posted here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=48d50965069b89953a04b10aff63c9cb

I want to convert some code to the new AsyncFn traits, and expected AsyncFn to be a drop-in replacement for code that previously used impl Fn () -> F with F: Future, but it doesn't seem to be and I am running into lifetime and/or Send trait issues.

Minimalized toy example, pre-conversion, this code works fine:

use std::marker::PhantomData;

pub async fn call_callback<'a, F, CallbackFn>(
    arg: &'a i32,
    callback: Callback<'a, F, CallbackFn>,
)
where
    F: Future,
    CallbackFn: FnMut(&'a i32) -> F,
{
    if let Callback::SomeCallback{mut func} = callback {
        func(arg).await;
    }
}

pub enum Callback<'a, F, CallbackFn>
where
    F: Future,
    CallbackFn: FnMut(&'a i32) -> F,
{
    NoCallback {
        _p: PhantomData<&'a i32>,
    },
    SomeCallback {
        func: CallbackFn,
    },
}

impl<'a> Callback<'a, std::future::Ready<()>, fn(&'a i32) -> std::future::Ready<()>>
{
    pub fn no_callback() -> Self {
        Self::NoCallback {
            _p: PhantomData,
        }
    }
}

fn assert_is_send<V: Send>(_v: V) {}

async fn test() {
    let task = async {
        let callback = Callback::no_callback();
        call_callback(&0, callback).await;
    };

    assert_is_send(task);
}

playground

and here the version converting it to AsyncFn:

use std::marker::PhantomData;

pub async fn call_callback<'a, CallbackFn>(
    arg: &'a i32,
    callback: Callback<'a, CallbackFn>,
)
where
    CallbackFn: AsyncFnMut(&'a i32),
{
    if let Callback::SomeCallback{mut func} = callback {
        func(arg).await;
    }
}

pub enum Callback<'p, CallbackFn>
where
    CallbackFn: AsyncFnMut(&'p i32),
{
    NoCallback {
        _p: PhantomData<&'p i32>,
    },
    SomeCallback {
        func: CallbackFn,
    },
}

impl<'p> Callback<'p, fn(&'p i32) -> std::future::Ready<()>>
{
    pub fn no_callback() -> Self {
        Self::NoCallback {
            _p: PhantomData,
        }
    }
}

fn assert_is_send<V: Send>(_v: V) {}

async fn test() {
    let task = async {
        let callback = Callback::no_callback();
        call_callback(&0, callback).await;
    };

    assert_is_send(task);
}

playground

The converted code fails with

   Compiling playground v0.0.1 (/playground)
error: implementation of `AsyncFnMut` is not general enough
  --> src/lib.rs:44:5
   |
44 |     assert_is_send(task);
   |     ^^^^^^^^^^^^^^^^^^^^ implementation of `AsyncFnMut` is not general enough
   |
   = note: `fn(&'0 i32) -> std::future::Ready<()>` must implement `AsyncFnMut<(&'1 i32,)>`, for any two lifetimes `'0` and `'1`...
   = note: ...but it actually implements `AsyncFnMut<(&i32,)>`

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

Interestingly, using a higher ranked type bound will fix it:

impl<'p> Callback<'p, for <'a> fn(&'a i32) -> std::future::Ready<()>>

But I do not understand why the converted version requires that and why the previous version does not require it (also note that in my particular case this solution doesn't work because in the non-minimized example, I actually don't have a lifetime but a type carrying a lifetime there and for<T> isn't allowed in stable Rust yet).

Another interesting point is that this doesn't actually seem to be a lifetime issue. When I remove the call to assert_is_send, everything works. So it seems the lifetimes work just fine, just the generated Future isn't Send for some reason.

Any idea what's going on and why the compiler behaves in this way here?

Meta

Rust 1.85
reproducible with rust playground links above

@smessmer smessmer added the C-bug Category: This is a bug. label Feb 27, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 27, 2025
@jieyouxu jieyouxu added A-async-await Area: Async & Await T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 27, 2025
@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 1, 2025
@Noratrieb
Copy link
Member

cc @compiler-errors you may know

@compiler-errors
Copy link
Member

Probably just a duplicate of #134997

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants