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

Function works but non-capturing closure with identical signature fails with strange error #81326

Open
angelsl opened this issue Jan 24, 2021 · 3 comments
Labels
A-async-await Area: Async & Await A-closures Area: Closures (`|…| { … }`) A-lifetimes Area: Lifetimes / regions AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. WG-async Working group: Async & await

Comments

@angelsl
Copy link
Contributor

angelsl commented Jan 24, 2021

Playground

I'm trying to use the solution suggested for a separate problem here.

The solution works with a coerced function item, but not a closure that is virtually identical.


use std::future::Future;

trait AsyncFn1Arg<'a, Arg: 'a, Ret: 'a> {
    type Fut: Future<Output=Ret> + 'a;
    fn call(&self, arg: Arg) -> Self::Fut;
}

impl<'a, Arg: 'a, Ret: 'a, Fut: Future<Output=Ret> + 'a, F: Fn(Arg) -> Fut> AsyncFn1Arg<'a, Arg, Ret> for F {
    type Fut = Fut;
    fn call(&self, arg: Arg) -> Fut {
        self(arg)
    }
}

async fn f(arg: &i32) {
}

async fn func<F>(f: F)
    where F: for<'a> AsyncFn1Arg<'a, &'a i32, ()>
    {
        let x: i32 = 0;
        f.call(&x).await;
}

fn main() {
    // works
    func(f);
    // fails
    func(|a| f(a));
}

produces

error[E0308]: mismatched types
  --> src/main.rs:29:5
   |
29 |     func(|a| f(a));
   |     ^^^^ lifetime mismatch
   |
   = note: expected type `FnOnce<(&'a i32,)>`
              found type `FnOnce<(&i32,)>`
note: this closure does not fulfill the lifetime requirements
  --> src/main.rs:29:10
   |
29 |     func(|a| f(a));
   |          ^^^^^^^^
note: the lifetime requirement is introduced here
  --> src/main.rs:19:14
   |
19 |     where F: for<'a> AsyncFn1Arg<'a, &'a i32, ()>
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error
@angelsl angelsl added the C-bug Category: This is a bug. label Jan 24, 2021
@angelsl
Copy link
Contributor Author

angelsl commented Jan 24, 2021

This may or may not be the same issue as #70263.

@leo60228
Copy link
Contributor

I've triggered almost this exact problem.

@fmease fmease added A-lifetimes Area: Lifetimes / regions A-closures Area: Closures (`|…| { … }`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-async-await Area: Async & Await T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage-legacy labels Jan 23, 2024
@traviscross
Copy link
Contributor

@rustbot labels +AsyncAwait-Triaged +WG-async

We reviewed this today in WG-async triage.

This is indeed not supported in Rust today. You can't have higher ranked closures that return futures that capture higher ranked arguments.

You're going to want to use async closures for this (currently on nightly).

@rustbot rustbot added AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. WG-async Working group: Async & await labels Feb 26, 2024
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-closures Area: Closures (`|…| { … }`) A-lifetimes Area: Lifetimes / regions AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. WG-async Working group: Async & await
Projects
None yet
Development

No branches or pull requests

6 participants