Skip to content

Commit 5bbdcb6

Browse files
authored
Rollup merge of #65166 - csmoe:async-move, r=estebank
Suggest to add `move` keyword for generator capture Closes #64382 r? @estebank
2 parents ae5bb7e + d1d2565 commit 5bbdcb6

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

src/librustc_mir/borrow_check/conflict_errors.rs

+26
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
750750
let kind_place = kind.filter(|_| place_desc.is_some()).map(|k| (k, place_span.0));
751751
let explanation = self.explain_why_borrow_contains_point(location, &borrow, kind_place);
752752

753+
debug!(
754+
"report_borrowed_value_does_not_live_long_enough(place_desc: {:?}, explanation: {:?})",
755+
place_desc,
756+
explanation
757+
);
753758
let err = match (place_desc, explanation) {
754759
(Some(_), _) if self.is_place_thread_local(root_place) => {
755760
self.report_thread_local_value_does_not_live_long_enough(drop_span, borrow_span)
@@ -790,6 +795,24 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
790795
span,
791796
&format!("`{}`", name),
792797
),
798+
(
799+
Some(ref name),
800+
BorrowExplanation::MustBeValidFor {
801+
category: category @ ConstraintCategory::OpaqueType,
802+
from_closure: false,
803+
ref region_name,
804+
span,
805+
..
806+
},
807+
808+
) if borrow_spans.for_generator() => self.report_escaping_closure_capture(
809+
borrow_spans.args_or_use(),
810+
borrow_span,
811+
region_name,
812+
category,
813+
span,
814+
&format!("`{}`", name),
815+
),
793816
(
794817
ref name,
795818
BorrowExplanation::MustBeValidFor {
@@ -1214,6 +1237,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12141237
ConstraintCategory::Return => {
12151238
err.span_note(constraint_span, "closure is returned here");
12161239
}
1240+
ConstraintCategory::OpaqueType => {
1241+
err.span_note(constraint_span, "generator is returned here");
1242+
}
12171243
ConstraintCategory::CallArgument => {
12181244
fr_name.highlight_region_name(&mut err);
12191245
err.span_note(

src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use syntax_pos::Span;
1717

1818
mod find_use;
1919

20+
#[derive(Debug)]
2021
pub(in crate::borrow_check) enum BorrowExplanation {
2122
UsedLater(LaterUseKind, Span),
2223
UsedLaterInLoop(LaterUseKind, Span),
@@ -35,7 +36,7 @@ pub(in crate::borrow_check) enum BorrowExplanation {
3536
Unexplained,
3637
}
3738

38-
#[derive(Clone, Copy)]
39+
#[derive(Clone, Copy, Debug)]
3940
pub(in crate::borrow_check) enum LaterUseKind {
4041
TraitCapture,
4142
ClosureCapture,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// edition:2018
2+
// run-rustfix
3+
4+
fn foo() -> Box<impl std::future::Future<Output = u32>> {
5+
let x = 0u32;
6+
Box::new(async move { x } )
7+
//~^ ERROR E0373
8+
}
9+
10+
fn main() {
11+
let _foo = foo();
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// edition:2018
2+
// run-rustfix
3+
4+
fn foo() -> Box<impl std::future::Future<Output = u32>> {
5+
let x = 0u32;
6+
Box::new(async { x } )
7+
//~^ ERROR E0373
8+
}
9+
10+
fn main() {
11+
let _foo = foo();
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0373]: closure may outlive the current function, but it borrows `x`, which is owned by the current function
2+
--> $DIR/async-borrowck-escaping-block-error.rs:6:20
3+
|
4+
LL | Box::new(async { x } )
5+
| ^^-^^
6+
| | |
7+
| | `x` is borrowed here
8+
| may outlive borrowed value `x`
9+
|
10+
note: generator is returned here
11+
--> $DIR/async-borrowck-escaping-block-error.rs:4:13
12+
|
13+
LL | fn foo() -> Box<impl std::future::Future<Output = u32>> {
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
help: to force the closure to take ownership of `x` (and any other referenced variables), use the `move` keyword
16+
|
17+
LL | Box::new(async move { x } )
18+
| ^^^^^^^^^^
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0373`.

0 commit comments

Comments
 (0)