Skip to content

Commit

Permalink
Fix diagnostics for async block cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
wutchzone committed Mar 23, 2024
1 parent 020bbe4 commit 0c7f8b0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(rustc::untranslatable_diagnostic)]

use either::Either;
use hir::ClosureKind;
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::{codes::*, struct_span_code_err, Applicability, Diag, MultiSpan};
Expand Down Expand Up @@ -463,6 +464,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
} else if let UseSpans::FnSelfUse { kind: CallKind::Normal { .. }, .. } = move_spans
{
// We already suggest cloning for these cases in `explain_captures`.
} else if let UseSpans::ClosureUse {
closure_kind:
ClosureKind::Coroutine(CoroutineKind::Desugared(_, CoroutineSource::Block)),
args_span: _,
capture_kind_span: _,
path_span,
} = move_spans
{
self.suggest_cloning(err, ty, expr, path_span);
} else if self.suggest_hoisting_call_outside_loop(err, expr) {
// The place where the the type moves would be misleading to suggest clone.
// #121466
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/borrowck/cloning-in-async-block-121547.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@ edition:2021

async fn clone_async_block(value: String) {
for _ in 0..10 {
async { //~ ERROR: use of moved value: `value` [E0382]
drop(value);
//~^ HELP: consider cloning the value if the performance cost is acceptable
}.await
}
}
fn main() {}
22 changes: 22 additions & 0 deletions tests/ui/borrowck/cloning-in-async-block-121547.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0382]: use of moved value: `value`
--> $DIR/cloning-in-async-block-121547.rs:5:9
|
LL | async fn clone_async_block(value: String) {
| ----- move occurs because `value` has type `String`, which does not implement the `Copy` trait
LL | for _ in 0..10 {
| -------------- inside of this loop
LL | / async {
LL | | drop(value);
| | ----- use occurs due to use in coroutine
LL | |
LL | | }.await
| |_________^ value moved here, in previous iteration of loop
|
help: consider cloning the value if the performance cost is acceptable
|
LL | drop(value.clone());
| ++++++++

error: aborting due to 1 previous error

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

0 comments on commit 0c7f8b0

Please sign in to comment.