Skip to content

Point at var in short lived borrows instead of drop location #46719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
@@ -919,11 +919,9 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
}

let mut db = self.path_does_not_live_long_enough(error_span, &msg, Origin::Ast);
let (value_kind, value_msg) = match err.cmt.cat {
mc::Categorization::Rvalue(..) =>
("temporary value", "temporary value created here"),
_ =>
("borrowed value", "borrow occurs here")
let value_kind = match err.cmt.cat {
mc::Categorization::Rvalue(..) => "temporary value",
_ => "borrowed value",
};

let is_closure = match cause {
@@ -936,14 +934,16 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
Some(primary) => {
db.span = MultiSpan::from_span(s);
db.span_label(primary, "capture occurs here");
db.span_label(s, "does not live long enough");
db.span_label(s, format!("{} does not live long enough",
value_kind));
true
}
None => false
}
}
_ => {
db.span_label(error_span, "does not live long enough");
db.span_label(error_span, format!("{} does not live long enough",
value_kind));
false
}
};
@@ -954,8 +954,6 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
match (sub_span, super_span) {
(Some(s1), Some(s2)) if s1 == s2 => {
if !is_closure {
db.span = MultiSpan::from_span(s1);
db.span_label(error_span, value_msg);
let msg = match opt_loan_path(&err.cmt) {
None => value_kind.to_string(),
Some(lp) => {
@@ -971,8 +969,6 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
they are created");
}
(Some(s1), Some(s2)) if !is_closure => {
db.span = MultiSpan::from_span(s2);
db.span_label(error_span, value_msg);
let msg = match opt_loan_path(&err.cmt) {
None => value_kind.to_string(),
Some(lp) => {
14 changes: 7 additions & 7 deletions src/librustc_mir/borrow_check/error_reporting.rs
Original file line number Diff line number Diff line change
@@ -394,10 +394,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
&mut self, name: &String, _scope_tree: &Rc<ScopeTree>, _borrow: &BorrowData<'tcx>,
drop_span: Span, borrow_span: Span, _proper_span: Span, end_span: Option<Span>
) {
let mut err = self.tcx.path_does_not_live_long_enough(drop_span,
let mut err = self.tcx.path_does_not_live_long_enough(borrow_span,
&format!("`{}`", name),
Origin::Mir);
err.span_label(borrow_span, "borrow occurs here");
err.span_label(borrow_span, "borrowed value does not live long enough");
err.span_label(drop_span, format!("`{}` dropped here while still borrowed", name));
if let Some(end) = end_span {
err.span_label(end, "borrowed value needs to live until here");
@@ -407,12 +407,12 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {

fn report_scoped_temporary_value_does_not_live_long_enough(
&mut self, _scope_tree: &Rc<ScopeTree>, _borrow: &BorrowData<'tcx>,
drop_span: Span, borrow_span: Span, proper_span: Span, end_span: Option<Span>
drop_span: Span, _borrow_span: Span, proper_span: Span, end_span: Option<Span>
) {
let mut err = self.tcx.path_does_not_live_long_enough(borrow_span,
let mut err = self.tcx.path_does_not_live_long_enough(proper_span,
"borrowed value",
Origin::Mir);
err.span_label(proper_span, "temporary value created here");
err.span_label(proper_span, "temporary value does not live long enough");
err.span_label(drop_span, "temporary value dropped here while still borrowed");
err.note("consider using a `let` binding to increase its lifetime");
if let Some(end) = end_span {
@@ -428,7 +428,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
let mut err = self.tcx.path_does_not_live_long_enough(borrow_span,
&format!("`{}`", name),
Origin::Mir);
err.span_label(borrow_span, "does not live long enough");
err.span_label(borrow_span, "borrowed value 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 ",
@@ -443,7 +443,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
let mut err = self.tcx.path_does_not_live_long_enough(proper_span,
"borrowed value",
Origin::Mir);
err.span_label(proper_span, "does not live long enough");
err.span_label(proper_span, "temporary value does not live long enough");
err.span_label(drop_span, "temporary value only lives until here");
self.tcx.note_and_explain_region(scope_tree, &mut err,
"borrowed value must be valid for ",
Original file line number Diff line number Diff line change
@@ -124,4 +124,4 @@ fn f<'a>(arena: &'a TypedArena<C<'a>>) {
fn main() {
let arena = TypedArena::new();
f(&arena);
} //~ ERROR `arena` does not live long enough
} //~^ ERROR `arena` does not live long enough
Original file line number Diff line number Diff line change
@@ -49,5 +49,5 @@ fn f<'a>(_arena: &'a TypedArena<C<'a>>) {}
fn main() {
let arena: TypedArena<C> = TypedArena::new();
f(&arena);
} //~ ERROR `arena` does not live long enough
} //~^ ERROR `arena` does not live long enough

3 changes: 2 additions & 1 deletion src/test/compile-fail/E0597.rs
Original file line number Diff line number Diff line change
@@ -16,4 +16,5 @@ fn main() {
let mut x = Foo { x: None };
let y = 0;
x.x = Some(&y);
} //~ `y` does not live long enough [E0597]
//~^ `y` does not live long enough [E0597]
}
3 changes: 2 additions & 1 deletion src/test/compile-fail/catch-bad-lifetime.rs
Original file line number Diff line number Diff line change
@@ -18,10 +18,11 @@ pub fn main() {
let _result: Result<(), &str> = do catch {
let my_string = String::from("");
let my_str: & str = & my_string;
//~^ ERROR `my_string` does not live long enough
Err(my_str) ?;
Err("") ?;
Ok(())
}; //~ ERROR `my_string` does not live long enough
};
}

{
4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-36082.rs
Original file line number Diff line number Diff line change
@@ -21,11 +21,11 @@ fn main() {
let val: &_ = x.borrow().0;
//[ast]~^ ERROR borrowed value does not live long enough [E0597]
//[ast]~| NOTE temporary value dropped here while still borrowed
//[ast]~| NOTE temporary value created here
//[ast]~| NOTE temporary value does not live long enough
//[ast]~| NOTE consider using a `let` binding to increase its lifetime
//[mir]~^^^^^ ERROR borrowed value does not live long enough [E0597]
//[mir]~| NOTE temporary value dropped here while still borrowed
//[mir]~| NOTE temporary value created here
//[mir]~| NOTE temporary value does not live long enough
//[mir]~| NOTE consider using a `let` binding to increase its lifetime
println!("{}", val);
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/region-borrow-params-issue-29793-big.rs
Original file line number Diff line number Diff line change
@@ -81,9 +81,9 @@ fn main() {
WrapB::new().set(|t: bool| if t { x } else { y }) // (separate errors for `x` vs `y`)
//[ast]~^ ERROR `x` does not live long enough
//[ast]~| ERROR `y` does not live long enough
//[mir]~^^^ ERROR `x` does not live long enough
//[mir]~| ERROR `y` does not live long enough
});
//[mir]~^ ERROR `x` does not live long enough
//[mir]~| ERROR `y` does not live long enough

w.handle(); // This works
// w.handle_ref(); // This doesn't
9 changes: 5 additions & 4 deletions src/test/ui/dropck/dropck-eyepatch-extern-crate.rs
Original file line number Diff line number Diff line change
@@ -37,22 +37,23 @@ fn main() {
dr = Dr("dr", &c_long);
// Error: destructor order imprecisely modelled
dt = Dt("dt", &c);
//~^ ERROR `c` does not live long enough
dr = Dr("dr", &c);
//~^ ERROR `c` does not live long enough

// No error: Drop impl asserts .1 (A and &'a _) are not accessed
pt = Pt("pt", &c, &c_long);
pr = Pr("pr", &c, &c_long);

// Error: Drop impl's assertion does not apply to `B` nor `&'b _`
pt = Pt("pt", &c_long, &c);
//~^ ERROR `c` does not live long enough
pr = Pr("pr", &c_long, &c);
//~^ ERROR `c` does not live long enough

// No error: St and Sr have no destructor.
st = St("st", &c);
sr = Sr("sr", &c);

println!("{:?}", (dt.0, dr.0, pt.0, pr.0, st.0, sr.0));
}//~ ERROR `c` does not live long enough
//~^ ERROR `c` does not live long enough
//~| ERROR `c` does not live long enough
//~| ERROR `c` does not live long enough
}
38 changes: 19 additions & 19 deletions src/test/ui/dropck/dropck-eyepatch-extern-crate.stderr
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
error[E0597]: `c` does not live long enough
--> $DIR/dropck-eyepatch-extern-crate.rs:55:1
--> $DIR/dropck-eyepatch-extern-crate.rs:39:20
|
39 | dt = Dt("dt", &c);
| - borrow occurs here
| ^ borrowed value does not live long enough
...
55 | }//~ ERROR `c` does not live long enough
| ^ `c` dropped here while still borrowed
59 | }
| - `c` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created

error[E0597]: `c` does not live long enough
--> $DIR/dropck-eyepatch-extern-crate.rs:55:1
--> $DIR/dropck-eyepatch-extern-crate.rs:41:20
|
40 | dr = Dr("dr", &c);
| - borrow occurs here
41 | dr = Dr("dr", &c);
| ^ borrowed value does not live long enough
...
55 | }//~ ERROR `c` does not live long enough
| ^ `c` dropped here while still borrowed
59 | }
| - `c` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created

error[E0597]: `c` does not live long enough
--> $DIR/dropck-eyepatch-extern-crate.rs:55:1
--> $DIR/dropck-eyepatch-extern-crate.rs:49:29
|
47 | pt = Pt("pt", &c_long, &c);
| - borrow occurs here
49 | pt = Pt("pt", &c_long, &c);
| ^ borrowed value does not live long enough
...
55 | }//~ ERROR `c` does not live long enough
| ^ `c` dropped here while still borrowed
59 | }
| - `c` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created

error[E0597]: `c` does not live long enough
--> $DIR/dropck-eyepatch-extern-crate.rs:55:1
--> $DIR/dropck-eyepatch-extern-crate.rs:51:29
|
48 | pr = Pr("pr", &c_long, &c);
| - borrow occurs here
51 | pr = Pr("pr", &c_long, &c);
| ^ borrowed value does not live long enough
...
55 | }//~ ERROR `c` does not live long enough
| ^ `c` dropped here while still borrowed
59 | }
| - `c` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created

8 changes: 4 additions & 4 deletions src/test/ui/dropck/dropck-eyepatch-reorder.rs
Original file line number Diff line number Diff line change
@@ -55,23 +55,23 @@ fn main() {
dr = Dr("dr", &c_long);
// Error: destructor order imprecisely modelled
dt = Dt("dt", &c);
//~^ ERROR `c` does not live long enough
dr = Dr("dr", &c);
//~^ ERROR `c` does not live long enough

// No error: Drop impl asserts .1 (A and &'a _) are not accessed
pt = Pt("pt", &c, &c_long);
pr = Pr("pr", &c, &c_long);

// Error: Drop impl's assertion does not apply to `B` nor `&'b _`
pt = Pt("pt", &c_long, &c);
//~^ ERROR `c` does not live long enough
pr = Pr("pr", &c_long, &c);
//~^ ERROR `c` does not live long enough

// No error: St and Sr have no destructor.
st = St("st", &c);
sr = Sr("sr", &c);

println!("{:?}", (dt.0, dr.0, pt.0, pr.0, st.0, sr.0));
}
//~^ ERROR `c` does not live long enough
//~| ERROR `c` does not live long enough
//~| ERROR `c` does not live long enough
//~| ERROR `c` does not live long enough
38 changes: 19 additions & 19 deletions src/test/ui/dropck/dropck-eyepatch-reorder.stderr
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
error[E0597]: `c` does not live long enough
--> $DIR/dropck-eyepatch-reorder.rs:73:1
--> $DIR/dropck-eyepatch-reorder.rs:57:20
|
57 | dt = Dt("dt", &c);
| - borrow occurs here
| ^ borrowed value does not live long enough
...
73 | }
| ^ `c` dropped here while still borrowed
77 | }
| - `c` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created

error[E0597]: `c` does not live long enough
--> $DIR/dropck-eyepatch-reorder.rs:73:1
--> $DIR/dropck-eyepatch-reorder.rs:59:20
|
58 | dr = Dr("dr", &c);
| - borrow occurs here
59 | dr = Dr("dr", &c);
| ^ borrowed value does not live long enough
...
73 | }
| ^ `c` dropped here while still borrowed
77 | }
| - `c` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created

