Skip to content
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

Don't match on region kinds when reporting NLL errors #52617

Merged
merged 1 commit into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
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
109 changes: 14 additions & 95 deletions src/librustc_mir/borrow_check/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc::mir::{BindingForm, BorrowKind, ClearCrossCrate, Field, Local};
use rustc::mir::{LocalDecl, LocalKind, Location, Operand, Place};
use rustc::mir::{ProjectionElem, Rvalue, Statement, StatementKind};
use rustc::mir::VarBindingForm;
use rustc::ty::{self, RegionKind};
use rustc::ty;
use rustc_data_structures::indexed_vec::Idx;
use rustc_data_structures::sync::Lrc;
use syntax_pos::Span;
Expand Down Expand Up @@ -427,34 +427,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
self.access_place_error_reported
.insert((root_place.clone(), borrow_span));

match (borrow.region, &self.describe_place(&borrow.borrowed_place)) {
(RegionKind::ReScope(_), Some(name)) => {
self.report_scoped_local_value_does_not_live_long_enough(
context,
name,
&scope_tree,
&borrow,
drop_span,
borrow_span,
proper_span,
);
}
(RegionKind::ReScope(_), None) => {
self.report_scoped_temporary_value_does_not_live_long_enough(
context,
&scope_tree,
&borrow,
drop_span,
borrow_span,
proper_span,
);
}
(RegionKind::ReEarlyBound(_), Some(name))
| (RegionKind::ReFree(_), Some(name))
| (RegionKind::ReStatic, Some(name))
| (RegionKind::ReEmpty, Some(name))
| (RegionKind::ReVar(_), Some(name)) => {
self.report_unscoped_local_value_does_not_live_long_enough(
match &self.describe_place(&borrow.borrowed_place) {
Some(name) => {
self.report_local_value_does_not_live_long_enough(
context,
name,
&scope_tree,
Expand All @@ -465,12 +440,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
kind.map(|k| (k, place_span.0)),
);
}
(RegionKind::ReEarlyBound(_), None)
| (RegionKind::ReFree(_), None)
| (RegionKind::ReStatic, None)
| (RegionKind::ReEmpty, None)
| (RegionKind::ReVar(_), None) => {
self.report_unscoped_temporary_value_does_not_live_long_enough(
None => {
self.report_temporary_value_does_not_live_long_enough(
context,
&scope_tree,
&borrow,
Expand All @@ -479,65 +450,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
proper_span,
);
}
(RegionKind::ReLateBound(_, _), _)
| (RegionKind::ReSkolemized(_, _), _)
| (RegionKind::ReClosureBound(_), _)
| (RegionKind::ReCanonical(_), _)
| (RegionKind::ReErased, _) => {
span_bug!(
drop_span,
"region {:?} does not make sense in this context",
borrow.region
);
}
}
}

fn report_scoped_local_value_does_not_live_long_enough(
&mut self,
context: Context,
name: &String,
_scope_tree: &Lrc<ScopeTree>,
borrow: &BorrowData<'tcx>,
drop_span: Span,
borrow_span: Span,
_proper_span: Span,
) {
let tcx = self.tcx;
let mut err =
tcx.path_does_not_live_long_enough(borrow_span, &format!("`{}`", name), Origin::Mir);
err.span_label(borrow_span, "borrowed value does not live long enough");
err.span_label(
drop_span,
format!("`{}` dropped here while still borrowed", name),
);
self.explain_why_borrow_contains_point(context, borrow, None, &mut err);
err.buffer(&mut self.errors_buffer);
}

fn report_scoped_temporary_value_does_not_live_long_enough(
&mut self,
context: Context,
_scope_tree: &Lrc<ScopeTree>,
borrow: &BorrowData<'tcx>,
drop_span: Span,
_borrow_span: Span,
proper_span: Span,
) {
let tcx = self.tcx;
let mut err =
tcx.path_does_not_live_long_enough(proper_span, "borrowed value", Origin::Mir);
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");
self.explain_why_borrow_contains_point(context, borrow, None, &mut err);
err.buffer(&mut self.errors_buffer);
}

fn report_unscoped_local_value_does_not_live_long_enough(
fn report_local_value_does_not_live_long_enough(
&mut self,
context: Context,
name: &String,
Expand All @@ -549,7 +465,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
kind_place: Option<(WriteKind, &Place<'tcx>)>,
) {
debug!(
"report_unscoped_local_value_does_not_live_long_enough(\
"report_local_value_does_not_live_long_enough(\
{:?}, {:?}, {:?}, {:?}, {:?}, {:?}\
)",
context, name, scope_tree, borrow, drop_span, borrow_span
Expand All @@ -559,13 +475,16 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
let mut err =
tcx.path_does_not_live_long_enough(borrow_span, &format!("`{}`", name), Origin::Mir);
err.span_label(borrow_span, "borrowed value does not live long enough");
err.span_label(drop_span, "borrowed value only lives until here");
err.span_label(
drop_span,
format!("`{}` dropped here while still borrowed", name),
);

self.explain_why_borrow_contains_point(context, borrow, kind_place, &mut err);
err.buffer(&mut self.errors_buffer);
}

fn report_unscoped_temporary_value_does_not_live_long_enough(
fn report_temporary_value_does_not_live_long_enough(
&mut self,
context: Context,
scope_tree: &Lrc<ScopeTree>,
Expand All @@ -575,7 +494,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
proper_span: Span,
) {
debug!(
"report_unscoped_temporary_value_does_not_live_long_enough(\
"report_temporary_value_does_not_live_long_enough(\
{:?}, {:?}, {:?}, {:?}, {:?}\
)",
context, scope_tree, borrow, drop_span, proper_span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | spawn(|| books.push(4));
| ^^^^^^^^^^^^^^^^ borrowed value does not live long enough
LL | //~^ ERROR E0373
LL | }
| - borrowed value only lives until here
| - `books` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | Box::new(|| books.push(4))
| ^^^^^^^^^^^^^^^^ borrowed value does not live long enough
LL | //~^ ERROR E0373
LL | }
| - borrowed value only lives until here
| - `books` dropped here while still borrowed
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 19:8...
--> $DIR/borrowck-escaping-closure-error-2.rs:19:8
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/dropck/dropck-eyepatch-extern-crate.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | dt = Dt("dt", &c_shortest);
LL | }
| -
| |
| borrowed value only lives until here
| `c_shortest` dropped here while still borrowed
| borrow later used here, when `dt` is dropped
|
= note: values in a scope are dropped in the opposite order they are defined
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/dropck/dropck-eyepatch-reorder.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | dt = Dt("dt", &c_shortest);
LL | }
| -
| |
| borrowed value only lives until here
| `c_shortest` dropped here while still borrowed
| borrow later used here, when `dt` is dropped
|
= note: values in a scope are dropped in the opposite order they are defined
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/dropck/dropck-eyepatch.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | dt = Dt("dt", &c_shortest);
LL | }
| -
| |
| borrowed value only lives until here
| `c_shortest` dropped here while still borrowed
| borrow later used here, when `dt` is dropped
|
= note: values in a scope are dropped in the opposite order they are defined
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0597.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | //~^ `y` does not live long enough [E0597]
LL | }
| -
| |
| borrowed value only lives until here
| `y` dropped here while still borrowed
| borrow later used here, when `x` is dropped
|
= note: values in a scope are dropped in the opposite order they are defined
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/generator/borrowing.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | unsafe { (|| yield &a).resume() }
| ^^^^^^^^^^^^^ borrowed value does not live long enough
LL | //~^ ERROR: `a` does not live long enough
LL | };
| - borrowed value only lives until here
| - `a` dropped here while still borrowed

error[E0597]: `a` does not live long enough
--> $DIR/borrowing.rs:24:9
Expand All @@ -16,7 +16,7 @@ LL | | //~^ ERROR: `a` does not live long enough
LL | | }
| |_________^ borrowed value does not live long enough
LL | };
| - borrowed value only lives until here
| - `a` dropped here while still borrowed
LL | }
| - borrow later used here, when `_b` is dropped

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/generator/dropck.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | let ref_ = Box::leak(Box::new(Some(cell.borrow_mut())));
LL | }
| -
| |
| borrowed value only lives until here
| `*cell` dropped here while still borrowed
| borrow later used here, when `gen` is dropped
|
= note: values in a scope are dropped in the opposite order they are defined
Expand All @@ -26,7 +26,7 @@ LL | | };
LL | }
| -
| |
| borrowed value only lives until here
| `ref_` dropped here while still borrowed
| borrow later used here, when `gen` is dropped
|
= note: values in a scope are dropped in the opposite order they are defined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | a = &b;
| ^^ borrowed value does not live long enough
LL | //~^ ERROR `b` does not live long enough
LL | };
| - borrowed value only lives until here
| - `b` dropped here while still borrowed

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issue-12470.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | let bb: &B = &*b; //~ ERROR does not live long enough
| ^^^ borrowed value does not live long enough
LL | make_a(bb)
LL | }
| - borrowed value only lives until here
| - `*b` dropped here while still borrowed
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 36:16...
--> $DIR/issue-12470.rs:36:16
Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/issue-13497-2.nll.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0597]: `rawLines` does not live long enough
--> $DIR/issue-13497-2.rs:13:5
|
LL | rawLines //~ ERROR `rawLines` does not live long enough
| ^^^^^^^^ borrowed value does not live long enough
LL | .iter().map(|l| l.trim()).collect()
LL | }
| - `rawLines` dropped here while still borrowed
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 11:24...
--> $DIR/issue-13497-2.rs:11:24
|
LL | fn read_lines_borrowed<'a>() -> Vec<&'a str> {
| ^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.
2 changes: 1 addition & 1 deletion src/test/ui/issue-17954.ast.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | let a = &FOO;
| ^^^^ borrowed value does not live long enough
...
LL | }
| - borrowed value only lives until here
| - `FOO` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issue-17954.mir.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | let a = &FOO;
| ^^^^ borrowed value does not live long enough
...
LL | }
| - borrowed value only lives until here
| - `FOO` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issue-17954.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ fn main() {
println!("{}", a);
});
}
//[mir]~^ borrowed value only lives until here
//[mir]~^ `FOO` dropped here while still borrowed
//[ast]~^^ temporary value only lives until here
2 changes: 1 addition & 1 deletion src/test/ui/issue-18118.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ LL | &p //~ ERROR `p` does not live long enough
| ^^ borrowed value does not live long enough
LL | //~^ ERROR let bindings in constants are unstable
LL | };
| - borrowed value only lives until here
| - `p` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issue-30438-c.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | &x
| ^^ borrowed value does not live long enough
LL | //~^ ERROR: `x` does not live long enough
LL | }
| - borrowed value only lives until here
| - `x` dropped here while still borrowed
|
note: borrowed value must be valid for the lifetime 'y as defined on the function body at 17:10...
--> $DIR/issue-30438-c.rs:17:10
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issue-4335.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | id(Box::new(|| *v))
| ^^^^^ borrowed value does not live long enough
...
LL | }
| - borrowed value only lives until here
| - `v` dropped here while still borrowed
|
note: borrowed value must be valid for the lifetime 'r as defined on the function body at 15:6...
--> $DIR/issue-4335.rs:15:6
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issue-46036.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | let foo = Foo { x: &a }; //~ ERROR E0597
| ^^ borrowed value does not live long enough
LL | loop { }
LL | }
| - borrowed value only lives until here
| - `a` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issue-46471-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ LL | | &mut z
LL | | };
| | -
| | |
| |_____borrowed value only lives until here
| |_____`z` dropped here while still borrowed
| borrow later used here

error: aborting due to 2 previous errors
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issue-46471.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LL | &x
| ^^ borrowed value does not live long enough
...
LL | }
| - borrowed value only lives until here
| - `x` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issue-52126-assign-op-invariance.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | println!("accumulator before add_assign {:?}", acc.map);
| ------- borrow later used here
...
LL | }
| - borrowed value only lives until here
| - `line` dropped here while still borrowed

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/nll/borrowed-local-error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LL | | //~^ ERROR `v` does not live long enough [E0597]
LL | | });
| |_____-- borrow later used here
| |
| borrowed value only lives until here
| `v` dropped here while still borrowed

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/nll/borrowed-universal-error-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | &v
| ^^ borrowed value does not live long enough
LL | //~^ ERROR `v` does not live long enough [E0597]
LL | }
| - borrowed value only lives until here
| - `v` dropped here while still borrowed
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 14:8...
--> $DIR/borrowed-universal-error-2.rs:14:8
Expand Down
Loading