Skip to content

Commit 0c7f8b0

Browse files
committed
Fix diagnostics for async block cloning
1 parent 020bbe4 commit 0c7f8b0

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(rustc::untranslatable_diagnostic)]
55

66
use either::Either;
7+
use hir::ClosureKind;
78
use rustc_data_structures::captures::Captures;
89
use rustc_data_structures::fx::FxIndexSet;
910
use rustc_errors::{codes::*, struct_span_code_err, Applicability, Diag, MultiSpan};
@@ -463,6 +464,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
463464
} else if let UseSpans::FnSelfUse { kind: CallKind::Normal { .. }, .. } = move_spans
464465
{
465466
// We already suggest cloning for these cases in `explain_captures`.
467+
} else if let UseSpans::ClosureUse {
468+
closure_kind:
469+
ClosureKind::Coroutine(CoroutineKind::Desugared(_, CoroutineSource::Block)),
470+
args_span: _,
471+
capture_kind_span: _,
472+
path_span,
473+
} = move_spans
474+
{
475+
self.suggest_cloning(err, ty, expr, path_span);
466476
} else if self.suggest_hoisting_call_outside_loop(err, expr) {
467477
// The place where the the type moves would be misleading to suggest clone.
468478
// #121466
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ edition:2021
2+
3+
async fn clone_async_block(value: String) {
4+
for _ in 0..10 {
5+
async { //~ ERROR: use of moved value: `value` [E0382]
6+
drop(value);
7+
//~^ HELP: consider cloning the value if the performance cost is acceptable
8+
}.await
9+
}
10+
}
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0382]: use of moved value: `value`
2+
--> $DIR/cloning-in-async-block-121547.rs:5:9
3+
|
4+
LL | async fn clone_async_block(value: String) {
5+
| ----- move occurs because `value` has type `String`, which does not implement the `Copy` trait
6+
LL | for _ in 0..10 {
7+
| -------------- inside of this loop
8+
LL | / async {
9+
LL | | drop(value);
10+
| | ----- use occurs due to use in coroutine
11+
LL | |
12+
LL | | }.await
13+
| |_________^ value moved here, in previous iteration of loop
14+
|
15+
help: consider cloning the value if the performance cost is acceptable
16+
|
17+
LL | drop(value.clone());
18+
| ++++++++
19+
20+
error: aborting due to 1 previous error
21+
22+
For more information about this error, try `rustc --explain E0382`.

0 commit comments

Comments
 (0)