about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Wood <david@davidtw.co>2017-12-10 20:36:42 +0000
committerDavid Wood <david@davidtw.co>2017-12-11 20:41:27 +0000
commitbaf68d3a3756b040aae2028f8d5a7a0a239458ef (patch)
treeaed63fb9c75e0ff854c76dad22701c2bff6da3da
parent52442d4d8a04f24735f9de120579ba3d997b811b (diff)
downloadrust-baf68d3a3756b040aae2028f8d5a7a0a239458ef.tar.gz
rust-baf68d3a3756b040aae2028f8d5a7a0a239458ef.zip
Fixed case where borrowed value lives until after scope
-rw-r--r--src/librustc_mir/borrow_check/error_reporting.rs32
-rw-r--r--src/test/ui/issue-46471-1.rs21
-rw-r--r--src/test/ui/issue-46471-1.stderr24
3 files changed, 69 insertions, 8 deletions
diff --git a/src/librustc_mir/borrow_check/error_reporting.rs b/src/librustc_mir/borrow_check/error_reporting.rs
index 33a6588c83b..a8feaa9ca94 100644
--- a/src/librustc_mir/borrow_check/error_reporting.rs
+++ b/src/librustc_mir/borrow_check/error_reporting.rs
@@ -356,14 +356,30 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
 
         match &self.describe_place(&borrow.place) {
             Some(description) => {
-                let mut err = self.tcx.path_does_not_live_long_enough(
-                    borrow_span, &format!("`{}`", description), Origin::Mir);
-                err.span_label(borrow_span, "does not live long enough");
-                err.span_label(drop_span, "borrowed value only lives until here");
-                self.tcx.note_and_explain_region(scope_tree, &mut err,
-                                                 "borrowed value must be valid for ",
-                                                 borrow.region, "...");
-                err.emit();
+                match borrow.region {
+                    RegionKind::ReScope(_) => {
+                        let mut err = self.tcx.path_does_not_live_long_enough(
+                            drop_span, &format!("`{}`", description), Origin::Mir);
+                        err.span_label(borrow_span, "borrow occurs here");
+                        err.span_label(drop_span,
+                                       format!("`{}` dropped here while still borrowed",
+                                               description));
+                        if let Some(end) = end_span {
+                            err.span_label(end, "borrowed value needs to live until here");
+                        }
+                        err.emit();
+                    },
+                    _ => {
+                        let mut err = self.tcx.path_does_not_live_long_enough(
+                            borrow_span, &format!("`{}`", description), Origin::Mir);
+                        err.span_label(borrow_span, "does not live long enough");
+                        err.span_label(drop_span, "borrowed value only lives until here");
+                        self.tcx.note_and_explain_region(scope_tree, &mut err,
+                                                         "borrowed value must be valid for ",
+                                                         borrow.region, "...");
+                        err.emit();
+                    }
+                }
             },
             None => {
                 match borrow.region {
diff --git a/src/test/ui/issue-46471-1.rs b/src/test/ui/issue-46471-1.rs
new file mode 100644
index 00000000000..977ea785fe6
--- /dev/null
+++ b/src/test/ui/issue-46471-1.rs
@@ -0,0 +1,21 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags: -Z emit-end-regions -Z borrowck=compare
+
+fn main() {
+    let y = {
+        let mut z = 0;
+        &mut z
+    };
+    //~^ ERROR `z` does not live long enough (Ast) [E0597]
+    //~| ERROR `z` does not live long enough (Mir) [E0597]
+    println!("{}", y);
+}
diff --git a/src/test/ui/issue-46471-1.stderr b/src/test/ui/issue-46471-1.stderr
new file mode 100644
index 00000000000..c33b9a7ba7b
--- /dev/null
+++ b/src/test/ui/issue-46471-1.stderr
@@ -0,0 +1,24 @@
+error[E0597]: `z` does not live long enough (Ast)
+  --> $DIR/issue-46471-1.rs:17:5
+   |
+16 |         &mut z
+   |              - borrow occurs here
+17 |     };
+   |     ^ `z` dropped here while still borrowed
+...
+21 | }
+   | - borrowed value needs to live until here
+
+error[E0597]: `z` does not live long enough (Mir)
+  --> $DIR/issue-46471-1.rs:17:6
+   |
+16 |         &mut z
+   |         ------ borrow occurs here
+17 |     };
+   |      ^ `z` dropped here while still borrowed
+...
+21 | }
+   | - borrowed value needs to live until here
+
+error: aborting due to 2 previous errors
+