Skip to content

Commit

Permalink
Tweak wording and spans of closure lifetime errors
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Jan 19, 2018
1 parent 1820da5 commit 6f9ecaa
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,46 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
sup_sp,
"borrowed data cannot be stored outside of its closure");
err.span_label(sup_sp, "cannot be stored outside of its closure");
if sup_sp == origin_sp {
if origin_sp == sup_sp || origin_sp.contains(sup_sp) {
// // sup_sp == origin.span():
//
// let mut x = None;
// ----- borrowed data cannot be stored into here...
// with_int(|y| x = Some(y));
// --- ^ cannot be stored outside of its closure
// |
// ...because it cannot outlive this closure
//
// // origin.contains(&sup_sp):
//
// let mut f: Option<&u32> = None;
// ----- borrowed data cannot be stored into here...
// closure_expecting_bound(|x: &'x u32| {
// ------------ ... because it cannot outlive this closure
// f = Some(x);
// ^ cannot be stored outside of its closure
err.span_label(*external_span,
"borrowed data cannot be stored into here...");
err.span_label(*closure_span,
"...because it cannot outlive this closure");
} else {
// FIXME: the wording for this case could be much improved
//
// let mut lines_to_use: Vec<&CrateId> = Vec::new();
// - cannot infer an appropriate lifetime...
// let push_id = |installed_id: &CrateId| {
// ------- ------------------------ borrowed data cannot outlive this closure
// |
// ...so that variable is valid at time of its declaration
// lines_to_use.push(installed_id);
// ^^^^^^^^^^^^ cannot be stored outside of its closure
err.span_label(origin_sp,
"cannot infer an appropriate lifetime...");
err.span_label(*external_span,
"...so that variable is valid at time of its \
declaration");
err.span_label(*closure_span,
"borrowed data cannot outlive this closure");
err.span_label(origin_sp,
"cannot infer an appropriate lifetime");
}
err.emit();
return Some(ErrorReported);
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/borrowck/issue-7573.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn remove_package_from_database() {
//~^ NOTE cannot infer an appropriate lifetime
let push_id = |installed_id: &CrateId| {
//~^ NOTE borrowed data cannot outlive this closure
//~| NOTE ...so that variable is valid at time of its declaration
lines_to_use.push(installed_id);
//~^ ERROR borrowed data cannot be stored outside of its closure
//~| NOTE cannot be stored outside of its closure
Expand Down
12 changes: 7 additions & 5 deletions src/test/ui/borrowck/issue-7573.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
error: borrowed data cannot be stored outside of its closure
--> $DIR/issue-7573.rs:31:27
--> $DIR/issue-7573.rs:32:27
|
27 | let mut lines_to_use: Vec<&CrateId> = Vec::new();
| - cannot infer an appropriate lifetime
| - cannot infer an appropriate lifetime...
28 | //~^ NOTE cannot infer an appropriate lifetime
29 | let push_id = |installed_id: &CrateId| {
| ------------------------ borrowed data cannot outlive this closure
30 | //~^ NOTE borrowed data cannot outlive this closure
31 | lines_to_use.push(installed_id);
| ------- ------------------------ borrowed data cannot outlive this closure
| |
| ...so that variable is valid at time of its declaration
...
32 | lines_to_use.push(installed_id);
| ^^^^^^^^^^^^ cannot be stored outside of its closure

error: aborting due to previous error
Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/borrowck/regions-escape-bound-fn.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
error: borrowed data cannot be stored outside of its closure
--> $DIR/regions-escape-bound-fn.rs:18:27
|
17 | let mut x: Option<&isize> = None;
| ----- borrowed data cannot be stored into here...
18 | with_int(|y| x = Some(y));
| --- -----^-
| | | |
| | | cannot be stored outside of its closure
| | cannot infer an appropriate lifetime
| borrowed data cannot outlive this closure
| --- ^ cannot be stored outside of its closure
| |
| ...because it cannot outlive this closure

error: aborting due to previous error

10 changes: 5 additions & 5 deletions src/test/ui/borrowck/regions-escape-unboxed-closure.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
error: borrowed data cannot be stored outside of its closure
--> $DIR/regions-escape-unboxed-closure.rs:16:32
|
15 | let mut x: Option<&isize> = None;
| ----- borrowed data cannot be stored into here...
16 | with_int(&mut |y| x = Some(y));
| --- -----^-
| | | |
| | | cannot be stored outside of its closure
| | cannot infer an appropriate lifetime
| borrowed data cannot outlive this closure
| --- ^ cannot be stored outside of its closure
| |
| ...because it cannot outlive this closure

error: aborting due to previous error

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

error: borrowed data cannot be stored outside of its closure
--> $DIR/expect-region-supply-region.rs:38:18
|
36 | let mut f: Option<&u32> = None;
| ----- borrowed data cannot be stored into here...
37 | closure_expecting_bound(|x: &u32| {
| --------- borrowed data cannot outlive this closure
| --------- ...because it cannot outlive this closure
38 | f = Some(x); //~ ERROR borrowed data cannot be stored outside of its closure
| -----^-
| | |
| | cannot be stored outside of its closure
| cannot infer an appropriate lifetime
| ^ cannot be stored outside of its closure

error[E0308]: mismatched types
--> $DIR/expect-region-supply-region.rs:47:33
Expand Down Expand Up @@ -87,14 +85,14 @@ note: ...does not necessarily outlive the anonymous lifetime #2 defined on the b
error: borrowed data cannot be stored outside of its closure
--> $DIR/expect-region-supply-region.rs:52:18
|
43 | let mut f: Option<&u32> = None;
| ----- borrowed data cannot be stored into here...
...
47 | closure_expecting_bound(|x: &'x u32| {
| ------------ borrowed data cannot outlive this closure
| ------------ ...because it cannot outlive this closure
...
52 | f = Some(x);
| -----^-
| | |
| | cannot be stored outside of its closure
| cannot infer an appropriate lifetime
| ^ cannot be stored outside of its closure

error: aborting due to 5 previous errors

0 comments on commit 6f9ecaa

Please sign in to comment.