summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorDavid Wood <david@davidtw.co>2017-12-10 17:00:20 +0000
committerDavid Wood <david@davidtw.co>2017-12-11 20:26:06 +0000
commit15b8fbdfb395315bb082eab793c4b02fa9534aee (patch)
treebc984f55c7b4fd4e8d2ce452da4936bfe40813da /src
parentd78e8a730a266ce75ff1fc0f9cdc66716de78744 (diff)
downloadrust-15b8fbdfb395315bb082eab793c4b02fa9534aee.tar.gz
rust-15b8fbdfb395315bb082eab793c4b02fa9534aee.zip
Added test for #46472
Diffstat (limited to 'src')
-rw-r--r--src/librustc_mir/borrow_check/error_reporting.rs5
-rw-r--r--src/librustc_mir/dataflow/impls/borrows.rs2
-rw-r--r--src/test/ui/issue-46472.rs19
-rw-r--r--src/test/ui/issue-46472.stderr40
4 files changed, 65 insertions, 1 deletions
diff --git a/src/librustc_mir/borrow_check/error_reporting.rs b/src/librustc_mir/borrow_check/error_reporting.rs
index 12f7f8cd8f8..4354c16f73b 100644
--- a/src/librustc_mir/borrow_check/error_reporting.rs
+++ b/src/librustc_mir/borrow_check/error_reporting.rs
@@ -324,6 +324,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
         borrows: &Borrows<'cx, 'gcx, 'tcx>
     ) {
         let end_span = borrows.opt_region_end_span(&borrow.region);
+        let scope_tree = borrows.scope_tree();
         let root_place = self.prefixes(&borrow.place, PrefixSet::All).last().unwrap();
 
         match root_place {
@@ -359,7 +360,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
                     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");
-                err.note("borrowed value must be valid for the static lifetime...");
+                self.tcx.note_and_explain_region(scope_tree, &mut err,
+                                                 "borrowed value must be valid for ",
+                                                 borrow.region, "...");
                 err.emit();
             },
             None => {
diff --git a/src/librustc_mir/dataflow/impls/borrows.rs b/src/librustc_mir/dataflow/impls/borrows.rs
index b3299b943ba..25bc702764a 100644
--- a/src/librustc_mir/dataflow/impls/borrows.rs
+++ b/src/librustc_mir/dataflow/impls/borrows.rs
@@ -160,6 +160,8 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
 
     pub fn borrows(&self) -> &IndexVec<BorrowIndex, BorrowData<'tcx>> { &self.borrows }
 
+    pub fn scope_tree(&self) -> &Rc<region::ScopeTree> { &self.scope_tree }
+
     pub fn location(&self, idx: BorrowIndex) -> &Location {
         &self.borrows[idx].location
     }
diff --git a/src/test/ui/issue-46472.rs b/src/test/ui/issue-46472.rs
new file mode 100644
index 00000000000..02c4dd7cb21
--- /dev/null
+++ b/src/test/ui/issue-46472.rs
@@ -0,0 +1,19 @@
+// 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 bar<'a>() -> &'a mut u32 {
+    &mut 4
+    //~^ ERROR borrowed value does not live long enough (Ast) [E0597]
+    //~| ERROR borrowed value does not live long enough (Mir) [E0597]
+}
+
+fn main() { }
diff --git a/src/test/ui/issue-46472.stderr b/src/test/ui/issue-46472.stderr
new file mode 100644
index 00000000000..ab35874fbb9
--- /dev/null
+++ b/src/test/ui/issue-46472.stderr
@@ -0,0 +1,40 @@
+error[E0597]: borrowed value does not live long enough (Ast)
+  --> $DIR/issue-46472.rs:14:10
+   |
+14 |     &mut 4
+   |          ^ does not live long enough
+...
+17 | }
+   | - temporary value only lives until here
+   |
+note: borrowed value must be valid for the lifetime 'a as defined on the function body at 13:1...
+  --> $DIR/issue-46472.rs:13:1
+   |
+13 | / fn bar<'a>() -> &'a mut u32 {
+14 | |     &mut 4
+15 | |     //~^ ERROR borrowed value does not live long enough (Ast) [E0597]
+16 | |     //~| ERROR borrowed value does not live long enough (Mir) [E0597]
+17 | | }
+   | |_^
+
+error[E0597]: borrowed value does not live long enough (Mir)
+  --> $DIR/issue-46472.rs:14:10
+   |
+14 |     &mut 4
+   |          ^ does not live long enough
+...
+17 | }
+   |  - temporary value only lives until here
+   |
+note: borrowed value must be valid for the lifetime 'a as defined on the function body at 13:1...
+  --> $DIR/issue-46472.rs:13:1
+   |
+13 | / fn bar<'a>() -> &'a mut u32 {
+14 | |     &mut 4
+15 | |     //~^ ERROR borrowed value does not live long enough (Ast) [E0597]
+16 | |     //~| ERROR borrowed value does not live long enough (Mir) [E0597]
+17 | | }
+   | |_^
+
+error: aborting due to 2 previous errors
+