rust-lang / rust-lang/impl-trait-utils

async function with default implementation won't compile

Open
#17 7 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
134
Forks
13
PR merge metrics
No merged PRs in 30d

Description

I have a trait such as this:

#[trait_variant::make(Test: Send)]
trait LocalTest {
    async fn foo() -> i32 { 0 }
}

but if fails with an error:

  |
3 | #[trait_variant::make(Test: Send)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `{integer}` is not a future
  |

It looks like the body of the default impl is translated to the Test trait, unchanged.

If I instead I try:

    async fn foo() -> i32 { std::future::ready(0) }

Then it looks like the Test works, but LocalTest fails with a somewhat opposite error:

5 |     async fn foo() -> i32 { std::future::ready(0) }
  |                             ^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found `Ready<{integer}>`

I'm not sure if this is a limitation of this crate, if so I think it should be documented somewhere; or if it is an oversight in the implementation. Or maybe I'm missing something?

Then, just to see what would happen, I tried:

async fn foo() -> i32 { async move { 0 } }

And I got an ICE!

5 |     async fn foo() -> i32 { async move { 0 } }
  |                             ^^^^^^^^^^^^^^^^ expected `i32`, found `async` block
  |
  = note:       expected type `i32`
          found `async` block `{async block@src/main.rs:5:29: 5:45}`

error: internal compiler error: compiler/rustc_mir_build/src/build/mod.rs:655:72: impossible case reached
...

Contributor guide

No contributing guide indexed for this repository

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

Reproduce the minimal trait_variant::make(Test: Send) example and compare the generated LocalTest and Test traits, including the std::future::ready and async-block variants. Determine whether default async method bodies are supported; done means the case either compiles without the ICE or its limitation and workaround are documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.