mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-01 06:51:58 +00:00
Fixed case where borrowed value lives until after scope
This commit is contained in:
parent
52442d4d8a
commit
baf68d3a37
@ -356,14 +356,30 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
|
|||||||
|
|
||||||
match &self.describe_place(&borrow.place) {
|
match &self.describe_place(&borrow.place) {
|
||||||
Some(description) => {
|
Some(description) => {
|
||||||
let mut err = self.tcx.path_does_not_live_long_enough(
|
match borrow.region {
|
||||||
borrow_span, &format!("`{}`", description), Origin::Mir);
|
RegionKind::ReScope(_) => {
|
||||||
err.span_label(borrow_span, "does not live long enough");
|
let mut err = self.tcx.path_does_not_live_long_enough(
|
||||||
err.span_label(drop_span, "borrowed value only lives until here");
|
drop_span, &format!("`{}`", description), Origin::Mir);
|
||||||
self.tcx.note_and_explain_region(scope_tree, &mut err,
|
err.span_label(borrow_span, "borrow occurs here");
|
||||||
"borrowed value must be valid for ",
|
err.span_label(drop_span,
|
||||||
borrow.region, "...");
|
format!("`{}` dropped here while still borrowed",
|
||||||
err.emit();
|
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 => {
|
None => {
|
||||||
match borrow.region {
|
match borrow.region {
|
||||||
|
21
src/test/ui/issue-46471-1.rs
Normal file
21
src/test/ui/issue-46471-1.rs
Normal file
@ -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);
|
||||||
|
}
|
24
src/test/ui/issue-46471-1.stderr
Normal file
24
src/test/ui/issue-46471-1.stderr
Normal file
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user