Skip to content

Commit 59c0ff6

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#36171 - jonathandturner:temporary_value, r=nikomatsakis
Update lifetime errors to specifically note temporaries This PR updates the error message we give in the case of a temporary value not living long enough. Before: <img width="497" alt="screen shot 2016-08-31 at 10 02 47 am" src="https://cloud.githubusercontent.com/assets/547158/18138551/27a06794-6f62-11e6-9ee2-bdf8bed75ca7.png"> Now: <img width="488" alt="screen shot 2016-08-31 at 10 03 01 am" src="https://cloud.githubusercontent.com/assets/547158/18138557/2e5cf322-6f62-11e6-9047-4a78abf3d78c.png"> Specifically, it makes the following changes: * Detects if a temporary is being used. If so, it changes the labels to mention that a temporary value specifically is in question * Simplifies wording of the existing labels to focus on lifetimes rather than values being valid * Changes the help to a note, since the help+span wasn't as helpful (and sometimes more confusing) than just a note. r? @nikomatsakis
2 parents 9327edd + 439afcd commit 59c0ff6

File tree

6 files changed

+44
-29
lines changed

6 files changed

+44
-29
lines changed

src/librustc_borrowck/borrowck/mod.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,12 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10281028
}
10291029

10301030
err_out_of_scope(super_scope, sub_scope, cause) => {
1031+
let (value_kind, value_msg) = match err.cmt.cat {
1032+
mc::Categorization::Rvalue(_) =>
1033+
("temporary value", "temporary value created here"),
1034+
_ =>
1035+
("borrowed value", "does not live long enough")
1036+
};
10311037
match cause {
10321038
euv::ClosureCapture(s) => {
10331039
// The primary span starts out as the closure creation point.
@@ -1038,13 +1044,13 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10381044
Some(primary) => {
10391045
db.span = MultiSpan::from_span(s);
10401046
db.span_label(primary, &format!("capture occurs here"));
1041-
db.span_label(s, &format!("does not live long enough"));
1047+
db.span_label(s, &value_msg);
10421048
}
10431049
None => ()
10441050
}
10451051
}
10461052
_ => {
1047-
db.span_label(error_span, &format!("does not live long enough"));
1053+
db.span_label(error_span, &value_msg);
10481054
}
10491055
}
10501056

@@ -1053,14 +1059,15 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10531059

