diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2021-04-07 13:07:17 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-07 13:07:17 +0200 |
| commit | d7d42ccfd254dca9923bca63b008423dc4e0f83d (patch) | |
| tree | fc12b3db17212c201f0303b2fd65a6e355f2320a /compiler | |
| parent | 9c688cd2a2935ae4645c0b878f1f40ea51e98816 (diff) | |
| parent | a77598434838376141c8cb25fe29693b3a26b173 (diff) | |
| download | rust-d7d42ccfd254dca9923bca63b008423dc4e0f83d.tar.gz rust-d7d42ccfd254dca9923bca63b008423dc4e0f83d.zip | |
Rollup merge of #83945 - SkiFire13:fix-83924, r=estebank
Add suggestion to reborrow mutable references when they're moved in a for loop Address #83924
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs b/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs index d5deec82088..5fdf8a8d1ee 100644 --- a/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs +++ b/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs @@ -264,7 +264,24 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { if let Some(DesugaringKind::ForLoop(_)) = move_span.desugaring_kind() { let sess = self.infcx.tcx.sess; - if let Ok(snippet) = sess.source_map().span_to_snippet(move_span) { + let ty = used_place.ty(self.body, self.infcx.tcx).ty; + // If we have a `&mut` ref, we need to reborrow. + if let ty::Ref(_, _, hir::Mutability::Mut) = ty.kind() { + // If we are in a loop this will be suggested later. + if !is_loop_move { + err.span_suggestion_verbose( + move_span.shrink_to_lo(), + &format!( + "consider creating a fresh reborrow of {} here", + self.describe_place(moved_place.as_ref()) + .map(|n| format!("`{}`", n)) + .unwrap_or_else(|| "the mutable reference".to_string()), + ), + format!("&mut *"), + Applicability::MachineApplicable, + ); + } + } else if let Ok(snippet) = sess.source_map().span_to_snippet(move_span) { err.span_suggestion( move_span, "consider borrowing to avoid moving into the for loop", |
