Skip to content

Commit 15b8fbd

Browse files
committed
Added test for #46472
1 parent d78e8a7 commit 15b8fbd

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
324324
borrows: &Borrows<'cx, 'gcx, 'tcx>
325325
) {
326326
let end_span = borrows.opt_region_end_span(&borrow.region);
327+
let scope_tree = borrows.scope_tree();
327328
let root_place = self.prefixes(&borrow.place, PrefixSet::All).last().unwrap();
328329

329330
match root_place {
@@ -359,7 +360,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
359360
borrow_span, &format!("`{}`", description), Origin::Mir);
360361
err.span_label(borrow_span, "does not live long enough");
361362
err.span_label(drop_span, "borrowed value only lives until here");
362-
err.note("borrowed value must be valid for the static lifetime...");
363+
self.tcx.note_and_explain_region(scope_tree, &mut err,
364+
"borrowed value must be valid for ",
365+
borrow.region, "...");
363366
err.emit();
364367
},
365368
None => {

src/librustc_mir/dataflow/impls/borrows.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
160160

161161
pub fn borrows(&self) -> &IndexVec<BorrowIndex, BorrowData<'tcx>> { &self.borrows }
162162

163+
pub fn scope_tree(&self) -> &Rc<region::ScopeTree> { &self.scope_tree }
164+
163165
pub fn location(&self, idx: BorrowIndex) -> &Location {
164166
&self.borrows[idx].location
165167
}

src/test/ui/issue-46472.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z emit-end-regions -Z borrowck=compare
12+
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+
}
18+
19+
fn main() { }

src/test/ui/issue-46472.stderr

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error[E0597]: borrowed value does not live long enough (Ast)
2+
--> $DIR/issue-46472.rs:14:10
3+
|
4+
14 | &mut 4
5+
| ^ does not live long enough
6+
...
7+
17 | }
8+
| - temporary value only lives until here
9+
|
10+
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 13:1...
11+
--> $DIR/issue-46472.rs:13:1
12+
|
13+
13 | / fn bar<'a>() -> &'a mut u32 {
14+
14 | | &mut 4
15+
15 | | //~^ ERROR borrowed value does not live long enough (Ast) [E0597]
16+
16 | | //~| ERROR borrowed value does not live long enough (Mir) [E0597]
17+
17 | | }
18+
| |_^
19+
20+
error[E0597]: borrowed value does not live long enough (Mir)
21+
--> $DIR/issue-46472.rs:14:10
22+
|
23+
14 | &mut 4
24+
| ^ does not live long enough
25+
...
26+
17 | }
27+
| - temporary value only lives until here
28+
|
29+
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 13:1...
30+
--> $DIR/issue-46472.rs:13:1
31+
|
32+
13 | / fn bar<'a>() -> &'a mut u32 {
33+
14 | | &mut 4
34+
15 | | //~^ ERROR borrowed value does not live long enough (Ast) [E0597]
35+
16 | | //~| ERROR borrowed value does not live long enough (Mir) [E0597]
36+
17 | | }
37+
| |_^
38+
39+
error: aborting due to 2 previous errors
40+

0 commit comments

Comments
 (0)