rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.21k stars 12.56k forks source link

Mysterious "higher-ranked lifetime error" with async fn in trait and return-type notation #110963

Open compiler-errors opened 1 year ago

compiler-errors commented 1 year ago

I tried this code:

#![feature(return_type_notation)]
#![feature(async_fn_in_trait)]

trait HealthCheck {
    async fn check<'a: 'a>(&'a mut self) -> bool;
}

async fn do_health_check_par<HC>(hc: HC)
where
    HC: HealthCheck<check(): Send> + Send + 'static,
{
    tokio::task::spawn(async move {
        let mut hc = hc;
        if !hc.check().await {
            log_health_check_failure().await;
        }
    });
}

async fn log_health_check_failure() {}

I expected to see this happen: it works

Instead, this happened:

error: higher-ranked lifetime error
  --> src/lib.rs:12:5
   |
12 | /     tokio::task::spawn(async move {
13 | |         let mut hc = hc;
14 | |         if !hc.check().await {
15 | |             log_health_check_failure().await;
16 | |         }
17 | |     });
   | |______^
   |
   = note: could not prove `[async block@src/lib.rs:12:24: 17:6]: Send`
compiler-errors commented 1 year ago

Actually I forgot to click submit on this issue and I think I solved it in the mean time...

edit: didnt actually solve it completely, it still exists for early-bound lifetimes

GlenDC commented 1 year ago

What’s the status on this? Can I help progress this? With some mentoring I do not mind volunteering dev time to help moving https://github.com/rust-lang/rust/labels/F-async_fn_in_trait further and getting it to hopefully something stable this year.

compiler-errors commented 1 year ago

The fix for this is not clear, possibly related to a far more general problem with async (#110338), and probably not mentorable unfortunately :(

This issue is not blocking async fn in trait stabilization, though.

GlenDC commented 1 year ago

Ok. Does seem like an easy issue to run into for which I do not see a workaround. But I’ll trust you at your word. Thx!