]> git.lizzy.rs Git - rust.git/commitdiff
Improve 'dropped here' note.
authorDavid Wood <david@davidtw.co>
Fri, 14 Sep 2018 21:13:24 +0000 (23:13 +0200)
committerDavid Wood <david@davidtw.co>
Sun, 23 Sep 2018 12:18:34 +0000 (14:18 +0200)
Start mentioning function name that the variable is valid within in
notes to provide context.

src/librustc_mir/borrow_check/error_reporting.rs
src/test/ui/issues/issue-30438-c.nll.stderr
src/test/ui/nll/borrowed-universal-error-2.stderr
src/test/ui/nll/issue-52534-1.stderr
src/test/ui/nll/issue-52534.stderr
src/test/ui/regions/regions-nested-fns-2.nll.stderr
src/test/ui/regions/regions-nested-fns.nll.stderr

index 600d052cafb737a980731c7846e14a2923cdcc73..6fe9087370b06f81b9df4b8f4fe154c2bd0ea725 100644 (file)
@@ -519,7 +519,23 @@ fn report_local_value_does_not_live_long_enough(
                 borrow_span,
                 format!("`{}` would have to be valid for `{}`", name, region_name)
             );
-            err.span_label(drop_span, format!("but `{}` dropped here while still borrowed", name));
+
+            if let Some(fn_node_id) = self.infcx.tcx.hir.as_local_node_id(self.mir_def_id) {
+                err.span_label(
+                    drop_span,
+                    format!(
+                        "...but `{}` is only valid for the duration of the `{}` function, so it \
+                         is dropped here while still borrowed",
+                        name,
+                        self.infcx.tcx.hir.name(fn_node_id),
+                    )
+                );
+            } else {
+                err.span_label(
+                    drop_span,
+                    format!("...but `{}` dropped here while still borrowed", name)
+                );
+            }
 
             if let BorrowExplanation::MustBeValidFor(..) = explanation { } else {
                 explanation.emit(self.infcx.tcx, &mut err);
index 6d8a750d3d06dfc13dce7f06e0beffadeb228f7f..11dbe5fcaca38253a85cba5357510ff19c30b5c7 100644 (file)
@@ -10,7 +10,7 @@ LL |     &x
    |     ^^ `x` would have to be valid for `'y`
 LL |     //~^ ERROR: `x` does not live long enough
 LL | }
-   | - but `x` dropped here while still borrowed
+   | - ...but `x` is only valid for the duration of the `silly` function, so it is dropped here while still borrowed
 
 error: aborting due to previous error
 
index b414c399884a3ddcad9414cc50f865e60d97249e..c8b473d2b2ccc01f29b077b68be80e1ae071db07 100644 (file)
@@ -10,7 +10,7 @@ LL |     &v
    |     ^^ `v` would have to be valid for `'a`
 LL |     //~^ ERROR `v` does not live long enough [E0597]
 LL | }
-   | - but `v` dropped here while still borrowed
+   | - ...but `v` is only valid for the duration of the `foo` function, so it is dropped here while still borrowed
 
 error: aborting due to previous error
 
index 7b89336dabf220c0343f398e46e8159a0ad4f950..233ed47163056446feec9e6e9216a2e299ce6ba1 100644 (file)
@@ -9,7 +9,7 @@ LL |         let x = 22;
 LL |         &x
    |         ^^ `x` would have to be valid for `'0`
 LL |     }
-   |     - but `x` dropped here while still borrowed
+   |     - ...but `x` is only valid for the duration of the `bar` function, so it is dropped here while still borrowed
 
 error[E0597]: `x` does not live long enough
   --> $DIR/issue-52534-1.rs:25:5
@@ -22,7 +22,7 @@ LL |     let x = 22;
 LL |     &x
    |     ^^ `x` would have to be valid for `'0`
 LL | }
-   | - but `x` dropped here while still borrowed
+   | - ...but `x` is only valid for the duration of the `foo` function, so it is dropped here while still borrowed
 
 error[E0597]: `x` does not live long enough
   --> $DIR/issue-52534-1.rs:30:6
@@ -35,7 +35,7 @@ LL |     let x = 22;
 LL |     &&x
    |      ^^ `x` would have to be valid for `'0`
 LL | }
-   | - but `x` dropped here while still borrowed
+   | - ...but `x` is only valid for the duration of the `baz` function, so it is dropped here while still borrowed
 
 error[E0597]: borrowed value does not live long enough
   --> $DIR/issue-52534-1.rs:30:6
index c92a4230e1e9dec19b96d3be8146947917951787..032aa218d4a8f9746282e9eb6d4136f52c1988d0 100644 (file)
@@ -6,7 +6,7 @@ LL |     foo(|a| &x)
    |           |
    |           has type `&'0 u32`
 LL | }
-   | - but `x` dropped here while still borrowed
+   | - ...but `x` is only valid for the duration of the `bar` function, so it is dropped here while still borrowed
 
 error: aborting due to previous error
 
index 3f64806cc0fa77178f1ba764f7b1a4e070a0a329..90e0f1b7e07f8c72c0b1620f6be966197549e456 100644 (file)
@@ -8,7 +8,7 @@ LL |             if false { &y } else { z }
    |                         ^ `y` would have to be valid for `'0`
 LL |         });
 LL | }
-   | - but `y` dropped here while still borrowed
+   | - ...but `y` is only valid for the duration of the `nested` function, so it is dropped here while still borrowed
 
 error: aborting due to previous error
 
index eab06e8b90f83c5f43dce3e745dd08f3bca6d5c8..e8309beb0ae87f4ca6daf72f45eff3d1747e73a8 100644 (file)
@@ -31,7 +31,7 @@ LL |         ay = &y;
    |               ^ `y` would have to be valid for `'0`
 ...
 LL | }
-   | - but `y` dropped here while still borrowed
+   | - ...but `y` is only valid for the duration of the `nested` function, so it is dropped here while still borrowed
 
 error: unsatisfied lifetime constraints
   --> $DIR/regions-nested-fns.rs:23:68