rust-lang / rust-lang/rust

Incorrect rejection of async code on type not living long enough

Open
#143,429 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Compiling the following code:

trait Foo: Sync + Send {
    type A<'a>: Bar + 'a
    where
        Self: 'a;

    fn get<'a>(&'a self) -> Self::A<'a>;
}

trait Bar: Sync + Send {}

fn do_something<'a, T: Foo + 'a>(obj: &'a T) -> impl Future<Output = ()> + Send + 'a {
    async move {
        let x = obj.get();
        tokio::time::sleep(std::time::Duration::from_secs(1)).await;
        drop(x)
    }
}

I would have expected it to compile without errors. Instead it reports

   Compiling playground v0.0.1 (/playground)
error: `T` does not live long enough
  --> src/lib.rs:12:5
   |
12 | /     async move {
13 | |         let x = obj.get();
14 | |         tokio::time::sleep(std::time::Duration::from_secs(1)).await;
15 | |         drop(x)
16 | |     }
   | |_____^

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

Removing either the Send bound on the future returned by do_something, or removing the sleep call, both make the function compile succesfully.

Should anyone else run into this, the following workaround is accepted as by the compiler:

trait Foo: Sync + Send {
    type A<'a>: Bar + 'a
    where
        Self: 'a;

    fn get<'a>(&'a self) -> Self::A<'a>;
}

trait Bar: Sync + Send {}

fn do_something<'a, T: Foo + 'a>(obj: &'a T) -> impl Future<Output = ()> + Send + 'a {
    fn do_inner<'b, U: Bar + 'b>(x: U) -> impl Future<Output = ()> + Send + 'b {
        async move {
            tokio::time::sleep(std::time::Duration::from_secs(1)).await;
            drop(x)
        }
    }
    async move {
        let x = obj.get();
        do_inner(x).await
    }
}

This may be related to #92096, but the code and error are different enough that I am not entirely sure about that.

Meta

This occured on a nightly compiler version from rust playground, and on the latest stable (1.88.0). Rust playground reported the following version info for nightly:

(2025-07-03 da58c051315268a197ce)

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 reproducing the example in Rust Playground on stable 1.88.0 and nightly 2025-07-03, then compare it with the variants that remove the Send bound or sleep call. Read the related discussion in #92096 and determine whether the reported lifetime rejection is expected. Done means the original example compiles correctly and the behavior has regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.