]> git.lizzy.rs Git - rust.git/commitdiff
Expand E0309 explanation with motivating example
authorJon Gjengset <jon@thesquareplanet.com>
Mon, 12 Dec 2016 04:57:15 +0000 (23:57 -0500)
committerJon Gjengset <jon@thesquareplanet.com>
Mon, 12 Dec 2016 04:57:20 +0000 (23:57 -0500)
src/librustc/diagnostics.rs

index ec09877ae121cdfeba73bc638f2ecf3492afee51..1655c716b6bfa59dac2c36c52420a055b115e29d 100644 (file)
@@ -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##"