10541060
match (sub_span, super_span) {
10551061
(Some(s1), Some(s2)) if s1 == s2 => {
1056-
db.span_label(s1, &"borrowed value dropped before borrower");
1062+
db.span_label(s1, &format!("{} dropped before borrower", value_kind));
10571063
db.note("values in a scope are dropped in the opposite order \
10581064
they are created");
10591065
}
10601066
_ => {
10611067
match sub_span {
10621068
Some(s) => {
1063-
db.span_label(s, &"borrowed value must be valid until here");
1069+
db.span_label(s, &format!("{} needs to live until here",
1070+
value_kind));
10641071
}
10651072
None => {
10661073
self.tcx.note_and_explain_region(
@@ -1072,7 +1079,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10721079
}
10731080
match super_span {
10741081
Some(s) => {
1075-
db.span_label(s, &"borrowed value only valid until here");
1082+
db.span_label(s, &format!("{} only lives until here", value_kind));
10761083
}
10771084
None => {
10781085
self.tcx.note_and_explain_region(
@@ -1085,9 +1092,8 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10851092
}
10861093
}
10871094

1088-
if let Some(span) = statement_scope_span(self.tcx, super_scope) {
1089-
db.span_help(span,
1090-
"consider using a `let` binding to increase its lifetime");
1095+
if let Some(_) = statement_scope_span(self.tcx, super_scope) {
1096+
db.note("consider using a `let` binding to increase its lifetime");
10911097
}
10921098
}
10931099

src/test/compile-fail/borrowck/borrowck-let-suggestion-suffixes.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,36 @@ fn f() {
2525

2626
v3.push(&'x'); // statement 6
2727
//~^ ERROR borrowed value does not live long enough
28-
//~| NOTE does not live long enough
29-
//~| NOTE borrowed value only valid until here
30-
//~| HELP consider using a `let` binding to increase its lifetime
28+
//~| NOTE temporary value created here
29+
//~| NOTE temporary value only lives until here
30+
//~| NOTE consider using a `let` binding to increase its lifetime
3131

3232
{
3333

3434
let mut v4 = Vec::new(); // (sub) statement 0
3535

3636
v4.push(&'y');
3737
//~^ ERROR borrowed value does not live long enough
38-
//~| NOTE does not live long enough
39-
//~| NOTE borrowed value only valid until here
40-
//~| HELP consider using a `let` binding to increase its lifetime
38+
//~| NOTE temporary value created here
39+
//~| NOTE temporary value only lives until here
40+
//~| NOTE consider using a `let` binding to increase its lifetime
4141

4242
} // (statement 7)
43-
//~^ NOTE borrowed value must be valid until here
43+
//~^ NOTE temporary value needs to live until here
4444

4545
let mut v5 = Vec::new(); // statement 8
4646

4747
v5.push(&'z');
4848
//~^ ERROR borrowed value does not live long enough
49-
//~| NOTE does not live long enough
50-
//~| NOTE borrowed value only valid until here
51-
//~| HELP consider using a `let` binding to increase its lifetime
49+
//~| NOTE temporary value created here
50+
//~| NOTE temporary value only lives until here
51+
//~| NOTE consider using a `let` binding to increase its lifetime
5252

5353
v1.push(&old[0]);
5454
}
5555
//~^ NOTE borrowed value dropped before borrower
56-
//~| NOTE borrowed value must be valid until here
57-
//~| NOTE borrowed value must be valid until here
56+
//~| NOTE temporary value needs to live until here
57+
//~| NOTE temporary value needs to live until here
5858

5959
fn main() {
6060
f();

src/test/compile-fail/regions-escape-loop-via-vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ fn broken() {
2424
x += 1; //~ ERROR cannot assign
2525
//~^ NOTE assignment to borrowed `x` occurs here
2626
}
27-
//~^ NOTE borrowed value only valid until here
27+
//~^ NOTE borrowed value only lives until here
2828
}
29-
//~^ NOTE borrowed value must be valid until here
29+
//~^ NOTE borrowed value needs to live until here
3030

3131
fn main() { }

src/test/compile-fail/borrowck/borrowck-let-suggestion.rs src/test/ui/lifetimes/borrowck-let-suggestion.rs

-5
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@
1010

1111
fn f() {
1212
let x = [1].iter();
13-
//~^ ERROR borrowed value does not live long enough
14-
//~| NOTE does not live long enough
15-
//~| NOTE borrowed value only valid until here
16-
//~| HELP consider using a `let` binding to increase its lifetime
1713
}
18-
//~^ borrowed value must be valid until here
1914

2015
fn main() {
2116
f();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: borrowed value does not live long enough
2+
--> $DIR/borrowck-let-suggestion.rs:12:13
3+
|
4+
12 | let x = [1].iter();
5+
| ^^^ - temporary value only lives until here
6+
| |
7+
| temporary value created here
8+
13 | }
9+
| - temporary value needs to live until here
10+
|
11+
= note: consider using a `let` binding to increase its lifetime
12+
13+
error: aborting due to previous error
14+

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ error: `x` does not live long enough
55
| ^
66
| |
77
| does not live long enough
8-
| borrowed value only valid until here
8+
| borrowed value only lives until here
99
...
1010
23 | }
11-
| - borrowed value must be valid until here
11+
| - borrowed value needs to live until here
1212

1313
error: aborting due to previous error
1414

0 commit comments

Comments
 (0)