Skip to content

GAT lifetime error is unhelpful #91883

@vorot93

Description

@vorot93

Given the following code:

#![feature(generic_associated_types)] use std::fmt::Debug; use std::marker::PhantomData; #[derive(Debug)] pub struct TransactionImpl<'db> { _marker: PhantomData<&'db ()>, } #[derive(Debug)] pub struct CursorImpl<'txn> { _marker: PhantomData<&'txn ()>, } pub trait Cursor<'txn> {} pub trait Transaction<'db>: Send + Sync + Debug + Sized { type Cursor<'tx>: Cursor<'tx> where 'db: 'tx, Self: 'tx; fn cursor<'tx>(&'tx self) -> Result<Self::Cursor<'tx>, ()> where 'db: 'tx; } impl<'tx> Cursor<'tx> for CursorImpl<'tx> {} impl<'db> Transaction<'db> for TransactionImpl<'db> { type Cursor<'tx> = CursorImpl<'tx>; fn cursor<'tx>(&'tx self) -> Result<Self::Cursor<'tx>, ()> where 'db: 'tx, { loop {} } }

The current output is:

error[E0478]: lifetime bound not satisfied --> src/lib.rs:32:24 | 32 | type Cursor<'tx> = CursorImpl<'tx>; | ^^^^^^^^^^^^^^^ | note: lifetime parameter instantiated with the lifetime `'db` as defined here --> src/lib.rs:31:6 | 31 | impl<'db> Transaction<'db> for TransactionImpl<'db> { | ^^^ note: but lifetime parameter must outlive the lifetime `'tx` as defined here --> src/lib.rs:32:17 | 32 | type Cursor<'tx> = CursorImpl<'tx>; 

Error is fixed by adding a where clause to associated type in impl, which is not immediately clear:

impl<'db> Transaction<'db> for TransactionImpl<'db> { type Cursor<'tx> where 'db: 'tx, = CursorImpl<'tx>; }

Metadata

Metadata

Assignees

Labels

A-GATsArea: Generic associated types (GATs)A-diagnosticsArea: Messages for errors, warnings, and lintsD-papercutDiagnostics: An error or lint that needs small tweaks.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsGATs-triagedIssues using the `generic_associated_types` feature that have been triagedT-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