Skip to content

Commit dbb2506

Browse files
author
Jonathan Turner
authored
Rollup merge of #37405 - mikhail-m1:dnlle, r=jonathandturner
Improve "Doesn't live long enough" error case with different lifetime scope issue #36537 part of #35233 r? @jonathandturner
2 parents d00e5e9 + a0e7e35 commit dbb2506

29 files changed

+309
-6
lines changed

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,19 @@ 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 => {
1068+
db.span = MultiSpan::from_span(s2);
1069+
db.span_label(error_span, &format!("borrow occurs here"));
1070+
let msg = match opt_loan_path(&err.cmt) {
1071+
None => "borrowed value".to_string(),
1072+
Some(lp) => {
1073+
format!("`{}`", self.loan_path_to_string(&lp))
1074+
}
1075+
};
1076+
db.span_label(s2,
1077+
&format!("{} dropped here while still borrowed", msg));
1078+
db.span_label(s1, &format!("{} needs to live until here", value_kind));
1079+
}
10671080
_ => {
10681081
match sub_span {
10691082
Some(s) => {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: borrowed value does not live long enough
2+
--> $DIR/borrowck-ref-into-rvalue.rs:18:5
3+
|
4+
14 | Some(ref m) => { //~ ERROR borrowed value does not live long enough
5+
| ----- borrow occurs here
6+
...
7+
18 | }
8+
| ^ borrowed value dropped here while still borrowed
9+
19 | println!("{}", *msg);
10+
20 | }
11+
| - borrowed value needs to live until here
12+
|
13+
= note: consider using a `let` binding to increase its lifetime
14+
15+
error: aborting due to previous error
16+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: `*a` does not live long enough
2+
--> $DIR/destructor-restrictions.rs:19:5
3+
|
4+
18 | *a.borrow() + 1 //~ ERROR `*a` does not live long enough
5+
| - borrow occurs here
6+
19 | };
7+
| ^- borrowed value needs to live until here
8+
| |
9+
| `*a` dropped here while still borrowed
10+
11+
error: aborting due to previous error
12+

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error: `x` does not live long enough
44
18 | let f = to_fn_once(move|| &x);
55
| ^
66
| |
7-
| does not live long enough
8-
| borrowed value only lives until here
7+
| borrow occurs here
8+
| `x` dropped here while still borrowed
99
...
1010
23 | }
1111
| - borrowed value needs to live until here

src/test/ui/span/issue-23338-locals-die-before-temps-of-body.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ error: `y` does not live long enough
99
= note: values in a scope are dropped in the opposite order they are created
1010

1111
error: `y` does not live long enough
12-
--> $DIR/issue-23338-locals-die-before-temps-of-body.rs:27:9
12+
--> $DIR/issue-23338-locals-die-before-temps-of-body.rs:28:5
1313
|
1414
27 | y.borrow().clone() //~ ERROR `y` does not live long enough
15-
| ^ does not live long enough
15+
| - borrow occurs here
1616
28 | };
17-
| -- borrowed value needs to live until here
17+
| ^- borrowed value needs to live until here
1818
| |
19-
| borrowed value only lives until here
19+
| `y` dropped here while still borrowed
2020

2121
error: aborting due to 2 previous errors
2222

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: `b` does not live long enough
2+
--> $DIR/mut-ptr-cant-outlive-ref.rs:19:5
3+
|
4+
18 | p = &*b; //~ ERROR `b` does not live long enough
5+
| - borrow occurs here
6+
19 | }
7+
| ^ `b` dropped here while still borrowed
8+
20 | }
9+
| - borrowed value needs to live until here
10+
11+
error: aborting due to previous error
12+
File renamed without changes.

0 commit comments

Comments
 (0)