From f09e2cc7ed34ae1623c334df92981bc53a11a6ae Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Sun, 11 Dec 2016 23:57:15 -0500 Subject: [PATCH] Expand E0309 explanation with motivating example --- src/librustc/diagnostics.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index ec09877ae12..1655c716b6b 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -1236,6 +1236,23 @@ struct Foo<'a, T: 'a> { foo: &'a T } ``` + +To see why this is important, consider the case where `T` is itself a reference +(e.g., `T = &str`). If we don't include the restriction that `T: 'a`, the +following code would be perfectly legal: + +```compile_fail,E0309 +struct Foo<'a, T> { + foo: &'a T +} + +fn main() { + let v = "42".to_string(); + let f = Foo{foo: &v}; + drop(v); + println!("{}", f.foo); // but we've already dropped v! +} +``` "##, E0310: r##" -- 2.44.0