Skip to content

Cannot implement an associated async trait #135239

@HaoboGu

Description

@HaoboGu

I tried this code:

trait MyTraitA {
    fn run_a(&mut self) -> impl futures::Future<Output = ()>;
}

trait MyTraitB: MyTraitA {
    fn run_b(&mut self) -> impl futures::Future<Output = ()>;
}

impl<T: MyTraitB> MyTraitA for T {
    async fn run_a(&mut self) {
        self.run_b().await;
    }
}

I expected to see this happen: I want every struct that implements MyTraitB automatically implements MyTraitA.

Instead, this happened: I got the following error message:

   Compiling playground v0.0.1 (/playground)
error[E0053]: method `run_a` has an incompatible type for trait
  --> src/main.rs:10:5
   |
10 |     async fn run_a(&mut self) {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^ expected associated type, found future
   |
note: type in trait
  --> src/main.rs:2:28
   |
2  |     fn run_a(&mut self) -> impl futures::Future<Output = ()>;
   |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: expected signature `fn(&mut _) -> impl futures::Future<Output = ()>`
              found signature `fn(&mut _) -> impl futures::Future<Output = ()>`

For more information about this error, try `rustc --explain E0053`.
error: could not compile `playground` (bin "playground") due to 1 previous error

The error message is not clear, and the expected & found signature are same. If it's not correct, I'd expect a hint and a suggestion that tell me why I can't do this and how can I achieve it.

Meta

rustc --version --verbose:

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: aarch64-apple-darwin
release: 1.83.0
LLVM version: 19.1.1
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions