Skip to content

Commit baf68d3

Browse files
committed
Fixed case where borrowed value lives until after scope
1 parent 52442d4 commit baf68d3

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,30 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
356356

357357
match &self.describe_place(&borrow.place) {
358358
Some(description) => {
359-
let mut err = self.tcx.path_does_not_live_long_enough(
360-
borrow_span, &format!("`{}`", description), Origin::Mir);
361-
err.span_label(borrow_span, "does not live long enough");
362-
err.span_label(drop_span, "borrowed value only lives until here");
363-
self.tcx.note_and_explain_region(scope_tree, &mut err,
364-
"borrowed value must be valid for ",
365-
borrow.region, "...");
366-
err.emit();
359+
match borrow.region {
360+
RegionKind::ReScope(_) => {
361+
let mut err = self.tcx.path_does_not_live_long_enough(
362+
drop_span, &format!("`{}`", description), Origin::Mir);
363+
err.span_label(borrow_span, "borrow occurs here");
364+
err.span_label(drop_span,
365+
format!("`{}` dropped here while still borrowed",
366+
description));
367+
if let Some(end) = end_span {
368+
err.span_label(end, "borrowed value needs to live until here");
369+
}
370+
err.emit();
371+
},
372+
_ => {
373+
let mut err = self.tcx.path_does_not_live_long_enough(
374+
borrow_span, &format!("`{}`", description), Origin::Mir);
375+
err.span_label(borrow_span, "does not live long enough");
376+
err.span_label(drop_span, "borrowed value only lives until here");
377+
self.tcx.note_and_explain_region(scope_tree, &mut err,
378+
"borrowed value must be valid for ",
379+
borrow.region, "...");
380+
err.emit();
381+
}
382+
}
367383
},
368384
None => {
369385
match borrow.region {

src/test/ui/issue-46471-1.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 main() {
14+
let y = {
15+
let mut z = 0;
16+
&mut z
17+
};
18+
//~^ ERROR `z` does not live long enough (Ast) [E0597]
19+
//~| ERROR `z` does not live long enough (Mir) [E0597]
20+
println!("{}", y);
21+
}

src/test/ui/issue-46471-1.stderr

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0597]: `z` does not live long enough (Ast)
2+
--> $DIR/issue-46471-1.rs:17:5
3+
|
4+
16 | &mut z
5+
| - borrow occurs here
6+
17 | };
7+
| ^ `z` dropped here while still borrowed
8+
...
9+
21 | }
10+
| - borrowed value needs to live until here
11+
12+
error[E0597]: `z` does not live long enough (Mir)
13+
--> $DIR/issue-46471-1.rs:17:6
14+
|
15+
16 | &mut z
16+
| ------ borrow occurs here
17+
17 | };
18+
| ^ `z` dropped here while still borrowed
19+
...
20+
21 | }
21+
| - borrowed value needs to live until here
22+
23+
error: aborting due to 2 previous errors
24+

0 commit comments

Comments
 (0)