Rollup merge of #124595 - estebank:issue-104232, r=davidtwco
Suggest cloning `Arc` moved into closure
```
error[E0382]: borrow of moved value: `x`
--> $DIR/moves-based-on-type-capture-clause-bad.rs:9:20
|
LL | let x = "Hello world!".to_string();
| - move occurs because `x` has type `String`, which does not implement the `Copy` trait
LL | thread::spawn(move || {
| ------- value moved into closure here
LL | println!("{}", x);
| - variable moved due to use in closure
LL | });
LL | println!("{}", x);
| ^ value borrowed here after move
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider cloning the value before moving it into the closure
|
LL ~ let value = x.clone();
LL ~ thread::spawn(move || {
LL ~ println!("{}", value);
|
```
Fix rust-lang/rust#104232.
This commit is contained in:
@@ -325,25 +325,17 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
|
||||
self.cannot_move_out_of(span, &description)
|
||||
}
|
||||
|
||||
fn suggest_clone_of_captured_var_in_move_closure(
|
||||
pub(in crate::diagnostics) fn suggest_clone_of_captured_var_in_move_closure(
|
||||
&self,
|
||||
err: &mut Diag<'_>,
|
||||
upvar_hir_id: HirId,
|
||||
upvar_name: &str,
|
||||
use_spans: Option<UseSpans<'tcx>>,
|
||||
) {
|
||||
let tcx = self.infcx.tcx;
|
||||
let typeck_results = tcx.typeck(self.mir_def_id());
|
||||
let Some(use_spans) = use_spans else { return };
|
||||
// We only care about the case where a closure captured a binding.
|
||||
let UseSpans::ClosureUse { args_span, .. } = use_spans else { return };
|
||||
let Some(body_id) = tcx.hir_node(self.mir_hir_id()).body_id() else { return };
|
||||
// Fetch the type of the expression corresponding to the closure-captured binding.
|
||||
let Some(captured_ty) = typeck_results.node_type_opt(upvar_hir_id) else { return };
|
||||
if !self.implements_clone(captured_ty) {
|
||||
// We only suggest cloning the captured binding if the type can actually be cloned.
|
||||
return;
|
||||
};
|
||||
// Find the closure that captured the binding.
|
||||
let mut expr_finder = FindExprBySpan::new(args_span, tcx);
|
||||
expr_finder.include_closures = true;
|
||||
@@ -396,7 +388,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
|
||||
.indentation_before(stmt.span)
|
||||
.unwrap_or_else(|| " ".to_string());
|
||||
err.multipart_suggestion_verbose(
|
||||
"clone the value before moving it into the closure",
|
||||
"consider cloning the value before moving it into the closure",
|
||||
vec![
|
||||
(
|
||||
stmt.span.shrink_to_lo(),
|
||||
@@ -426,7 +418,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
|
||||
.indentation_before(closure_expr.span)
|
||||
.unwrap_or_else(|| " ".to_string());
|
||||
err.multipart_suggestion_verbose(
|
||||
"clone the value before moving it into the closure",
|
||||
"consider cloning the value before moving it into the closure",
|
||||
vec![
|
||||
(
|
||||
closure_expr.span.shrink_to_lo(),
|
||||
@@ -523,20 +515,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
|
||||
);
|
||||
|
||||
let closure_span = tcx.def_span(def_id);
|
||||
let mut err = self
|
||||
.cannot_move_out_of(span, &place_description)
|
||||
self.cannot_move_out_of(span, &place_description)
|
||||
.with_span_label(upvar_span, "captured outer variable")
|
||||
.with_span_label(
|
||||
closure_span,
|
||||
format!("captured by this `{closure_kind}` closure"),
|
||||
);
|
||||
self.suggest_clone_of_captured_var_in_move_closure(
|
||||
&mut err,
|
||||
upvar_hir_id,
|
||||
&upvar_name,
|
||||
use_spans,
|
||||
);
|
||||
err
|
||||
)
|
||||
}
|
||||
_ => {
|
||||
let source = self.borrowed_content_source(deref_base);
|
||||
@@ -597,7 +581,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
|
||||
};
|
||||
|
||||
if let Some(expr) = self.find_expr(span) {
|
||||
self.suggest_cloning(err, place_ty, expr, None);
|
||||
self.suggest_cloning(err, move_from.as_ref(), place_ty, expr, None);
|
||||
}
|
||||
|
||||
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
|
||||
@@ -629,7 +613,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
|
||||
};
|
||||
|
||||
if let Some(expr) = self.find_expr(use_span) {
|
||||
self.suggest_cloning(err, place_ty, expr, Some(use_spans));
|
||||
self.suggest_cloning(
|
||||
err,
|
||||
original_path.as_ref(),
|
||||
place_ty,
|
||||
expr,
|
||||
Some(use_spans),
|
||||
);
|
||||
}
|
||||
|
||||
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
|
||||
@@ -832,7 +822,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
|
||||
let place_desc = self.local_name(*local).map(|sym| format!("`{sym}`"));
|
||||
|
||||
if let Some(expr) = self.find_expr(binding_span) {
|
||||
self.suggest_cloning(err, bind_to.ty, expr, None);
|
||||
let local_place: PlaceRef<'tcx> = (*local).into();
|
||||
self.suggest_cloning(err, local_place, bind_to.ty, expr, None);
|
||||
}
|
||||
|
||||
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
|
||||
|
||||
Reference in New Issue
Block a user