Skip to content

Commit

Permalink
Give an error number for "borrowed data escapes outside of closure"
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Oct 20, 2018
1 parent 049bee0 commit 275432c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 9 deletions.
Expand Up @@ -13,6 +13,7 @@ use borrow_check::nll::region_infer::RegionInferenceContext;
use borrow_check::nll::region_infer::error_reporting::region_name::RegionNameSource;
use borrow_check::nll::type_check::Locations;
use borrow_check::nll::universal_regions::DefiningTy;
use util::borrowck_errors::{BorrowckErrors, Origin};
use rustc::hir::def_id::DefId;
use rustc::infer::error_reporting::nice_region_error::NiceRegionError;
use rustc::infer::InferCtxt;
Expand Down Expand Up @@ -395,9 +396,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
category, span, errors_buffer);
}

let mut diag = infcx.tcx.sess.struct_span_err(
span, &format!("borrowed data escapes outside of {}", escapes_from),
);
let mut diag = infcx.tcx.borrowed_data_escapes_closure(span, escapes_from, Origin::Mir);

if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span {
diag.span_label(
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/diagnostics.rs
Expand Up @@ -2339,6 +2339,7 @@ register_diagnostics! {
// E0471, // constant evaluation error (in pattern)
// E0385, // {} in an aliasable location
E0493, // destructors cannot be evaluated at compile-time
E0521, // borrowed data escapes outside of closure
E0524, // two closures require unique access to `..` at the same time
E0526, // shuffle indices are not constant
E0594, // cannot assign to {}
Expand Down
18 changes: 18 additions & 0 deletions src/librustc_mir/util/borrowck_errors.rs
Expand Up @@ -717,6 +717,24 @@ pub trait BorrowckErrors<'cx>: Sized + Copy {
self.cancel_if_wrong_origin(err, o)
}

fn borrowed_data_escapes_closure(
self,
escape_span: Span,
escapes_from: &str,
o: Origin,
) -> DiagnosticBuilder<'cx> {
let err = struct_span_err!(
self,
escape_span,
E0521,
"borrowed data escapes outside of {}{OGN}",
escapes_from,
OGN = o
);

self.cancel_if_wrong_origin(err, o)
}

fn thread_local_value_does_not_live_long_enough(
self,
span: Span,
Expand Down
5 changes: 3 additions & 2 deletions src/test/ui/borrowck/issue-45983.nll.stderr
@@ -1,4 +1,4 @@
error: borrowed data escapes outside of closure
error[E0521]: borrowed data escapes outside of closure
--> $DIR/issue-45983.rs:36:18
|
LL | let x = None;
Expand All @@ -18,4 +18,5 @@ LL | give_any(|y| x = Some(y));

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0594`.
Some errors occurred: E0521, E0594.
For more information about an error, try `rustc --explain E0521`.
Expand Up @@ -13,7 +13,7 @@ LL | | })
for<'r> extern "rust-call" fn((std::cell::Cell<&'_#1r u32>, std::cell::Cell<&ReLateBound(DebruijnIndex(0), BrNamed(crate0:DefIndex(0:0), 'r)) u32>))
]

error: borrowed data escapes outside of closure
error[E0521]: borrowed data escapes outside of closure
--> $DIR/propagate-approximated-shorter-to-static-comparing-against-free.rs:32:9
|
LL | foo(cell, |cell_a, cell_x| {
Expand Down Expand Up @@ -80,4 +80,5 @@ LL | }

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0597`.
Some errors occurred: E0521, E0597.
For more information about an error, try `rustc --explain E0521`.
Expand Up @@ -34,7 +34,7 @@ LL | | }
|
= note: defining type: DefId(0/0:6 ~ propagate_approximated_shorter_to_static_no_bound[317d]::supply[0]) with substs []

error: borrowed data escapes outside of function
error[E0521]: borrowed data escapes outside of function
--> $DIR/propagate-approximated-shorter-to-static-no-bound.rs:45:5
|
LL | fn supply<'a, 'b>(cell_a: Cell<&'a u32>, cell_b: Cell<&'b u32>) {
Expand All @@ -61,3 +61,4 @@ LL | demand_y(x, y, x.get())

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0521`.
Expand Up @@ -33,7 +33,7 @@ LL | | }
|
= note: defining type: DefId(0/0:6 ~ propagate_approximated_shorter_to_static_wrong_bound[317d]::supply[0]) with substs []

error: borrowed data escapes outside of function
error[E0521]: borrowed data escapes outside of function
--> $DIR/propagate-approximated-shorter-to-static-wrong-bound.rs:48:5
|
LL | fn supply<'a, 'b>(cell_a: Cell<&'a u32>, cell_b: Cell<&'b u32>) {
Expand All @@ -59,3 +59,4 @@ LL | demand_y(x, y, x.get())

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0521`.

0 comments on commit 275432c

Please sign in to comment.