You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Mismatched assumptions for interaction between associated type and lifetime in trait #24622
pubtraitFoo<'a>{typeBar: std::fmt::Debug;fnbaz(&'aself) -> Self::Bar;}pubstructX{name:String,}impl<'a>Foo<'a>forX{typeBar = &'aString;fnbaz(&'aself) -> &'aString{&self.name}}pubfnfoo<T:for<'a>Foo<'a>>(x:T){let y = x.baz();drop(x);// for T = X, this is a &String pointing to dropped memoryprintln!("{:?}", y);}pubfnmain(){foo(X{name:"foo".to_string()});}
This segfaults.
The impl is allowing &'a String because 'a is in scope, but the use in foo assumes that Bar is independent of 'a.
pubtraitFoo<'a>{typeBar;}impl<'a,T>Foo<'a>forT{typeBar = &'aT;}fndenormalise<'a,T>(t:&'aT) -> <TasFoo<'a>>::Bar{
t
}pubfnfree_and_use<T:for<'a>Foo<'a>,F:for<'a>FnOnce(<TasFoo<'a>>::Bar)>(x:T,f:F){let y;'body:loop{// lifetime annotations added for clarity's:loop{ y = denormalise(&x);break}drop(x);returnf(y);}}pubfnmain(){free_and_use("foo".to_string(), |s| println!("{}", s));}
It is clear here that the declaration and implementation of Foo is not doing anything wrong, but free_and_use wrongly assumes that <T as Foo<'s>>::Bar : 'body, which does not make a lot of sense.
Yes, I see the problem. It lies in some of the logic around type parameter outlives rules -- we assume type parameters outlive the current fn, and we assume the same of projections, which is clearly wrong. I've been wanting to revisit these rules about projections and how long they live (#23442) though not for soundness reasons.
Activity
huonw commentedon Apr 20, 2015
triage: P-high
cc @nikomatsakis
bluss commentedon Apr 20, 2015
petgraph uses this kind of trait to provide generic "neighbor vertex" iterators.
arielb1 commentedon Apr 21, 2015
I think a cleaner example is:
It is clear here that the declaration and implementation of
Foois not doing anything wrong, butfree_and_usewrongly assumes that<T as Foo<'s>>::Bar : 'body, which does not make a lot of sense.nikomatsakis commentedon Apr 24, 2015
Yes, I see the problem. It lies in some of the logic around type parameter outlives rules -- we assume type parameters outlive the current fn, and we assume the same of projections, which is clearly wrong. I've been wanting to revisit these rules about projections and how long they live (#23442) though not for soundness reasons.
nikomatsakis commentedon Apr 24, 2015
triage: P-high
nikomatsakis commentedon May 28, 2015
Just FYI -- I've been working steadily on this. I'm going to try and writeup my thoughts "soon" (famous last words).
2 remaining items
Modify implicator to take a "syntactic" approach, ensuring that fns and
Modify implicator to take a "syntactic" approach, ensuring that fns and
nikomatsakis commentedon Nov 5, 2015
This original example is now giving me an error on stable: http://is.gd/bidora
The question is, did we add a test?
nikomatsakis commentedon Nov 5, 2015
The test associated-types-outlives.rs claims to be a regression test for this issue. Closing!