Closed
Description
pub trait Foo<'a> {
type Bar: std::fmt::Debug;
fn baz(&'a self) -> Self::Bar;
}
pub struct X {
name : String,
}
impl<'a> Foo<'a> for X {
type Bar = &'a String;
fn baz(&'a self) -> &'a String {
&self.name
}
}
pub fn foo<T: for<'a> Foo<'a>>(x: T) {
let y = x.baz();
drop(x);
// for T = X, this is a &String pointing to dropped memory
println!("{:?}", y);
}
pub fn main() {
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
.
(Noticed in http://stackoverflow.com/q/29745134/1256624 .)