Skip to content

Unclear "higher-ranked lifetime error" error #115525

Open
@DCNick3

Description

Code

main.rs:

use tokio::io::{self, AsyncWriteExt};

pub async fn get_by_id<I, S>(ids: I) -> io::Result<()>
where
    I: IntoIterator<Item = S>,
    S: Into<String>,
{
    for s in ids {
        let s = s.into();
        let mut stdout = io::stdout();
        stdout.write_all(s.as_bytes()).await?;
    }
    
    Ok(())
}

async fn beba() -> io::Result<()> {
    let audios = vec![(1, 2)];
    let _audios = get_by_id(
        audios
            .iter()
            .map(|(a, b)| format!("{}_{}", a, b)),
    )
    .await?;

    Ok(())
}

trait FnTrait {}

impl<F, Fut> FnTrait for F
where
    F: Fn() -> Fut + Send + Sync + 'static,
    Fut: std::future::Future<Output = io::Result<()>> + Send + 'static,
{}

fn check_fn<F: FnTrait>(_f: F) {}

#[tokio::main]
async fn main() -> io::Result<()> {
    check_fn(beba);

    Ok(())
}

Cargo.toml:

[package]
name = "hrle-repro"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { version = "1.32.0", features = ["full"] }

Current output

error: higher-ranked lifetime error
  --> src/main.rs:41:5
   |
41 |     check_fn(beba);
   |     ^^^^^^^^^^^^^^
   |
   = note: could not prove `fn() -> impl Future<Output = Result<(), std::io::Error>> {beba}: FnTrait`

error: could not compile `hrle-repro` (bin "hrle-repro") due to previous error

Desired output

I am not sure of the exact wording, but it should explain that the failure is due to the Future returned from the function not implementing Send and Sync

Rationale and extra context

Without explanation of why does the passed function not implement the required trait, it is not immediately clear what the issue is.

Other cases

The error output is the same on stable and nightly

Anything else?

Originally minimized from this code using reqwest and teloxide

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-lifetimesArea: Lifetimes / regionsD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable ExampleT-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