Skip to content

Commit

Permalink
Rollup merge of #65166 - csmoe:async-move, r=estebank
Browse files Browse the repository at this point in the history
Suggest to add `move` keyword for generator capture

 Closes #64382
r? @estebank
  • Loading branch information
Centril committed Oct 9, 2019
2 parents ae5bb7e + d1d2565 commit 5bbdcb6
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/librustc_mir/borrow_check/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let kind_place = kind.filter(|_| place_desc.is_some()).map(|k| (k, place_span.0));
let explanation = self.explain_why_borrow_contains_point(location, &borrow, kind_place);

debug!(
"report_borrowed_value_does_not_live_long_enough(place_desc: {:?}, explanation: {:?})",
place_desc,
explanation
);
let err = match (place_desc, explanation) {
(Some(_), _) if self.is_place_thread_local(root_place) => {
self.report_thread_local_value_does_not_live_long_enough(drop_span, borrow_span)
Expand Down Expand Up @@ -790,6 +795,24 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
span,
&format!("`{}`", name),
),
(
Some(ref name),
BorrowExplanation::MustBeValidFor {
category: category @ ConstraintCategory::OpaqueType,
from_closure: false,
ref region_name,
span,
..
},

) if borrow_spans.for_generator() => self.report_escaping_closure_capture(
borrow_spans.args_or_use(),
borrow_span,
region_name,
category,
span,
&format!("`{}`", name),
),
(
ref name,
BorrowExplanation::MustBeValidFor {
Expand Down Expand Up @@ -1214,6 +1237,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
ConstraintCategory::Return => {
err.span_note(constraint_span, "closure is returned here");
}
ConstraintCategory::OpaqueType => {
err.span_note(constraint_span, "generator is returned here");
}
ConstraintCategory::CallArgument => {
fr_name.highlight_region_name(&mut err);
err.span_note(
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use syntax_pos::Span;

mod find_use;

#[derive(Debug)]
pub(in crate::borrow_check) enum BorrowExplanation {
UsedLater(LaterUseKind, Span),
UsedLaterInLoop(LaterUseKind, Span),
Expand All @@ -35,7 +36,7 @@ pub(in crate::borrow_check) enum BorrowExplanation {
Unexplained,
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
pub(in crate::borrow_check) enum LaterUseKind {
TraitCapture,
ClosureCapture,
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/async-await/async-borrowck-escaping-block-error.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// edition:2018
// run-rustfix

fn foo() -> Box<impl std::future::Future<Output = u32>> {
let x = 0u32;
Box::new(async move { x } )
//~^ ERROR E0373
}

fn main() {
let _foo = foo();
}
12 changes: 12 additions & 0 deletions src/test/ui/async-await/async-borrowck-escaping-block-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// edition:2018
// run-rustfix

fn foo() -> Box<impl std::future::Future<Output = u32>> {
let x = 0u32;
Box::new(async { x } )
//~^ ERROR E0373
}

fn main() {
let _foo = foo();
}
22 changes: 22 additions & 0 deletions src/test/ui/async-await/async-borrowck-escaping-block-error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0373]: closure may outlive the current function, but it borrows `x`, which is owned by the current function
--> $DIR/async-borrowck-escaping-block-error.rs:6:20
|
LL | Box::new(async { x } )
| ^^-^^
| | |
| | `x` is borrowed here
| may outlive borrowed value `x`
|
note: generator is returned here
--> $DIR/async-borrowck-escaping-block-error.rs:4:13
|
LL | fn foo() -> Box<impl std::future::Future<Output = u32>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: to force the closure to take ownership of `x` (and any other referenced variables), use the `move` keyword
|
LL | Box::new(async move { x } )
| ^^^^^^^^^^

error: aborting due to previous error

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

0 comments on commit 5bbdcb6

Please sign in to comment.