1
Fork 0

Improve "Doesn't live long enough" error

case with temporary variable
This commit is contained in:
Mikhail Modin 2016-11-03 09:58:01 +03:00
parent a0e7e357a7
commit cfdf7633f0
9 changed files with 63 additions and 24 deletions

View file

@ -1013,11 +1013,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
} }
err_out_of_scope(super_scope, sub_scope, cause) => { err_out_of_scope(super_scope, sub_scope, cause) => {
let (value_kind, value_msg, is_temporary) = match err.cmt.cat { let (value_kind, value_msg) = match err.cmt.cat {
mc::Categorization::Rvalue(_) => mc::Categorization::Rvalue(_) =>
("temporary value", "temporary value created here", true), ("temporary value", "temporary value created here"),
_ => _ =>
("borrowed value", "does not live long enough", false) ("borrowed value", "borrow occurs here")
}; };
let is_closure = match cause { let is_closure = match cause {
@ -1030,14 +1030,14 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
Some(primary) => { Some(primary) => {
db.span = MultiSpan::from_span(s); db.span = MultiSpan::from_span(s);
db.span_label(primary, &format!("capture occurs here")); db.span_label(primary, &format!("capture occurs here"));
db.span_label(s, &value_msg); db.span_label(s, &"does not live long enough");
true true
} }
None => false None => false
} }
} }
_ => { _ => {
db.span_label(error_span, &value_msg); db.span_label(error_span, &"does not live long enough");
false false
} }
}; };
@ -1047,11 +1047,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
match (sub_span, super_span) { match (sub_span, super_span) {
(Some(s1), Some(s2)) if s1 == s2 => { (Some(s1), Some(s2)) if s1 == s2 => {
if !is_temporary && !is_closure { if !is_closure {
db.span = MultiSpan::from_span(s1); db.span = MultiSpan::from_span(s1);
db.span_label(error_span, &format!("borrow occurs here")); db.span_label(error_span, &value_msg);
let msg = match opt_loan_path(&err.cmt) { let msg = match opt_loan_path(&err.cmt) {
None => "borrowed value".to_string(), None => value_kind.to_string(),
Some(lp) => { Some(lp) => {
format!("`{}`", self.loan_path_to_string(&lp)) format!("`{}`", self.loan_path_to_string(&lp))
} }
@ -1064,17 +1064,16 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
db.note("values in a scope are dropped in the opposite order \ db.note("values in a scope are dropped in the opposite order \
they are created"); they are created");
} }
(Some(s1), Some(s2)) if !is_temporary && !is_closure => { (Some(s1), Some(s2)) if !is_closure => {
db.span = MultiSpan::from_span(s2); db.span = MultiSpan::from_span(s2);
db.span_label(error_span, &format!("borrow occurs here")); db.span_label(error_span, &value_msg);
let msg = match opt_loan_path(&err.cmt) { let msg = match opt_loan_path(&err.cmt) {
None => "borrowed value".to_string(), None => value_kind.to_string(),
Some(lp) => { Some(lp) => {
format!("`{}`", self.loan_path_to_string(&lp)) format!("`{}`", self.loan_path_to_string(&lp))
} }
}; };
db.span_label(s2, db.span_label(s2, &format!("{} dropped here while still borrowed", msg));
&format!("{} dropped here while still borrowed", msg));
db.span_label(s1, &format!("{} needs to live until here", value_kind)); db.span_label(s1, &format!("{} needs to live until here", value_kind));
} }
_ => { _ => {

View file

@ -1,8 +1,8 @@
error: borrowed value does not live long enough error: borrowed value does not live long enough
--> $DIR/borrowck-let-suggestion.rs:12:13 --> $DIR/borrowck-let-suggestion.rs:12:23
| |
12 | let x = [1].iter(); 12 | let x = [1].iter();
| ^^^ - temporary value only lives until here | --- ^ temporary value dropped here while still borrowed
| | | |
| temporary value created here | temporary value created here
13 | } 13 | }

View file

@ -10,10 +10,10 @@ error: `young[..]` does not live long enough
= note: values in a scope are dropped in the opposite order they are created = note: values in a scope are dropped in the opposite order they are created
error: borrowed value does not live long enough error: borrowed value does not live long enough
--> $DIR/borrowck-let-suggestion-suffixes.rs:24:14 --> $DIR/borrowck-let-suggestion-suffixes.rs:24:18
| |
24 | v3.push(&'x'); // statement 6 24 | v3.push(&'x'); // statement 6
| ^^^ - temporary value only lives until here | --- ^ temporary value dropped here while still borrowed
| | | |
| temporary value created here | temporary value created here
... ...
@ -23,10 +23,10 @@ error: borrowed value does not live long enough
= note: consider using a `let` binding to increase its lifetime = note: consider using a `let` binding to increase its lifetime
error: borrowed value does not live long enough error: borrowed value does not live long enough
--> $DIR/borrowck-let-suggestion-suffixes.rs:34:18 --> $DIR/borrowck-let-suggestion-suffixes.rs:34:22
| |
34 | v4.push(&'y'); 34 | v4.push(&'y');
| ^^^ - temporary value only lives until here | --- ^ temporary value dropped here while still borrowed
| | | |
| temporary value created here | temporary value created here
... ...
@ -36,10 +36,10 @@ error: borrowed value does not live long enough
= note: consider using a `let` binding to increase its lifetime = note: consider using a `let` binding to increase its lifetime
error: borrowed value does not live long enough error: borrowed value does not live long enough
--> $DIR/borrowck-let-suggestion-suffixes.rs:45:14 --> $DIR/borrowck-let-suggestion-suffixes.rs:45:18
| |
45 | v5.push(&'z'); 45 | v5.push(&'z');
| ^^^ - temporary value only lives until here | --- ^ temporary value dropped here while still borrowed
| | | |
| temporary value created here | temporary value created here
... ...

View file

@ -11,7 +11,6 @@
fn main() { fn main() {
let v = vec![ let v = vec![
&3 &3
//~^ ERROR borrowed value does not live long enough
]; ];
for &&x in &v { for &&x in &v {

View file

@ -0,0 +1,15 @@
error: borrowed value does not live long enough
--> $DIR/issue-15480.rs:14:6
|
13 | &3
| - temporary value created here
14 | ];
| ^ temporary value dropped here while still borrowed
...
19 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
error: aborting due to previous error

View file

@ -17,7 +17,7 @@ impl<'a> Foo for &'a isize { }
fn main() { fn main() {
let blah; let blah;
{ {
let ss: &isize = &1; //~ ERROR borrowed value does not live long enough let ss: &isize = &1;
blah = box ss as Box<Foo>; blah = box ss as Box<Foo>;
} }
} }

View file

@ -0,0 +1,13 @@
error: borrowed value does not live long enough
--> $DIR/regions-close-over-borrowed-ref-in-obj.rs:22:5
|
20 | let ss: &isize = &1;
| - temporary value created here
21 | blah = box ss as Box<Foo>;
22 | }
| ^ temporary value dropped here while still borrowed
23 | }
| - temporary value needs to live until here
error: aborting due to previous error

View file

@ -13,7 +13,7 @@
fn main() { fn main() {
let y; let y;
{ {
let x: &[isize] = &[1, 2, 3, 4, 5]; //~ ERROR borrowed value does not live long enough let x: &[isize] = &[1, 2, 3, 4, 5];
y = &x[1..]; y = &x[1..];
} }
} }

View file

@ -0,0 +1,13 @@
error: borrowed value does not live long enough
--> $DIR/slice-borrow.rs:18:5
|
16 | let x: &[isize] = &[1, 2, 3, 4, 5];
| --------------- temporary value created here
17 | y = &x[1..];
18 | }
| ^ temporary value dropped here while still borrowed
19 | }
| - temporary value needs to live until here
error: aborting due to previous error