-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Closed
Copy link
Labels
A-associated-itemsArea: Associated items (types, constants & functions)Area: Associated items (types, constants & functions)A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-trait-systemArea: Trait systemArea: Trait systemP-highHigh priorityHigh priorityT-langRelevant to the language teamRelevant to the language team
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 .)
Metadata
Metadata
Assignees
Labels
A-associated-itemsArea: Associated items (types, constants & functions)Area: Associated items (types, constants & functions)A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-trait-systemArea: Trait systemArea: Trait systemP-highHigh priorityHigh priorityT-langRelevant to the language teamRelevant to the language team