Skip to content

Commit 2c0f05a

Browse files
authored
Rollup merge of #63699 - gilescope:async-move-diagnostic, r=estebank
Fix suggestion from incorrect `move async` to `async move`. PR for #61920. Happy with the test. There must be a better implementation though - possibly a MIR visitor to estabilsh a span that doesn't include the `async` keyword?
2 parents a2080a6 + ef3e66d commit 2c0f05a

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/librustc_mir/borrow_check/conflict_errors.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11901190
);
11911191

11921192
let suggestion = match tcx.sess.source_map().span_to_snippet(args_span) {
1193-
Ok(string) => format!("move {}", string),
1193+
Ok(mut string) => {
1194+
if string.starts_with("async ") {
1195+
string.insert_str(6, "move ");
1196+
} else if string.starts_with("async|") {
1197+
string.insert_str(5, " move");
1198+
} else {
1199+
string.insert_str(0, "move ");
1200+
};
1201+
string
1202+
},
11941203
Err(_) => "move |<args>| <body>".to_string()
11951204
};
11961205

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// edition:2018
2+
#![feature(async_closure,async_await)]
3+
fn foo() -> Box<dyn std::future::Future<Output = u32>> {
4+
let x = 0u32;
5+
Box::new((async || x)())
6+
//~^ ERROR E0373
7+
}
8+
9+
fn main() {
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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-closure-error.rs:5:15
3+
|
4+
LL | Box::new((async || x)())
5+
| ^^^^^^^^ - `x` is borrowed here
6+
| |
7+
| may outlive borrowed value `x`
8+
|
9+
note: closure is returned here
10+
--> $DIR/async-borrowck-escaping-closure-error.rs:5:5
11+
|
12+
LL | Box::new((async || x)())
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^
14+
help: to force the closure to take ownership of `x` (and any other referenced variables), use the `move` keyword
15+
|
16+
LL | Box::new((async move || x)())
17+
| ^^^^^^^^^^^^^
18+
19+
error: aborting due to previous error
20+
21+
For more information about this error, try `rustc --explain E0373`.

0 commit comments

Comments
 (0)