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

Lifetime in GAT must outlive trait parameters #91036

Closed
ilslv opened this issue Nov 19, 2021 · 5 comments · Fixed by #91849
Closed

Lifetime in GAT must outlive trait parameters #91036

ilslv opened this issue Nov 19, 2021 · 5 comments · Fixed by #91849
Labels
A-GATs Area: Generic associated types (GATs) A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ilslv
Copy link

ilslv commented Nov 19, 2021

Related to #89970

Code

#![feature(generic_associated_types)]

trait T<'ctx> {
    type Fut<'out> 
    where 
        'ctx: 'out, 
        Self: 'out;
    
    fn test<'me, 'out>(&'me self, ctx: &'ctx i32) -> Self::Fut<'out>
    where
        'me: 'out,
        'ctx: 'out;
}

Basically I want to desugar async fn. For this I need to describe Fut GAT that lives the smallest of the 'me and 'ctx, but I get compiler error with requirement that doesn't really make sense to me.

error: Missing required bounds on Fut
 --> src/lib.rs:4:5
  |
4 | /     type Fut<'out> 
5 | |     where 
6 | |         'ctx: 'out, 
7 | |         Self: 'out;
  | |                   ^ help: add the required where clauses: `, 'out: 'ctx`
  | |___________________|
  | 

error: could not compile `playground` due to previous error

'out cant't outlive 'ctx.

Meta

fails on 1.58.0-nightly (2021-11-18 cc946fcd326f7d85d4af) Playground

@vandenheuvel
Copy link
Contributor

Do you need a GAT here? Lifetime 'out outlives everything else. I would expect a GAT lifetime to live shorter than the trait.

@ilslv
Copy link
Author

ilslv commented Nov 19, 2021

@vandenheuvel

Lifetime 'out outlives everything else

Not really. Lifetime 'out live the shortest of 'me and 'ctx lifetimes. This is done by 'me: 'out, 'ctx: 'out which means: both 'me and 'ctx outlive 'out => 'out should be shortest of 'me and 'ctx

See #56238 (comment) for more info

@jackh726
Copy link
Member

Yeah, this looks weird

@jackh726 jackh726 added the F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs label Nov 19, 2021
@vandenheuvel
Copy link
Contributor

Thanks @ilslv, turns out it is possible to code rust for quite a while without properly understanding lifetimes.

@inquisitivecrystal inquisitivecrystal added A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 20, 2021
@vorot93
Copy link

vorot93 commented Nov 27, 2021

Having the same issue with the latest nightlies:

pub trait Transaction<'db> {
    type Cursor<'tx, T>: Cursor<'tx, T>
    where
        'db: 'tx,
        Self: 'tx,
        T: Table + 'db + 'tx;
}
add the required where clauses: `, 'tx: 'db`

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 13, 2021
…r=nikomatsakis

GATs outlives lint: Try to prove bounds

Fixes rust-lang#91036
Fixes rust-lang#90888
Fixes rust-lang#91348 (better error + documentation to be added to linked issue)

Instead of checking for bounds directly, try to prove them in the associated type environment.

Also, add a bit of extra information to the error, including a link to the relevant discussion issue (rust-lang#87479). That should be edited to include a brief summary of the current state of the outlives lint, including a brief background. It also might or might not be worth it to bump this to a full error code at some point.

r? `@nikomatsakis`
@bors bors closed this as completed in 8487833 Dec 13, 2021
@fmease fmease added the A-GATs Area: Generic associated types (GATs) label Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-GATs Area: Generic associated types (GATs) A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs 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.

6 participants