Skip to content

Commit a6fa572

Browse files
authored
Auto merge of #37174 - mikhail-m1:dnlle, r=jonathandturner
improve "Doesn't live long enough" error I've fixed only with same case issue #36537 part of #35233 r? @jonathandturner
2 parents 5509ae3 + e852775 commit a6fa572

File tree

48 files changed

+711
-98
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+711
-98
lines changed

src/librustc_borrowck/borrowck/mod.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -1024,13 +1024,14 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10241024
}
10251025

10261026
err_out_of_scope(super_scope, sub_scope, cause) => {
1027-
let (value_kind, value_msg) = match err.cmt.cat {
1027+
let (value_kind, value_msg, is_temporary) = match err.cmt.cat {
10281028
mc::Categorization::Rvalue(_) =>
1029-
("temporary value", "temporary value created here"),
1029+
("temporary value", "temporary value created here", true),
10301030
_ =>
1031-
("borrowed value", "does not live long enough")
1031+
("borrowed value", "does not live long enough", false)
10321032
};
1033-
match cause {
1033+
1034+
let is_closure = match cause {
10341035
euv::ClosureCapture(s) => {
10351036
// The primary span starts out as the closure creation point.
10361037
// Change the primary span here to highlight the use of the variable
@@ -1041,21 +1042,36 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10411042
db.span = MultiSpan::from_span(s);
10421043
db.span_label(primary, &format!("capture occurs here"));
10431044
db.span_label(s, &value_msg);
1045+
true
10441046
}
1045-
None => ()
1047+
None => false
10461048
}
10471049
}
10481050
_ => {
10491051
db.span_label(error_span, &value_msg);
1052+
false
10501053
}
1051-
}
1054+
};
10521055

10531056
let sub_span = self.region_end_span(sub_scope);
10541057
let super_span = self.region_end_span(super_scope);
10551058

10561059
match (sub_span, super_span) {
10571060
(Some(s1), Some(s2)) if s1 == s2 => {
1058-
db.span_label(s1, &format!("{} dropped before borrower", value_kind));
1061+
if !is_temporary && !is_closure {
1062+
db.span = MultiSpan::from_span(s1);
1063+
db.span_label(error_span, &format!("borrow occurs here"));
1064+
let msg = match opt_loan_path(&err.cmt) {
1065+
None => "borrowed value".to_string(),
1066+
Some(lp) => {
1067+
format!("`{}`", self.loan_path_to_string(&lp))
1068+
}
1069+
};
1070+
db.span_label(s1,
1071+
&format!("{} dropped here while still borrowed", msg));
1072+
} else {
1073+
db.span_label(s1, &format!("{} dropped before borrower", value_kind));
1074+
}
10591075
db.note("values in a scope are dropped in the opposite order \
10601076
they are created");
10611077
}

src/test/compile-fail-fulldeps/dropck_tarena_cycle_checked.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,5 @@ fn f<'a>(arena: &'a TypedArena<C<'a>>) {
123123

124124
fn main() {
125125
let arena = TypedArena::new();
126-
f(&arena); //~ ERROR `arena` does not live long enough
127-
}
126+
f(&arena);
127+
} //~ ERROR `arena` does not live long enough

src/test/compile-fail-fulldeps/dropck_tarena_unsound_drop.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ fn f<'a>(_arena: &'a TypedArena<C<'a>>) {}
4646

4747
fn main() {
4848
let arena: TypedArena<C> = TypedArena::new();
49-
f(&arena); //~ ERROR `arena` does not live long enough
50-
}
49+
f(&arena);
50+
} //~ ERROR `arena` does not live long enough
51+

src/test/ui/dropck/dropck-eyepatch-extern-crate.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
error: `c` does not live long enough
2-
--> $DIR/dropck-eyepatch-extern-crate.rs:39:20
2+
--> $DIR/dropck-eyepatch-extern-crate.rs:55:1
33
|
44
39 | dt = Dt("dt", &c); //~ ERROR `c` does not live long enough
5-
| ^ does not live long enough
5+
| - borrow occurs here
66
...
77
55 | }
8-
| - borrowed value dropped before borrower
8+
| ^ `c` dropped here while still borrowed
99
|
1010
= note: values in a scope are dropped in the opposite order they are created
1111

1212
error: `c` does not live long enough
13-
--> $DIR/dropck-eyepatch-extern-crate.rs:40:20
13+
--> $DIR/dropck-eyepatch-extern-crate.rs:55:1
1414
|
1515
40 | dr = Dr("dr", &c); //~ ERROR `c` does not live long enough
16-
| ^ does not live long enough
16+
| - borrow occurs here
1717
...
1818
55 | }
19-
| - borrowed value dropped before borrower
19+
| ^ `c` dropped here while still borrowed
2020
|
2121
= note: values in a scope are dropped in the opposite order they are created
2222

2323
error: `c` does not live long enough
24-
--> $DIR/dropck-eyepatch-extern-crate.rs:47:29
24+
--> $DIR/dropck-eyepatch-extern-crate.rs:55:1
2525
|
2626
47 | pt = Pt("pt", &c_long, &c); //~ ERROR `c` does not live long enough
27-
| ^ does not live long enough
27+
| - borrow occurs here
2828
...
2929
55 | }
30-
| - borrowed value dropped before borrower
30+
| ^ `c` dropped here while still borrowed
3131
|
3232
= note: values in a scope are dropped in the opposite order they are created
3333

3434
error: `c` does not live long enough
35-
--> $DIR/dropck-eyepatch-extern-crate.rs:48:29
35+
--> $DIR/dropck-eyepatch-extern-crate.rs:55:1
3636
|
3737
48 | pr = Pr("pr", &c_long, &c); //~ ERROR `c` does not live long enough
38-
| ^ does not live long enough
38+
| - borrow occurs here
3939
...
4040
55 | }
41-
| - borrowed value dropped before borrower
41+
| ^ `c` dropped here while still borrowed
4242
|
4343
= note: values in a scope are dropped in the opposite order they are created
4444

src/test/ui/dropck/dropck-eyepatch-reorder.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
error: `c` does not live long enough
2-
--> $DIR/dropck-eyepatch-reorder.rs:57:20
2+
--> $DIR/dropck-eyepatch-reorder.rs:73:1
33
|
44
57 | dt = Dt("dt", &c); //~ ERROR `c` does not live long enough
5-
| ^ does not live long enough
5+
| - borrow occurs here
66
...
77
73 | }
8-
| - borrowed value dropped before borrower
8+
| ^ `c` dropped here while still borrowed
99
|
1010
= note: values in a scope are dropped in the opposite order they are created
1111

1212
error: `c` does not live long enough
13-
--> $DIR/dropck-eyepatch-reorder.rs:58:20
13+
--> $DIR/dropck-eyepatch-reorder.rs:73:1
1414
|
1515
58 | dr = Dr("dr", &c); //~ ERROR `c` does not live long enough
16-
| ^ does not live long enough
16+
| - borrow occurs here
1717
...
1818
73 | }
19-
| - borrowed value dropped before borrower
19+
| ^ `c` dropped here while still borrowed
2020
|
2121
= note: values in a scope are dropped in the opposite order they are created
2222

2323
error: `c` does not live long enough
24-
--> $DIR/dropck-eyepatch-reorder.rs:65:29
24+
--> $DIR/dropck-eyepatch-reorder.rs:73:1
2525
|
2626
65 | pt = Pt("pt", &c_long, &c); //~ ERROR `c` does not live long enough
27-
| ^ does not live long enough
27+
| - borrow occurs here
2828
...
2929
73 | }
30-
| - borrowed value dropped before borrower
30+
| ^ `c` dropped here while still borrowed
3131
|
3232
= note: values in a scope are dropped in the opposite order they are created
3333

3434
error: `c` does not live long enough
35-
--> $DIR/dropck-eyepatch-reorder.rs:66:29
35+
--> $DIR/dropck-eyepatch-reorder.rs:73:1
3636
|
3737
66 | pr = Pr("pr", &c_long, &c); //~ ERROR `c` does not live long enough
38-
| ^ does not live long enough
38+
| - borrow occurs here
3939
...
4040
73 | }
41-
| - borrowed value dropped before borrower
41+
| ^ `c` dropped here while still borrowed
4242
|
4343
= note: values in a scope are dropped in the opposite order they are created
4444

src/test/ui/dropck/dropck-eyepatch.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
error: `c` does not live long enough
2-
--> $DIR/dropck-eyepatch.rs:80:20
2+
--> $DIR/dropck-eyepatch.rs:96:1
33
|
44
80 | dt = Dt("dt", &c); //~ ERROR `c` does not live long enough
5-
| ^ does not live long enough
5+
| - borrow occurs here
66
...
77
96 | }
8-
| - borrowed value dropped before borrower
8+
| ^ `c` dropped here while still borrowed
99
|
1010
= note: values in a scope are dropped in the opposite order they are created
1111

1212
error: `c` does not live long enough
13-
--> $DIR/dropck-eyepatch.rs:81:20
13+
--> $DIR/dropck-eyepatch.rs:96:1
1414
|
1515
81 | dr = Dr("dr", &c); //~ ERROR `c` does not live long enough
16-
| ^ does not live long enough
16+
| - borrow occurs here
1717
...
1818
96 | }
19-
| - borrowed value dropped before borrower
19+
| ^ `c` dropped here while still borrowed
2020
|
2121
= note: values in a scope are dropped in the opposite order they are created
2222

2323
error: `c` does not live long enough
24-
--> $DIR/dropck-eyepatch.rs:88:29
24+
--> $DIR/dropck-eyepatch.rs:96:1
2525
|
2626
88 | pt = Pt("pt", &c_long, &c); //~ ERROR `c` does not live long enough
27-
| ^ does not live long enough
27+
| - borrow occurs here
2828
...
2929
96 | }
30-
| - borrowed value dropped before borrower
30+
| ^ `c` dropped here while still borrowed
3131
|
3232
= note: values in a scope are dropped in the opposite order they are created
3333

3434
error: `c` does not live long enough
35-
--> $DIR/dropck-eyepatch.rs:89:29
35+
--> $DIR/dropck-eyepatch.rs:96:1
3636
|
3737
89 | pr = Pr("pr", &c_long, &c); //~ ERROR `c` does not live long enough
38-
| ^ does not live long enough
38+
| - borrow occurs here
3939
...
4040
96 | }
41-
| - borrowed value dropped before borrower
41+
| ^ `c` dropped here while still borrowed
4242
|
4343
= note: values in a scope are dropped in the opposite order they are created
4444

src/test/compile-fail/borrowck/borrowck-let-suggestion-suffixes.rs renamed to src/test/ui/span/borrowck-let-suggestion-suffixes.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ fn f() {
1717
let young = ['y']; // statement 3
1818

1919
v2.push(&young[0]); // statement 4
20-
//~^ ERROR `young[..]` does not live long enough
21-
//~| NOTE does not live long enough
22-
//~| NOTE values in a scope are dropped in the opposite order they are created
20+
//~^ NOTE borrow occurs here
2321

2422
let mut v3 = Vec::new(); // statement 5
2523

@@ -52,7 +50,9 @@ fn f() {
5250

5351
v1.push(&old[0]);
5452
}
55-
//~^ NOTE borrowed value dropped before borrower
53+
//~^ ERROR `young[..]` does not live long enough
54+
//~| NOTE `young[..]` dropped here while still borrowed
55+
//~| NOTE values in a scope are dropped in the opposite order they are created
5656
//~| NOTE temporary value needs to live until here
5757
//~| NOTE temporary value needs to live until here
5858

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
error: `young[..]` does not live long enough
2+
--> $DIR/borrowck-let-suggestion-suffixes.rs:52:1
3+
|
4+
19 | v2.push(&young[0]); // statement 4
5+
| -------- borrow occurs here
6+
...
7+
52 | }
8+
| ^ `young[..]` dropped here while still borrowed
9+
|
10+
= note: values in a scope are dropped in the opposite order they are created
11+
12+
error: borrowed value does not live long enough
13+
--> $DIR/borrowck-let-suggestion-suffixes.rs:24:14
14+
|
15+
24 | v3.push(&'x'); // statement 6
16+
| ^^^ - temporary value only lives until here
17+
| |
18+
| temporary value created here
19+
...
20+
52 | }
21+
| - temporary value needs to live until here
22+
|
23+
= note: consider using a `let` binding to increase its lifetime
24+
25+
error: borrowed value does not live long enough
26+
--> $DIR/borrowck-let-suggestion-suffixes.rs:34:18
27+
|
28+
34 | v4.push(&'y');
29+
| ^^^ - temporary value only lives until here
30+
| |
31+
| temporary value created here
32+
...
33+
40 | } // (statement 7)
34+
| - temporary value needs to live until here
35+
|
36+
= note: consider using a `let` binding to increase its lifetime
37+
38+
error: borrowed value does not live long enough
39+
--> $DIR/borrowck-let-suggestion-suffixes.rs:45:14
40+
|
41+
45 | v5.push(&'z');
42+
| ^^^ - temporary value only lives until here
43+
| |
44+
| temporary value created here
45+
...
46+
52 | }
47+
| - temporary value needs to live until here
48+
|
49+
= note: consider using a `let` binding to increase its lifetime
50+
51+
error: aborting due to 4 previous errors
52+

src/test/compile-fail/dropck-object-cycle.rs renamed to src/test/ui/span/dropck-object-cycle.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'t> MakerTrait for Box<Trait<'t>+'static> {
3535
pub fn main() {
3636
let m : Box<Trait+'static> = make_val();
3737
assert_eq!(object_invoke1(&*m), (4,5));
38-
//~^ ERROR `*m` does not live long enough
38+
//~^ NOTE borrow occurs here
3939

4040
// the problem here is that the full type of `m` is
4141
//
@@ -55,3 +55,7 @@ pub fn main() {
5555
// the type of `m` *strictly outlives* `'m`. Hence we get an
5656
// error.
5757
}
58+
//~^ ERROR `*m` does not live long enough
59+
//~| NOTE `*m` dropped here while still borrowed
60+
//~| NOTE values in a scope are dropped in the opposite order they are created
61+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: `*m` does not live long enough
2+
--> $DIR/dropck-object-cycle.rs:57:1
3+
|
4+
37 | assert_eq!(object_invoke1(&*m), (4,5));
5+
| -- borrow occurs here
6+
...
7+
57 | }
8+
| ^ `*m` dropped here while still borrowed
9+
|
10+
= note: values in a scope are dropped in the opposite order they are created
11+
12+
error: aborting due to previous error
13+

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,19 @@ fn f() {
100100
b1 = B::new();
101101
b2 = B::new();
102102
b3 = B::new();
103-
b1.a[0].v.set(Some(&b2)); //~ ERROR `b2` does not live long enough
104-
b1.a[1].v.set(Some(&b3)); //~ ERROR `b3` does not live long enough
105-
b2.a[0].v.set(Some(&b2)); //~ ERROR `b2` does not live long enough
106-
b2.a[1].v.set(Some(&b3)); //~ ERROR `b3` does not live long enough
107-
b3.a[0].v.set(Some(&b1)); //~ ERROR `b1` does not live long enough
108-
b3.a[1].v.set(Some(&b2)); //~ ERROR `b2` does not live long enough
103+
b1.a[0].v.set(Some(&b2));
104+
b1.a[1].v.set(Some(&b3));
105+
b2.a[0].v.set(Some(&b2));
106+
b2.a[1].v.set(Some(&b3));
107+
b3.a[0].v.set(Some(&b1));
108+
b3.a[1].v.set(Some(&b2));
109109
}
110+
//~^ ERROR `b2` does not live long enough
111+
//~| ERROR `b3` does not live long enough
112+
//~| ERROR `b2` does not live long enough
113+
//~| ERROR `b3` does not live long enough
114+
//~| ERROR `b1` does not live long enough
115+
//~| ERROR `b2` does not live long enough
110116

111117
fn main() {
112118
f();

0 commit comments

Comments
 (0)