diff --git a/src/librustc_mir/borrow_check/error_reporting.rs b/src/librustc_mir/borrow_check/error_reporting.rs index 12f7f8cd8f8b..4354c16f73b7 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 b3299b943ba5..25bc702764a3 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> { &self.borrows } + pub fn scope_tree(&self) -> &Rc { &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 000000000000..02c4dd7cb21d --- /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 or the MIT license +// , 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 000000000000..ab35874fbb96 --- /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 +