Skip to content

Adding Self: Sized alters AsyncFnOnce bounds #137781

Closed
@ilslv

Description

@ilslv

I tried this code:

use std::future::Future;

pub trait Run<Ctx> {
    fn run(self, ctx: &mut Ctx) -> impl Future;
}

impl<Ctx, F, A> Run<Ctx> for (F, A)
where
    F: AsyncFnOnce(&mut Ctx, A),
    Self: Sized,
{
    fn run(self, ctx: &mut Ctx) -> impl Future {
        let (f, a) = self;
        (f)(ctx, a)
    }
}

And got error

   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `F: AsyncFnOnce(F, A)` is not satisfied
  --> src/lib.rs:14:9
   |
14 |         (f)(ctx, a)
   |         ^^^^^^^^^^^ the trait `AsyncFnOnce(F, A)` is not implemented for `F`

error[E0308]: mismatched types
  --> src/lib.rs:14:13
   |
7  | impl<Ctx, F, A> Run<Ctx> for (F, A)
   |           - expected this type parameter
...
14 |         (f)(ctx, a)
   |         --- ^^^ expected type parameter `F`, found `&mut Ctx`
   |         |
   |         arguments to this function are incorrect
   |
   = note: expected type parameter `F`
           found mutable reference `&mut Ctx`
note: method defined here
  --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/async_function.rs:60:27
   |
60 |     extern "rust-call" fn async_call_once(self, args: Args) -> Self::CallOnceFuture;
   |                           ^^^^^^^^^^^^^^^

error[E0277]: the trait bound `F: AsyncFnOnce(F, A)` is not satisfied
  --> src/lib.rs:12:36
   |
12 |     fn run(self, ctx: &mut Ctx) -> impl Future {
   |                                    ^^^^^^^^^^^ the trait `AsyncFnOnce(F, A)` is not implemented for `F`

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 3 previous errors

While removing Self: Sized bound solves the problem:

  use std::future::Future;
  
  pub trait Run<Ctx> {
      fn run(self, ctx: &mut Ctx) -> impl Future;
  }
  
  impl<Ctx, F, A> Run<Ctx> for (F, A)
  where
      F: AsyncFnOnce(&mut Ctx, A),
-      Self: Sized,
  {
      fn run(self, ctx: &mut Ctx) -> impl Future {
          let (f, a) = self;
          (f)(ctx, a)
      }
  }

Metadata

Metadata

Labels

A-async-awaitArea: Async & AwaitA-async-closures`async || {}`C-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions