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

async fn unmet lifetime constraints produce confusing diagnostics #66168

Open
s97712 opened this issue Nov 6, 2019 · 2 comments
Open

async fn unmet lifetime constraints produce confusing diagnostics #66168

s97712 opened this issue Nov 6, 2019 · 2 comments
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@s97712
Copy link

s97712 commented Nov 6, 2019

use std::future::Future;

struct Foo<'a> {
  pub value: &'a str
}

fn mode_1(_b: Foo<'_>) -> impl Future<Output=()> {
  async {}
}

async fn mode_2(_b: Foo<'_>) {
}


fn test<F, Fut>(_f: F ) 
  where
    Fut: Future<Output=()>,
    F: FnOnce(Foo<'_>) -> Fut,
{
    
}


fn main() {

    // here can compiled
    test(mode_1);
    
    // here can't compiled
    test(mode_2);
}
   Compiling playground v0.0.1 (/playground)
error[E0271]: type mismatch resolving `for<'r> <for<'_> fn(Foo<'_>) -> impl std::future::Future {mode_2} as std::ops::FnOnce<(Foo<'r>,)>>::Output == _`
  --> src/main.rs:29:5
   |
14 | fn test<F, Fut>(_f: F ) 
   |    ----
...
17 |     F: FnOnce(Foo<'_>) -> Fut,
   |                           --- required by this bound in `test`
...
29 |     test(mode_2);
   |     ^^^^ expected bound lifetime parameter, found concrete lifetime

error: aborting due to previous error

For more information about this error, try `rustc --explain E0271`.

rust playground

@Centril Centril added the A-async-await Area: Async & Await label Nov 7, 2019
@estebank
Copy link
Contributor

estebank commented Nov 7, 2019

Note that you can write code that will compile for this, it is just not obvious (note the added explicit lifetime 'r):

fn test<'r, F, Fut>(_f: F) 
  where
    Fut: Future<Output=()> + 'r,
    F: FnOnce(Foo<'r>) -> Fut,

The reason one works and the other one doesn't is because they are not equivalent. mode_2 gets desugared to

fn mode_2(_b: Foo<'_>) -> impl Future<Output=()> + '_ { // note the relaxing of the lifetime constraint to the return type
  async {}
}

which also doesn't compile unless you add a lifetime to test.

I believe there are already quite a few A-async-await tickets about the default behavior of lifetimes needing some work.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 7, 2019
@estebank estebank changed the title async fn bug async fn unmet lifetime constraints produce confusing diagnostics Nov 7, 2019
@nikomatsakis nikomatsakis added the AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. label Nov 12, 2019
@s97712

This comment has been minimized.

@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Feb 2, 2020
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-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants