Skip to content

Commit 6f9ecaa

Browse files
committed
Tweak wording and spans of closure lifetime errors
1 parent 1820da5 commit 6f9ecaa

File tree

6 files changed

+64
-33
lines changed

6 files changed

+64
-33
lines changed

src/librustc/infer/error_reporting/nice_region_error/outlives_closure.rs

+33-3
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,46 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
6969
sup_sp,
7070
"borrowed data cannot be stored outside of its closure");
7171
err.span_label(sup_sp, "cannot be stored outside of its closure");
72-
if sup_sp == origin_sp {
72+
if origin_sp == sup_sp || origin_sp.contains(sup_sp) {
73+
// // sup_sp == origin.span():
74+
//
75+
// let mut x = None;
76+
// ----- borrowed data cannot be stored into here...
77+
// with_int(|y| x = Some(y));
78+
// --- ^ cannot be stored outside of its closure
79+
// |
80+
// ...because it cannot outlive this closure
81+
//
82+
// // origin.contains(&sup_sp):
83+
//
84+
// let mut f: Option<&u32> = None;
85+
// ----- borrowed data cannot be stored into here...
86+
// closure_expecting_bound(|x: &'x u32| {
87+
// ------------ ... because it cannot outlive this closure
88+
// f = Some(x);
89+
// ^ cannot be stored outside of its closure
7390
err.span_label(*external_span,
7491
"borrowed data cannot be stored into here...");
7592
err.span_label(*closure_span,
7693
"...because it cannot outlive this closure");
7794
} else {
95+
// FIXME: the wording for this case could be much improved
96+
//
97+
// let mut lines_to_use: Vec<&CrateId> = Vec::new();
98+
// - cannot infer an appropriate lifetime...
99+
// let push_id = |installed_id: &CrateId| {
100+
// ------- ------------------------ borrowed data cannot outlive this closure
101+
// |
102+
// ...so that variable is valid at time of its declaration
103+
// lines_to_use.push(installed_id);
104+
// ^^^^^^^^^^^^ cannot be stored outside of its closure
105+
err.span_label(origin_sp,
106+
"cannot infer an appropriate lifetime...");
107+
err.span_label(*external_span,
108+
"...so that variable is valid at time of its \
109+
declaration");
78110
err.span_label(*closure_span,
79111
"borrowed data cannot outlive this closure");
80-
err.span_label(origin_sp,
81-
"cannot infer an appropriate lifetime");
82112
}
83113
err.emit();
84114
return Some(ErrorReported);

src/test/ui/borrowck/issue-7573.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub fn remove_package_from_database() {
2828
//~^ NOTE cannot infer an appropriate lifetime
2929
let push_id = |installed_id: &CrateId| {
3030
//~^ NOTE borrowed data cannot outlive this closure
31+
//~| NOTE ...so that variable is valid at time of its declaration
3132
lines_to_use.push(installed_id);
3233
//~^ ERROR borrowed data cannot be stored outside of its closure
3334
//~| NOTE cannot be stored outside of its closure

src/test/ui/borrowck/issue-7573.stderr

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
error: borrowed data cannot be stored outside of its closure
2-
--> $DIR/issue-7573.rs:31:27
2+
--> $DIR/issue-7573.rs:32:27
33
|
44
27 | let mut lines_to_use: Vec<&CrateId> = Vec::new();
5-
| - cannot infer an appropriate lifetime
5+
| - cannot infer an appropriate lifetime...
66
28 | //~^ NOTE cannot infer an appropriate lifetime
77
29 | let push_id = |installed_id: &CrateId| {
8-
| ------------------------ borrowed data cannot outlive this closure
9-
30 | //~^ NOTE borrowed data cannot outlive this closure
10-
31 | lines_to_use.push(installed_id);
8+
| ------- ------------------------ borrowed data cannot outlive this closure
9+
| |
10+
| ...so that variable is valid at time of its declaration
11+
...
12+
32 | lines_to_use.push(installed_id);
1113
| ^^^^^^^^^^^^ cannot be stored outside of its closure
1214

1315
error: aborting due to previous error
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
error: borrowed data cannot be stored outside of its closure
22
--> $DIR/regions-escape-bound-fn.rs:18:27
33
|
4+
17 | let mut x: Option<&isize> = None;
5+
| ----- borrowed data cannot be stored into here...
46
18 | with_int(|y| x = Some(y));
5-
| --- -----^-
6-
| | | |
7-
| | | cannot be stored outside of its closure
8-
| | cannot infer an appropriate lifetime
9-
| borrowed data cannot outlive this closure
7+
| --- ^ cannot be stored outside of its closure
8+
| |
9+
| ...because it cannot outlive this closure
1010

1111
error: aborting due to previous error
1212

Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
error: borrowed data cannot be stored outside of its closure
22
--> $DIR/regions-escape-unboxed-closure.rs:16:32
33
|
4+
15 | let mut x: Option<&isize> = None;
5+
| ----- borrowed data cannot be stored into here...
46
16 | with_int(&mut |y| x = Some(y));
5-
| --- -----^-
6-
| | | |
7-
| | | cannot be stored outside of its closure
8-
| | cannot infer an appropriate lifetime
9-
| borrowed data cannot outlive this closure
7+
| --- ^ cannot be stored outside of its closure
8+
| |
9+
| ...because it cannot outlive this closure
1010

1111
error: aborting due to previous error
1212

src/test/ui/closure-expected-type/expect-region-supply-region.stderr

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
error: borrowed data cannot be stored outside of its closure
22
--> $DIR/expect-region-supply-region.rs:28:18
33
|
4+
26 | let mut f: Option<&u32> = None;
5+
| ----- borrowed data cannot be stored into here...
46
27 | closure_expecting_bound(|x| {
5-
| --- borrowed data cannot outlive this closure
7+
| --- ...because it cannot outlive this closure
68
28 | f = Some(x); //~ ERROR borrowed data cannot be stored outside of its closure
7-
| -----^-
8-
| | |
9-
| | cannot be stored outside of its closure
10-
| cannot infer an appropriate lifetime
9+
| ^ cannot be stored outside of its closure
1110

1211
error: borrowed data cannot be stored outside of its closure
1312
--> $DIR/expect-region-supply-region.rs:38:18
1413
|
14+
36 | let mut f: Option<&u32> = None;
15+
| ----- borrowed data cannot be stored into here...
1516
37 | closure_expecting_bound(|x: &u32| {
16-
| --------- borrowed data cannot outlive this closure
17+
| --------- ...because it cannot outlive this closure
1718
38 | f = Some(x); //~ ERROR borrowed data cannot be stored outside of its closure
18-
| -----^-
19-
| | |
20-
| | cannot be stored outside of its closure
21-
| cannot infer an appropriate lifetime
19+
| ^ cannot be stored outside of its closure
2220

2321
error[E0308]: mismatched types
2422
--> $DIR/expect-region-supply-region.rs:47:33
@@ -87,14 +85,14 @@ note: ...does not necessarily outlive the anonymous lifetime #2 defined on the b
8785
error: borrowed data cannot be stored outside of its closure
8886
--> $DIR/expect-region-supply-region.rs:52:18
8987
|
88+
43 | let mut f: Option<&u32> = None;
89+
| ----- borrowed data cannot be stored into here...
90+
...
9091
47 | closure_expecting_bound(|x: &'x u32| {
91-
| ------------ borrowed data cannot outlive this closure
92+
| ------------ ...because it cannot outlive this closure
9293
...
9394
52 | f = Some(x);
94-
| -----^-
95-
| | |
96-
| | cannot be stored outside of its closure
97-
| cannot infer an appropriate lifetime
95+
| ^ cannot be stored outside of its closure
9896

9997
error: aborting due to 5 previous errors
10098

0 commit comments

Comments
 (0)