Skip to content

implied lifetimes lead to mismatched type error with seemingly identical types in the msg - async closure edition #108114

Open
@luketpeterson

Description

@luketpeterson

Code

use core::future::{Future};
use anyhow::Error;

struct Session {}

impl Session {
    pub async fn dispatch_request<T, F, Fut>(&self, interpreter: F) -> Result<T, Error>
        where
        F: FnMut(&mut String) -> Fut,
        Fut: Future<Output = Result<T, Error>>,
    {
        let mut interpreter = interpreter;
        let mut stream = "hello".to_string();
        interpreter(&mut stream).await
    }
}

async fn test_interpreter(input_stream: &mut String) -> Result<String, Error> {

    Ok(input_stream.clone())
}

#[tokio::main]
async fn main() {
    let session = Session{};

    session.dispatch_request(test_interpreter).await.unwrap();
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
  --> src/main.rs:27:5
   |
27 |     session.dispatch_request(test_interpreter).await.unwrap();
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected trait `for<'a> <for<'a> fn(&'a mut String) -> impl Future<Output = Result<String, anyhow::Error>> {test_interpreter} as FnOnce<(&'a mut String,)>>`
              found trait `for<'a> <for<'a> fn(&'a mut String) -> impl Future<Output = Result<String, anyhow::Error>> {test_interpreter} as FnOnce<(&'a mut String,)>>`
note: the lifetime requirement is introduced here
  --> src/main.rs:9:34
   |
9  |         F: FnMut(&mut String) -> Fut,
   |                                  ^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

Desired output

Something that makes it more clear what is causing the problem or how to address it.

Rationale and extra context

This thread has some additional context as well as two proposed solutions. https://users.rust-lang.org/t/lifetime-bounds-to-use-for-future-that-isnt-supposed-to-outlive-calling-scope/89277

I understand this situation is one among a set of situations under which the same confusing error might manifest. However this might be a candidate for a special case check.

Thank you.

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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