Skip to content

async fn unmet lifetime constraints produce confusing diagnostics #66168

Description

@s97712
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions