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

Lifetimes get replaced with 'static in Self within (return position) impl Trait. #53613

Open
eddyb opened this issue Aug 22, 2018 · 2 comments
Open

Comments

@eddyb
Copy link
Member

@eddyb eddyb commented Aug 22, 2018

#[derive(Copy, Clone)]
struct Invariant<'a>(std::marker::PhantomData<&'a mut &'a ()>);

impl<'a> Invariant<'a> {
    fn lazy_dup(self) -> impl Fn() -> (Invariant<'a>, Self) {
        || (self, self)
    }
}
error[E0308]: mismatched types
 --> src/lib.rs:6:19
  |
6 |         || (self, self)
  |                   ^^^^ lifetime mismatch
  |
  = note: expected type `Invariant<'static>`
             found type `Invariant<'a>`

I didn't write 'static anywhere - it comes from part of the implementation of impl Trait, and Self references lifetimes (from the impl header) "indirectly", hiding them from impl Trait logic.

cc @cramertj @oli-obk @nikomatsakis

@nikomatsakis
Copy link
Contributor

@nikomatsakis nikomatsakis commented Jul 5, 2019

There is a variant of this problem that was revealed in #62221. If you write T::Item, and we expand that (in type-checking) to <T as Foo<'a>>::Item, the 'a is overlooked as well (playground):

pub trait HasItem<'a> {
    type Item;
}

// Does not compile:
fn example1<'a, T: HasItem<'a>>(item: T::Item) -> impl IntoIterator<Item = T::Item> {
    Some(item)
}

// Compiles:
fn example2<'a, T: HasItem<'a>>(item: T::Item) -> impl IntoIterator<Item = <T as HasItem<'a>>::Item> {
    Some(item)
}

fn main() { }

UPDATE: This is #51525

@nikomatsakis
Copy link
Contributor

@nikomatsakis nikomatsakis commented Jul 9, 2019

I left some notes in this Zulip topic that explain one way to fix this problem. The idea is basically to detect Self during lowering and take it into account.

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

Successfully merging a pull request may close this issue.

None yet
3 participants