rust-lang / rust-lang/rust

Higher ranked lifetime error when checking auto traits of async functions containing calls to async closures which capture a local

Open
#134,997 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I tried this code:

#![feature(async_fn_traits)]

use std::ops::AsyncFn;

// this function is important, inlining it breaks the poc.
// We also tried replacing it with `identity` but the fact that this is an
// async function calling the closure inside is also relevant.
async fn do_not_inline(f: impl AsyncFn()) {
    f().await;
}

fn foo() -> impl Send {
    async move {
        // just replacing this with unit, leaves the error with `Send` but fixes the
        // "higher-ranked lifetime error"
        let s = &();

        do_not_inline(async || {
            &s;
        })
        .await;
    }
}

I expected to see this happen: compilation succeeds
Instead, this happened:

error: implementation of `Send` is not general enough
  --> src/lib.rs:13:5
   |
13 | /     async move {
14 | |         // just replacing this with unit, leaves the error with `Send` but fixes the
15 | |         // "higher-ranked lifetime error"
16 | |         let s = &();
...  |
21 | |         .await;
22 | |     }
   | |_____^ implementation of `Send` is not general enough
   |
   = note: `Send` would have to be implemented for the type `&'0 &()`, for any lifetime `'0`...
   = note: ...but `Send` is actually implemented for the type `&'1 &()`, for some specific lifetime `'1`

error: higher-ranked lifetime error
  --> src/lib.rs:13:5
   |
13 | /     async move {
14 | |         // just replacing this with unit, leaves the error with `Send` but fixes the
15 | |         // "higher-ranked lifetime error"
16 | |         let s = &();
...  |
21 | |         .await;
22 | |     }
   | |_____^

error: could not compile `noteslsp` (lib) due to 2 previous errors

try it here: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=ab71ed9cfa6f91320c7a24fe7f2bdca9

note, we got here through a bigger example, the impl Send used to come from the async_trait library where it produced Pin<Box<dyn Future<Output=()> + Send>> but most of that was not relevant for the mcve.

Meta

rustc --version --verbose:

rustc 1.85.0-nightly (d117b7f21 2024-12-31)
binary: rustc
commit-hash: d117b7f211835282b3b177dc64245fff0327c04c
commit-date: 2024-12-31
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6

cc: @compiler-errors

@rustbot label +F-async_closure +T-compiler +A-auto-traits

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 nightly compiler failure from src/lib.rs, focusing on foo, do_not_inline, and the async closure in the linked Playground example. Compare the reported Send and higher-ranked lifetime errors with the expected successful compilation. Done means the minimal example is handled correctly without those errors.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.