Skip to content

Commit

Permalink
Rollup merge of rust-lang#38315 - jonhoo:better-E0309, r=pnkfelix
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alexcrichton committed Dec 20, 2016
2 parents dfa9948 + f09e2cc commit a7710ef
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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##"
Expand Down

0 comments on commit a7710ef

Please sign in to comment.