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

GAT: lifetime bound not satisfied #186

Closed
vorot93 opened this issue Dec 14, 2021 · 1 comment
Closed

GAT: lifetime bound not satisfied #186

vorot93 opened this issue Dec 14, 2021 · 1 comment

Comments

@vorot93
Copy link

vorot93 commented Dec 14, 2021

rust-lang/rust#91883 slightly expanded fails:

#![feature(generic_associated_types)]

use async_trait::async_trait;
use std::fmt::Debug;
use std::marker::PhantomData;

#[derive(Debug)]
pub struct TransactionImpl<'db> {
    _marker: PhantomData<&'db ()>,
}

#[derive(Debug)]
pub struct CursorImpl<'tx> {
    _marker: PhantomData<&'tx ()>,
}

#[async_trait]
pub trait Cursor<'tx>: Send {
    async fn seek_exact(&mut self, key: String) -> Result<String, ()>;
}

#[async_trait]
pub trait Transaction<'db>: Send + Sync + Debug + Sized {
    type Cursor<'tx>: Cursor<'tx>
    where
        'db: 'tx,
        Self: 'tx;

    async fn cursor<'tx>(&'tx self) -> Result<Self::Cursor<'tx>, ()>
    where
        'db: 'tx;

    async fn read<'tx>(&'tx self, key: String) -> Result<String, ()>
    where
        'db: 'tx,
    {
        Ok(self.cursor().await?.seek_exact(key).await?)
    }
}

#[async_trait]
impl<'tx> Cursor<'tx> for CursorImpl<'tx> {
    async fn seek_exact(&mut self, key: String) -> Result<String, ()> {
        loop {}
    }
}

#[async_trait]
impl<'db> Transaction<'db> for TransactionImpl<'db> {
    type Cursor<'tx>
    where
        'db: 'tx,
    = CursorImpl<'tx>;

    async fn cursor<'tx>(&'tx self) -> Result<Self::Cursor<'tx>, ()>
    where
        'db: 'tx,
    {
        loop {}
    }
}
error[E0478]: lifetime bound not satisfied
  --> src/lib.rs:36:5
   |
36 | /     {
37 | |         Ok(self.cursor().await?.seek_exact(key).await?)
38 | |     }
   | |_____^
@dtolnay
Copy link
Owner

dtolnay commented Mar 25, 2022

This seems like a compiler bug. It should be providing more guidance than this on what lifetime constraint it has in mind and what extra bound needs to be added where.

I'll close this issue until the GAT implementation in rustc is in better shape. After that, if something causes the new diagnostic from rustc to be misplaced or otherwise hard to interpret specifically due to async-trait, we can reopen an issue.

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

No branches or pull requests

2 participants