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

associated type does not live long enough #23442

Closed
nikomatsakis opened this issue Mar 17, 2015 · 4 comments
Closed

associated type does not live long enough #23442

nikomatsakis opened this issue Mar 17, 2015 · 4 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@nikomatsakis
Copy link
Contributor

This program fails to compile:

#![crate_type="rlib"]

use std::marker::PhantomData;

pub struct UnionedKeys<'a,K>
    where K: UnifyKey + 'a
{
    table: &'a mut UnificationTable<K>,
    root_key: K,
    stack: Vec<K>,
}

pub trait UnifyKey {
    type Value;
}

pub struct UnificationTable<K:UnifyKey> {
    values: Delegate<K>,
}

pub struct Delegate<K>(PhantomData<K>);

This was supposed to be fixed by #22436, but apparently that fix is not working. I've been wanting to revisit the strategy there anyway, because it leads to infinite recursion in some cases and I'm not happy about it.

The error I see is:

/home/nmatsakis/tmp/bar.rs:5:1: 11:2 error: the associated type `<K as UnifyKey>::Value` may not live long enough [E0309]
/home/nmatsakis/tmp/bar.rs:5 pub struct UnionedKeys<'a,K>
/home/nmatsakis/tmp/bar.rs:6     where K: UnifyKey + 'a
/home/nmatsakis/tmp/bar.rs:7 {
/home/nmatsakis/tmp/bar.rs:8     table: &'a mut UnificationTable<K>,
/home/nmatsakis/tmp/bar.rs:9     root_key: K,
/home/nmatsakis/tmp/bar.rs:10     stack: Vec<K>,
                              ...
/home/nmatsakis/tmp/bar.rs:11:2: 11:2 help: consider adding an explicit lifetime bound `<K as UnifyKey>::Value: 'a`...
/home/nmatsakis/tmp/bar.rs:5:1: 11:2 note: ...so that the reference type `&'a mut UnificationTable<K>` does not outlive the data it points at
/home/nmatsakis/tmp/bar.rs:5 pub struct UnionedKeys<'a,K>
/home/nmatsakis/tmp/bar.rs:6     where K: UnifyKey + 'a
/home/nmatsakis/tmp/bar.rs:7 {
/home/nmatsakis/tmp/bar.rs:8     table: &'a mut UnificationTable<K>,
/home/nmatsakis/tmp/bar.rs:9     root_key: K,
/home/nmatsakis/tmp/bar.rs:10     stack: Vec<K>,
                              ...
@nikomatsakis nikomatsakis added A-associated-items Area: Associated items (types, constants & functions) I-wrong labels Mar 17, 2015
@nikomatsakis
Copy link
Contributor Author

The strategy I had in mind was to require that for every associated type T found in an impl:

for<'a> (Self : 'a) => (T : 'a)

This allows us later to deduce that if Self:'a, then T:'a. However, I've not thought this all the way through, because I'm not 100% sure how I'm going to make use of that information. The vague idea was that when we have a requirement that X::T : 'a, we can possibly satisfy that if we can determine that X : 'a. (Note that we cannot deduce however that X::T : 'a => X : 'a, which might sometimes be nice.)

@nikomatsakis
Copy link
Contributor Author

Ah, well, I can see why the original fix didn't work. Nothing in the current system allows it to deduce that K:'a implies K::Value : 'a -- but we CAN deduce that &'a Foo<K> => K:'a and K::Value:'a. Annoying.

@apasel422
Copy link
Contributor

This appears to compile with rustc 1.4.0-nightly (d50352419 2015-08-29).

@sfackler sfackler added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Aug 29, 2015
bors added a commit that referenced this issue Sep 9, 2015
@Eh2406
Copy link
Contributor

Eh2406 commented Sep 6, 2017

There is a FIXME related to this issue,
https://github.com/rust-lang/rust/blob/master/src/libcore/convert.rs#L368
Now that the issue is closed can the FIXME be fix, or made more specific?

cramertj added a commit to cramertj/rust that referenced this issue Oct 19, 2017
A long time ago, FIXMEs were left to implement
AsRef and AsRef for all types which implemented
Deref and DerefMut. This wasn't done at the time
because of rust-lang#23442.

Unfortunately, it's not possible to add these
implementations backwards-compabily, since they
can cause conflicting implementation overlaps.
nivkner added a commit to nivkner/rust that referenced this issue Mar 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

No branches or pull requests

4 participants