diff --git a/src/librustc/infer/error_reporting/nice_region_error/outlives_closure.rs b/src/librustc/infer/error_reporting/nice_region_error/outlives_closure.rs index f6a99579598ca..95f44b813c5d2 100644 --- a/src/librustc/infer/error_reporting/nice_region_error/outlives_closure.rs +++ b/src/librustc/infer/error_reporting/nice_region_error/outlives_closure.rs @@ -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); diff --git a/src/test/ui/borrowck/issue-7573.rs b/src/test/ui/borrowck/issue-7573.rs index b318bd70cf2d2..8f1545fa00986 100644 --- a/src/test/ui/borrowck/issue-7573.rs +++ b/src/test/ui/borrowck/issue-7573.rs @@ -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 diff --git a/src/test/ui/borrowck/issue-7573.stderr b/src/test/ui/borrowck/issue-7573.stderr index 6c549a029b602..99b48d9276c06 100644 --- a/src/test/ui/borrowck/issue-7573.stderr +++ b/src/test/ui/borrowck/issue-7573.stderr @@ -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 diff --git a/src/test/ui/borrowck/regions-escape-bound-fn.stderr b/src/test/ui/borrowck/regions-escape-bound-fn.stderr index 306da8b54e4f7..a2ad7c3f768c6 100644 --- a/src/test/ui/borrowck/regions-escape-bound-fn.stderr +++ b/src/test/ui/borrowck/regions-escape-bound-fn.stderr @@ -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 diff --git a/src/test/ui/borrowck/regions-escape-unboxed-closure.stderr b/src/test/ui/borrowck/regions-escape-unboxed-closure.stderr index 5e51be4629539..4b01e42fa67d0 100644 --- a/src/test/ui/borrowck/regions-escape-unboxed-closure.stderr +++ b/src/test/ui/borrowck/regions-escape-unboxed-closure.stderr @@ -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 diff --git a/src/test/ui/closure-expected-type/expect-region-supply-region.stderr b/src/test/ui/closure-expected-type/expect-region-supply-region.stderr index d34b17bb25b5f..ebb1e561e57ab 100644 --- a/src/test/ui/closure-expected-type/expect-region-supply-region.stderr +++ b/src/test/ui/closure-expected-type/expect-region-supply-region.stderr @@ -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 @@ -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