Skip to content

recursive async function using Box::new results in cycle error #122332

Open
@lcnr

Description

@lcnr
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

Metadata

Metadata

Labels

A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.D-confusingDiagnostics: Confusing error or lint that should be reworked.I-cycleIssue: A query cycle occurred while none was expectedT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-asyncWorking group: Async & await

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions