rust-lang / rust-lang/rust

recursive async function using `Box::new` results in cycle error

Open
#122,332 1 comment 0 reactions 1 assignee View on GitHub

@compiler-errors is already working on this.

Since Mar 25, 2024.

A-async-await A-diagnostics AsyncAwait-Triaged D-confusing I-cycle T-compiler WG-async
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

async fn recur(n : u32) -> u32 {
   match n {
       0 | 1 => 1,
       _ => n + Box::new(recur(n-1)).await // using `Box::pin` compiles
   }
}

this results in

error: cannot check whether the hidden type of opaque type satisfies auto traits
 --> src/lib.rs:4:38
  |
4 |        _ => n + Box::new(recur(n-1)).await // using `Box::pin` compiles
  |                                      ^^^^^
  |
note: opaque type is declared here
 --> src/lib.rs:1:1
  |
1 | async fn recur(n : u32) -> u32 {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: this item depends on auto traits of the hidden type, but may also be registering the hidden type. This is not supported right now. You can try moving the opaque type and the item that actually registers a hidden type into a new submodule
 --> src/lib.rs:1:10
  |
1 | async fn recur(n : u32) -> u32 {
  |          ^^^^^
  = note: required for `Box<impl Future<Output = u32>>` to implement `Future`
  = note: required for `Box<impl Future<Output = u32>>` to implement `IntoFuture`

error[E0391]: cycle detected when computing type of opaque `recur::{opaque#0}`
 --> src/lib.rs:1:1
  |
1 | async fn recur(n : u32) -> u32 {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: ...which requires type-checking `recur`...
 --> src/lib.rs:1:1
  |
1 | async fn recur(n : u32) -> u32 {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: ...which again requires computing type of opaque `recur::{opaque#0}`, completing the cycle
note: cycle used when computing type of `recur::{opaque#0}`
 --> src/lib.rs:1:1
  |
1 | async fn recur(n : u32) -> u32 {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

We should instead mention that the async function does not implement Unpin, causing Box<opaque> to not implement IntoFuture.
cc @oli-obk this error should get significantly improved by #122192

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.