Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when implementing async method of another trait from another trait #116984

Open
lohvht opened this issue Oct 20, 2023 · 2 comments
Open

Error when implementing async method of another trait from another trait #116984

lohvht opened this issue Oct 20, 2023 · 2 comments
Assignees
Labels
F-async_fn_in_trait Static async fn in traits fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@lohvht
Copy link

lohvht commented Oct 20, 2023

I tried this code:

trait TraitOne {
    async fn m3<B, W>(&self, _w: &mut W, _b: B) -> Result<(), ()>;
}

trait TraitTwo: TraitOne {
    async fn m4(&self, a: u32) -> Result<(), ()>;
    async fn m5(&self, b: String) -> Result<(), ()>;
}

impl<T: TraitTwo> TraitOne for T {
    async fn m3<B, W>(&self, _w: &mut W, _b: B) -> Result<(), ()> {
        Ok(())
    }
}
fn main() {
    println!("hello world");
}

I expected to see this happen: the above should be able to compile as the sync version of that code can also be compiled (i.e. removing all async keywords from the example above)

Instead, this happened: it gave the following error below, where the signatures match exactly but compilation still gave an error.

❯ RUST_BACKTRACE=1 cargo run
   Compiling async-trait-err v0.1.0 (/home/vloh/projects/test-async-trait)
error[E0053]: method `m3` has an incompatible type for trait
  --> src/main.rs:11:5
   |
11 |     async fn m3<B, W>(&self, _w: &mut W, _b: B) -> Result<(), ()> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected associated type, found future
   |
note: type in trait
  --> src/main.rs:2:5
   |
2  |     async fn m3<B, W>(&self, _w: &mut W, _b: B) -> Result<(), ()>;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: expected signature `fn(&T, &mut W, _) -> impl Future<Output = Result<(), ()>>`
              found signature `fn(&T, &mut W, _) -> impl Future<Output = Result<(), ()>>`

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

Meta

rustc --version --verbose:

rustc 1.75.0-nightly (4578435e1 2023-10-19)
binary: rustc
commit-hash: 4578435e1695863d921c7763d5a0add98f8e3869
commit-date: 2023-10-19
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.3
Backtrace

<backtrace>

@lohvht lohvht added the C-bug Category: This is a bug. label Oct 20, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 20, 2023
@compiler-errors
Copy link
Member

I'm pretty certain that this is a duplicate of #112626, and is unfortunate consequence of how the old trait solver works. I'll see if there's a quick fix we can enact here, but I'm somewhat doubtful 😓

@compiler-errors compiler-errors added F-async_fn_in_trait Static async fn in traits T-types Relevant to the types team, which will review and decide on the PR/issue. fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 20, 2023
@compiler-errors compiler-errors self-assigned this Oct 20, 2023
@lohvht
Copy link
Author

lohvht commented Oct 23, 2023

Thanks for getting back to me, turns out I was also overthinking my implementation and I just wanted a blanket implementation of TraitOne over TraitTwo and I didn't really need TraitTwo to have a circular impl as well (i.e. TraitTwo : TraitOne).

I still think that the diagnostic returned wasn't of help though in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-async_fn_in_trait Static async fn in traits fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants