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 error is unhelpful #91883

Closed
vorot93 opened this issue Dec 14, 2021 · 3 comments · Fixed by #98538
Closed

GAT lifetime error is unhelpful #91883

vorot93 opened this issue Dec 14, 2021 · 3 comments · Fixed by #98538
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. E-needs-test Call 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. GATs GATs-triaged Issues using the `generic_associated_types` feature that have been triaged T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@vorot93
Copy link

vorot93 commented Dec 14, 2021

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>;
}
@vorot93 vorot93 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 14, 2021
@jackh726 jackh726 added the F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs label Dec 14, 2021
@jackh726
Copy link
Member

GATs issue triage: not blocking. Poor diagnostic, but not a miscompile.

@jackh726 jackh726 added the GATs-triaged Issues using the `generic_associated_types` feature that have been triaged label Dec 14, 2021
@BGR360
Copy link
Contributor

BGR360 commented Dec 25, 2021

@rustbot label +D-papercut +D-terse

@rustbot rustbot added D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Dec 25, 2021
@jackh726
Copy link
Member

jackh726 commented Mar 9, 2022

Current output is

help: try copying these clauses from the trait: `where 'db: 'tx, Self: 'tx`

I think that's pretty helpful. Going to mark as needs-test

@jackh726 jackh726 added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Mar 9, 2022
@TaKO8Ki TaKO8Ki self-assigned this Jun 26, 2022
TaKO8Ki added a commit to TaKO8Ki/rust that referenced this issue Jun 26, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 26, 2022
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 26, 2022
…askrgr

Rollup of 11 pull requests

Successful merges:

 - rust-lang#97140 (std: use an event-flag-based thread parker on SOLID)
 - rust-lang#97295 ([rustc_parse] Forbid `let`s in certain places)
 - rust-lang#97743 (make const_err show up in future breakage reports)
 - rust-lang#97908 (Stabilize NonZero* checked operations constness.)
 - rust-lang#98297 (Transform help popup into a pocket menu)
 - rust-lang#98428 (macros: use typed identifiers in diag and subdiag derive)
 - rust-lang#98528 (Respect --color when building rustbuild itself)
 - rust-lang#98535 (Add regression test for generic const in rustdoc)
 - rust-lang#98538 (Add a ui test for issue rust-lang#91883)
 - rust-lang#98540 (Add regression test for rust-lang#87558)
 - rust-lang#98541 (Update `std::alloc::System` doc example code style)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. E-needs-test Call 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. GATs GATs-triaged Issues using the `generic_associated_types` feature that have been triaged T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants