Skip to content

Commit cfdf763

Browse files
committed
Improve "Doesn't live long enough" error
case with temporary variable
1 parent a0e7e35 commit cfdf763

9 files changed

+63
-24
lines changed

src/librustc_borrowck/borrowck/mod.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1013,11 +1013,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10131013
}
10141014

10151015
err_out_of_scope(super_scope, sub_scope, cause) => {
1016-
let (value_kind, value_msg, is_temporary) = match err.cmt.cat {
1016+
let (value_kind, value_msg) = match err.cmt.cat {
10171017
mc::Categorization::Rvalue(_) =>
1018-
("temporary value", "temporary value created here", true),
1018+
("temporary value", "temporary value created here"),
10191019
_ =>
1020-
("borrowed value", "does not live long enough", false)
1020+
("borrowed value", "borrow occurs here")
10211021
};
10221022

10231023
let is_closure = match cause {
@@ -1030,14 +1030,14 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10301030
Some(primary) => {
10311031
db.span = MultiSpan::from_span(s);
10321032
db.span_label(primary, &format!("capture occurs here"));
1033-
db.span_label(s, &value_msg);
1033+
db.span_label(s, &"does not live long enough");
10341034
true
10351035
}
10361036
None => false
10371037
}
10381038
}
10391039
_ => {
1040-
db.span_label(error_span, &value_msg);
1040+
db.span_label(error_span, &"does not live long enough");
10411041
false
10421042
}
10431043
};
@@ -1047,11 +1047,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10471047

10481048
match (sub_span, super_span) {
10491049
(Some(s1), Some(s2)) if s1 == s2 => {
1050-
if !is_temporary && !is_closure {
1050+
if !is_closure {
10511051
db.span = MultiSpan::from_span(s1);
1052-
db.span_label(error_span, &format!("borrow occurs here"));
1052+
db.span_label(error_span, &value_msg);
10531053
let msg = match opt_loan_path(&err.cmt) {
1054-
None => "borrowed value".to_string(),
1054+
None => value_kind.to_string(),
10551055
Some(lp) => {
10561056
format!("`{}`", self.loan_path_to_string(&lp))
10571057
}
@@ -1064,17 +1064,16 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10641064
db.note("values in a scope are dropped in the opposite order \
10651065
they are created");
10661066
}
1067-
(Some(s1), Some(s2)) if !is_temporary && !is_closure => {
1067+
(Some(s1), Some(s2)) if !is_closure => {
10681068
db.span = MultiSpan::from_span(s2);
1069-
db.span_label(error_span, &format!("borrow occurs here"));
1069+
db.span_label(error_span, &value_msg);
10701070
let msg = match opt_loan_path(&err.cmt) {
1071-
None => "borrowed value".to_string(),
1071+
None => value_kind.to_string(),
10721072
Some(lp) => {
10731073
format!("`{}`", self.loan_path_to_string(&lp))
10741074
}
10751075
};
1076-
db.span_label(s2,
1077-
&format!("{} dropped here while still borrowed", msg));
1076+
db.span_label(s2, &format!("{} dropped here while still borrowed", msg));
10781077
db.span_label(s1, &format!("{} needs to live until here", value_kind));
10791078
}
10801079
_ => {

src/test/ui/lifetimes/borrowck-let-suggestion.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: borrowed value does not live long enough
2-
--> $DIR/borrowck-let-suggestion.rs:12:13
2+
--> $DIR/borrowck-let-suggestion.rs:12:23
33
|
44
12 | let x = [1].iter();
5-
| ^^^ - temporary value only lives until here
5+
| --- ^ temporary value dropped here while still borrowed
66
| |
77
| temporary value created here
88
13 | }

src/test/ui/span/borrowck-let-suggestion-suffixes.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ error: `young[..]` does not live long enough
1010
= note: values in a scope are dropped in the opposite order they are created
1111

1212
error: borrowed value does not live long enough
13-
--> $DIR/borrowck-let-suggestion-suffixes.rs:24:14
13+
--> $DIR/borrowck-let-suggestion-suffixes.rs:24:18
1414
|
1515
24 | v3.push(&'x'); // statement 6
16-
| ^^^ - temporary value only lives until here
16+
| --- ^ temporary value dropped here while still borrowed
1717
| |
1818
| temporary value created here
1919
...
@@ -23,10 +23,10 @@ error: borrowed value does not live long enough
2323
= note: consider using a `let` binding to increase its lifetime
2424

2525
error: borrowed value does not live long enough
26-
--> $DIR/borrowck-let-suggestion-suffixes.rs:34:18
26+
--> $DIR/borrowck-let-suggestion-suffixes.rs:34:22
2727
|
2828
34 | v4.push(&'y');
29-
| ^^^ - temporary value only lives until here
29+
| --- ^ temporary value dropped here while still borrowed
3030
| |
3131
| temporary value created here
3232
...
@@ -36,10 +36,10 @@ error: borrowed value does not live long enough
3636
= note: consider using a `let` binding to increase its lifetime
3737

3838
error: borrowed value does not live long enough
39-
--> $DIR/borrowck-let-suggestion-suffixes.rs:45:14
39+
--> $DIR/borrowck-let-suggestion-suffixes.rs:45:18
4040
|
4141
45 | v5.push(&'z');
42-
| ^^^ - temporary value only lives until here
42+
| --- ^ temporary value dropped here while still borrowed
4343
| |
4444
| temporary value created here
4545
...

src/test/compile-fail/issue-15480.rs renamed to src/test/ui/span/issue-15480.rs

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
fn main() {
1212
let v = vec![
1313
&3
14-
//~^ ERROR borrowed value does not live long enough
1514
];
1615

1716
for &&x in &v {

src/test/ui/span/issue-15480.stderr

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

src/test/compile-fail/regions-close-over-borrowed-ref-in-obj.rs renamed to src/test/ui/span/regions-close-over-borrowed-ref-in-obj.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl<'a> Foo for &'a isize { }
1717
fn main() {
1818
let blah;
1919
{
20-
let ss: &isize = &1; //~ ERROR borrowed value does not live long enough
20+
let ss: &isize = &1;
2121
blah = box ss as Box<Foo>;
2222
}
2323
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: borrowed value does not live long enough
2+
--> $DIR/regions-close-over-borrowed-ref-in-obj.rs:22:5
3+
|
4+
20 | let ss: &isize = &1;
5+
| - temporary value created here
6+
21 | blah = box ss as Box<Foo>;
7+
22 | }
8+
| ^ temporary value dropped here while still borrowed
9+
23 | }
10+
| - temporary value needs to live until here
11+
12+
error: aborting due to previous error
13+

src/test/compile-fail/slice-borrow.rs renamed to src/test/ui/span/slice-borrow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
fn main() {
1414
let y;
1515
{
16-
let x: &[isize] = &[1, 2, 3, 4, 5]; //~ ERROR borrowed value does not live long enough
16+
let x: &[isize] = &[1, 2, 3, 4, 5];
1717
y = &x[1..];
1818
}
1919
}

src/test/ui/span/slice-borrow.stderr

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

0 commit comments

Comments
 (0)