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

Adding Self: Sized alters AsyncFnOnce bounds #137781

Open
ilslv opened this issue Feb 28, 2025 · 7 comments · May be fixed by #137811
Open

Adding Self: Sized alters AsyncFnOnce bounds #137781

ilslv opened this issue Feb 28, 2025 · 7 comments · May be fixed by #137811
Assignees
Labels
A-async-await Area: Async & Await A-async-closures `async || {}` C-bug Category: This is a bug. 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

@ilslv
Copy link

ilslv commented Feb 28, 2025

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)
      }
  }
@ilslv ilslv added the C-bug Category: This is a bug. label Feb 28, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 28, 2025
@theemathas
Copy link
Contributor

Minimized:

fn run<F, T>(f: F)
where
    F: AsyncFnOnce(i32),
    (T,): Sized,
{
    f(1i32);
}
error[E0277]: the trait bound `F: AsyncFnOnce(T)` is not satisfied
 --> src/lib.rs:6:5
  |
6 |     f(1i32);
  |     ^^^^^^^ the trait `AsyncFnOnce(T)` is not implemented for `F`

error[E0308]: mismatched types
  --> src/lib.rs:6:7
   |
1  | fn run<F, T>(f: F)
   |           - expected this type parameter
...
6  |     f(1i32);
   |     - ^^^^ expected type parameter `T`, found `i32`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected type parameter `T`
                        found type `i32`
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;
   |                           ^^^^^^^^^^^^^^^

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.

@Noratrieb
Copy link
Member

@compiler-errors you probably know what's up

@theemathas
Copy link
Contributor

This compiles fine, so the issue involves specifically the function call syntax sugar

#![feature(async_fn_traits)]
fn run<F, T>(f: F)
where
    F: AsyncFnOnce(i32),
    (T,): Sized,
{
    f.async_call_once((1i32,));
}

@ShoyuVanilla
Copy link
Member

This happens iff the arity of the input type of AsyncFnOnce is equal to the tuple with the Sized bound, like

// This causes error
fn run<F, T>(f: F)
where
    F: AsyncFnOnce(i32, i32),
    (T, T): Sized,
{
    f.async_call_once((1i32, 1i32));
}

// But not this
fn run<F, T>(f: F)
where
    F: AsyncFnOnce(i32, i32),
    (T,): Sized,
{
    f.async_call_once((1i32, 1i32));
}

@ShoyuVanilla
Copy link
Member

In the following code @theemathas minimized:

#![feature(async_fn_traits)]
fn run<F, T>(f: F)
where
    F: AsyncFnOnce(i32),
    (T,): Sized,
{
    f.async_call_once((1i32,));
}

It seems that while rustc_trait_select trying to fulfill obligation Arg: Sized in F: AsyncFnOnce(Arg), it selects (T,) as Arg because the (T,) is : Sized, Tuple 😬

Anyway, the next-gen solver doesn't make such confusion. The above code compiles well with -Znext-solver

@ShoyuVanilla
Copy link
Member

@rustbot label +fixed-by-next-solver

@rustbot rustbot added the fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. label Feb 28, 2025
@compiler-errors compiler-errors self-assigned this Feb 28, 2025
@compiler-errors compiler-errors added A-async-await Area: Async & Await A-async-closures `async || {}` T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 28, 2025
@compiler-errors
Copy link
Member

This is morally equivalent to this failure in the old solver:

trait Foo<T> {
    fn method();
}

fn test<T, U>()
where
    T: Foo<(i32,)>,
    (U,): Sized,
{
    <T as Foo<(_,)>>::method();
}

I can put up a fix for the AsyncFn* built-in fn call syntax tho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await A-async-closures `async || {}` C-bug Category: This is a bug. 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
6 participants