error[E0597]: `c` does not live long enough
--> $DIR/dropck-eyepatch-reorder.rs:73:1
--> $DIR/dropck-eyepatch-reorder.rs:67:29
|
65 | pt = Pt("pt", &c_long, &c);
| - borrow occurs here
67 | pt = Pt("pt", &c_long, &c);
| ^ borrowed value does not live long enough
...
73 | }
| ^ `c` dropped here while still borrowed
77 | }
| - `c` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created

error[E0597]: `c` does not live long enough
--> $DIR/dropck-eyepatch-reorder.rs:73:1
--> $DIR/dropck-eyepatch-reorder.rs:69:29
|
66 | pr = Pr("pr", &c_long, &c);
| - borrow occurs here
69 | pr = Pr("pr", &c_long, &c);
| ^ borrowed value does not live long enough
...
73 | }
| ^ `c` dropped here while still borrowed
77 | }
| - `c` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created

8 changes: 4 additions & 4 deletions src/test/ui/dropck/dropck-eyepatch.rs
Original file line number Diff line number Diff line change
@@ -78,23 +78,23 @@ fn main() {
dr = Dr("dr", &c_long);
// Error: destructor order imprecisely modelled
dt = Dt("dt", &c);
//~^ ERROR `c` does not live long enough
dr = Dr("dr", &c);
//~^ ERROR `c` does not live long enough

// No error: Drop impl asserts .1 (A and &'a _) are not accessed
pt = Pt("pt", &c, &c_long);
pr = Pr("pr", &c, &c_long);

// Error: Drop impl's assertion does not apply to `B` nor `&'b _`
pt = Pt("pt", &c_long, &c);
//~^ ERROR `c` does not live long enough
pr = Pr("pr", &c_long, &c);
//~^ ERROR `c` does not live long enough

// No error: St and Sr have no destructor.
st = St("st", &c);
sr = Sr("sr", &c);

println!("{:?}", (dt.0, dr.0, pt.0, pr.0, st.0, sr.0));
}
//~^ ERROR `c` does not live long enough
//~| ERROR `c` does not live long enough
//~| ERROR `c` does not live long enough
//~| ERROR `c` does not live long enough
Loading