Skip to content

Commit 439afcd

Browse files
author
Jonathan Turner
committed
Update error message for lifetime of borrowed values
1 parent 86dde9b commit 439afcd

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
@@ -1029,6 +1029,12 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10291029
}
10301030

10311031
err_out_of_scope(super_scope, sub_scope, cause) => {
1032+
let (value_kind, value_msg) = match err.cmt.cat {
1033+
mc::Categorization::Rvalue(_) =>
1034+
("temporary value", "temporary value created here"),
1035+
_ =>
1036+
("borrowed value", "does not live long enough")
1037+
};
10321038
match cause {
10331039
euv::ClosureCapture(s) => {
10341040
// The primary span starts out as the closure creation point.
@@ -1039,13 +1045,13 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10391045
Some(primary) => {
10401046
db.span = MultiSpan::from_span(s);
10411047
db.span_label(primary, &format!("capture occurs here"));
1042-
db.span_label(s, &format!("does not live long enough"));
1048+
db.span_label(s, &value_msg);
10431049
}
10441050
None => ()
10451051
}
10461052
}
10471053
_ => {
1048-
db.span_label(error_span, &format!("does not live long enough"));
1054+
db.span_label(error_span, &value_msg);
10491055
}
10501056
}
10511057

@@ -1054,14 +1060,15 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10541060

10551061
match (sub_span, super_span) {
10561062
(Some(s1), Some(s2)) if s1 == s2 => {
1057-
db.span_label(s1, &"borrowed value dropped before borrower");
1063+
db.span_label(s1, &format!("{} dropped before borrower", value_kind));
10581064
db.note("values in a scope are dropped in the opposite order \
10591065
they are created");
10601066
}
10611067
_ => {
10621068
match sub_span {
10631069
Some(s) => {
1064-
db.span_label(s, &"borrowed value must be valid until here");
1070+
db.span_label(s, &format!("{} needs to live until here",
1071+
value_kind));
10651072
}
10661073
None => {
10671074
self.tcx.note_and_explain_region(
@@ -1073,7 +1080,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10731080
}
10741081
match super_span {
10751082
Some(s) => {
1076-
db.span_label(s, &"borrowed value only valid until here");
1083+
db.span_label(s, &format!("{} only lives until here", value_kind));
10771084
}
10781085
None => {
10791086
self.tcx.note_and_explain_region(
@@ -1086,9 +1093,8 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10861093
}
10871094
}
10881095

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

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 renamed to 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)