rust-lang / rust-lang/rust

Migrating from `Fn -> impl Future` to `AsyncFn` breaks Send

Open
#137,698 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-async-await C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the two Rust Playground examples linked in the issue and compare the FnMut and AsyncFnMut bounds in call_callback and Callback. Focus on the assert_is_send failure and the higher-ranked AsyncFnMut implementation; done means explaining or correcting the lifetime and Send behavior, with the reported examples continuing to compile as intended.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.