Skip to content

Compile error: static lifetime not satisfied but it is #86172

Open

Description

I tried this code:

use std::{
    future::Future,
    marker::PhantomData,
    {pin::Pin, sync::Arc},
};

fn validate_signature() -> impl Future<Output = ()> + Send {
    let certificate_store = Cache::<CertificateRetriever>::new();
    let res = Box::pin(async move {
        certificate_store.get().await;
    });
    fn foo<T: 'static>(_c: &T) {}
    res
}

type CertificateRetriever =
    Arc<dyn Retrieve<Future = Pin<Box<dyn Future<Output = ()> + Send + 'static>>>>;

trait Retrieve: Send + Sync {
    type Future: Future<Output = ()> + Send + 'static;

    fn get(&self) -> Self::Future;
}

impl<R: Retrieve + ?Sized> Retrieve for Arc<R> {
    type Future = R::Future;

    fn get(&self) -> Self::Future {
        todo!()
    }
}

struct Cache<R: Retrieve> {
    marker: PhantomData<R>,
}

impl<R> Cache<R>
where
    R: Retrieve + 'static,
{
    fn new() -> Self {
        todo!()
    }
    async fn get(&self) {
        self.get_value().await
    }

    fn get_value(&self) -> R::Future {
        todo!()
    }
}

Playground

I expected to see this to compile fine, minor changes to the code cause it to compile.

Instead, I get this error:

error[E0477]: the type `Pin<Box<dyn Future<Output = ()> + Send>>` does not fulfill the required lifetime
 --> src/lib.rs:7:28
  |
7 | fn validate_signature() -> impl Future<Output = ()> + Send {
  |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: type must satisfy the static lifetime

error: aborting due to previous error

This may be a duplicate of #80052 but as it involves no associated constants I'm keeping it separate for now.
Compiled with rustc 1.52.1

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-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-asyncWorking group: Async & awaitregression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions