Skip to content

Commit a7710ef

Browse files
committed
Rollup merge of rust-lang#38315 - jonhoo:better-E0309, r=pnkfelix
Expand E0309 explanation with motivating example I recently started reading @gankro's "[Learning Rust With Entirely Too Many Linked Lists](http://cglab.ca/~abeinges/blah/too-many-lists/book/README.html)", and came across [a part](http://cglab.ca/~abeinges/blah/too-many-lists/book/second-iter.html) where he comes across `E0309`, and after showing `rustc --explain E0309` prompty says > This is dumb. I think it's dumb. You have to do it. Humor aside, I think this says something about the current explanation being somewhat lacking. This patch introduces a motivating example saying why `T: 'a` is a necessary restriction. Hopefully, this will help new Rustaceans understand why leaving out the `'a` bound on `T` might lead to broken code.
2 parents dfa9948 + f09e2cc commit a7710ef

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/librustc/diagnostics.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,23 @@ struct Foo<'a, T: 'a> {
12361236
foo: &'a T
12371237
}
12381238
```
1239+
1240+
To see why this is important, consider the case where `T` is itself a reference
1241+
(e.g., `T = &str`). If we don't include the restriction that `T: 'a`, the
1242+
following code would be perfectly legal:
1243+
1244+
```compile_fail,E0309
1245+
struct Foo<'a, T> {
1246+
foo: &'a T
1247+
}
1248+
1249+
fn main() {
1250+
let v = "42".to_string();
1251+
let f = Foo{foo: &v};
1252+
drop(v);
1253+
println!("{}", f.foo); // but we've already dropped v!
1254+
}
1255+
```
12391256
"##,
12401257

12411258
E0310: r##"

0 commit comments

Comments
 (0)