Skip to content

rustc doesn't infer that the future of a recursive async fn is Send. #134101

Open
@jimblandy

Description

@jimblandy

(Note: this bug is not about recursion in asynchronous functions being limited because futures of async functions cannot contain a copy of themselves.)

I would expect this code to compile successfully:

use std::future::Future;

pub async fn recur(depth: usize) {
    if depth == 0 {
        return;
    }
    spawn(recur(depth - 1));
}

pub fn spawn(_future: impl Future + Send + 'static) { }

Instead, rustc complains:

error[E0283]: type annotations needed: cannot satisfy `impl Future<Output = ()>: Send`
  --> recursive/src/lib.rs:7:5
   |
7  |     spawn(recur(depth - 1));
   |     ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: cannot satisfy `impl Future<Output = ()>: Send`
note: required by a bound in `spawn`
  --> recursive/src/lib.rs:10:37
   |
10 | pub fn spawn(_future: impl Future + Send + 'static) { }
   |                                     ^^^^ required by this bound in `spawn`

For more information about this error, try `rustc --explain E0283`.
error: could not compile `recursive` (lib) due to 1 previous error

The program compiles fine if I pass RUSTFLAGS=-Znext-solver to nightly rustc. I wasn't able to find any existing issues that seemed similar.

Meta

rustc +nightly --version --verbose:

rustc 1.85.0-nightly (9c707a8b7 2024-12-07)
binary: rustc
commit-hash: 9c707a8b769523bb6768bf58e74fa2c39cc24844
commit-date: 2024-12-07
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions