Skip to content

Commit 33ed75c

Browse files
authored
Unrolled build for rust-lang#122589
Rollup merge of rust-lang#122589 - wutchzone:121547, r=compiler-errors Fix diagnostics for async block cloning Closes rust-lang#121547 r? diagnostics
2 parents 47ecded + 2c433d0 commit 33ed75c

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+13-3
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
@@ -621,7 +631,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
621631
}
622632

623633
// FIXME: We make sure that this is a normal top-level binding,
624-
// but we could suggest `todo!()` for all uninitalized bindings in the pattern pattern
634+
// but we could suggest `todo!()` for all uninitialized bindings in the pattern pattern
625635
if let hir::StmtKind::Let(hir::LetStmt { span, ty, init: None, pat, .. }) =
626636
&ex.kind
627637
&& let hir::PatKind::Binding(..) = pat.kind
@@ -749,7 +759,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
749759
true
750760
}
751761

752-
/// In a move error that occurs on a call wihtin a loop, we try to identify cases where cloning
762+
/// In a move error that occurs on a call within a loop, we try to identify cases where cloning
753763
/// the value would lead to a logic error. We infer these cases by seeing if the moved value is
754764
/// part of the logic to break the loop, either through an explicit `break` or if the expression
755765
/// is part of a `while let`.
@@ -950,7 +960,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
950960
{
951961
// FIXME: We could check that the call's *parent* takes `&mut val` to make the
952962
// suggestion more targeted to the `mk_iter(val).next()` case. Maybe do that only to
953-
// check for wheter to suggest `let value` or `let mut value`.
963+
// check for whether to suggest `let value` or `let mut value`.
954964

955965
let span = in_loop.span;
956966
if !finder.found_breaks.is_empty()
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)