1use std::collections::BTreeMap;
4
5use rustc_abi::{FieldIdx, VariantIdx};
6use rustc_data_structures::fx::FxIndexMap;
7use rustc_errors::{Applicability, Diag, EmissionGuarantee, MultiSpan, listify};
8use rustc_hir::def::{CtorKind, Namespace};
9use rustc_hir::{
10 self as hir, CoroutineKind, GenericBound, LangItem, WhereBoundPredicate, WherePredicateKind,
11};
12use rustc_index::{IndexSlice, IndexVec};
13use rustc_infer::infer::{BoundRegionConversionTime, NllRegionVariableOrigin};
14use rustc_infer::traits::SelectionError;
15use rustc_middle::mir::{
16 AggregateKind, CallSource, ConstOperand, ConstraintCategory, FakeReadCause, Local, LocalInfo,
17 LocalKind, Location, Operand, Place, PlaceRef, PlaceTy, ProjectionElem, Rvalue, Statement,
18 StatementKind, Terminator, TerminatorKind, VarDebugInfoContents, find_self_call,
19};
20use rustc_middle::ty::print::Print;
21use rustc_middle::ty::{self, Ty, TyCtxt};
22use rustc_middle::{bug, span_bug};
23use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult, MoveOutIndex};
24use rustc_span::def_id::LocalDefId;
25use rustc_span::source_map::Spanned;
26use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
27use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
28use rustc_trait_selection::error_reporting::traits::call_kind::{CallDesugaringKind, call_kind};
29use rustc_trait_selection::infer::InferCtxtExt;
30use rustc_trait_selection::traits::{
31 FulfillmentError, FulfillmentErrorCode, type_known_to_meet_bound_modulo_regions,
32};
33use tracing::debug;
34
35use super::MirBorrowckCtxt;
36use super::borrow_set::BorrowData;
37use crate::constraints::OutlivesConstraint;
38use crate::fluent_generated as fluent;
39use crate::nll::ConstraintDescription;
40use crate::session_diagnostics::{
41 CaptureArgLabel, CaptureReasonLabel, CaptureReasonNote, CaptureReasonSuggest, CaptureVarCause,
42 CaptureVarKind, CaptureVarPathUseCause, OnClosureNote,
43};
44
45mod find_all_local_uses;
46mod find_use;
47mod outlives_suggestion;
48mod region_name;
49mod var_name;
50
51mod bound_region_errors;
52mod conflict_errors;
53mod explain_borrow;
54mod move_errors;
55mod mutability_errors;
56mod opaque_types;
57mod region_errors;
58
59pub(crate) use bound_region_errors::{ToUniverseInfo, UniverseInfo};
60pub(crate) use move_errors::{IllegalMoveOriginKind, MoveError};
61pub(crate) use mutability_errors::AccessKind;
62pub(crate) use outlives_suggestion::OutlivesSuggestionBuilder;
63pub(crate) use region_errors::{ErrorConstraintInfo, RegionErrorKind, RegionErrors};
64pub(crate) use region_name::{RegionName, RegionNameSource};
65pub(crate) use rustc_trait_selection::error_reporting::traits::call_kind::CallKind;
66
67pub(super) struct DescribePlaceOpt {
68 including_downcast: bool,
69
70 including_tuple_field: bool,
73}
74
75pub(super) struct IncludingTupleField(pub(super) bool);
76
77enum BufferedDiag<'infcx> {
78 Error(Diag<'infcx>),
79 NonError(Diag<'infcx, ()>),
80}
81
82impl<'infcx> BufferedDiag<'infcx> {
83 fn sort_span(&self) -> Span {
84 match self {
85 BufferedDiag::Error(diag) => diag.sort_span,
86 BufferedDiag::NonError(diag) => diag.sort_span,
87 }
88 }
89}
90
91#[derive(Default)]
92pub(crate) struct BorrowckDiagnosticsBuffer<'infcx, 'tcx> {
93 buffered_move_errors: BTreeMap<Vec<MoveOutIndex>, (PlaceRef<'tcx>, Diag<'infcx>)>,
108
109 buffered_mut_errors: FxIndexMap<Span, (Diag<'infcx>, usize)>,
110
111 buffered_diags: Vec<BufferedDiag<'infcx>>,
113}
114
115impl<'infcx, 'tcx> BorrowckDiagnosticsBuffer<'infcx, 'tcx> {
116 pub(crate) fn buffer_non_error(&mut self, diag: Diag<'infcx, ()>) {
117 self.buffered_diags.push(BufferedDiag::NonError(diag));
118 }
119}
120
121impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
122 pub(crate) fn buffer_error(&mut self, diag: Diag<'infcx>) {
123 self.diags_buffer.buffered_diags.push(BufferedDiag::Error(diag));
124 }
125
126 pub(crate) fn buffer_non_error(&mut self, diag: Diag<'infcx, ()>) {
127 self.diags_buffer.buffer_non_error(diag);
128 }
129
130 pub(crate) fn buffer_move_error(
131 &mut self,
132 move_out_indices: Vec<MoveOutIndex>,
133 place_and_err: (PlaceRef<'tcx>, Diag<'infcx>),
134 ) -> bool {
135 if let Some((_, diag)) =
136 self.diags_buffer.buffered_move_errors.insert(move_out_indices, place_and_err)
137 {
138 diag.cancel();
140 false
141 } else {
142 true
143 }
144 }
145
146 pub(crate) fn get_buffered_mut_error(&mut self, span: Span) -> Option<(Diag<'infcx>, usize)> {
147 self.diags_buffer.buffered_mut_errors.swap_remove(&span)
149 }
150
151 pub(crate) fn buffer_mut_error(&mut self, span: Span, diag: Diag<'infcx>, count: usize) {
152 self.diags_buffer.buffered_mut_errors.insert(span, (diag, count));
153 }
154
155 pub(crate) fn emit_errors(&mut self) -> Option<ErrorGuaranteed> {
156 let mut res = self.infcx.tainted_by_errors();
157
158 for (_, (_, diag)) in std::mem::take(&mut self.diags_buffer.buffered_move_errors) {
160 self.buffer_error(diag);
162 }
163 for (_, (mut diag, count)) in std::mem::take(&mut self.diags_buffer.buffered_mut_errors) {
164 if count > 10 {
165 #[allow(rustc::diagnostic_outside_of_impl)]
166 #[allow(rustc::untranslatable_diagnostic)]
167 diag.note(format!("...and {} other attempted mutable borrows", count - 10));
168 }
169 self.buffer_error(diag);
170 }
171
172 if !self.diags_buffer.buffered_diags.is_empty() {
173 self.diags_buffer.buffered_diags.sort_by_key(|buffered_diag| buffered_diag.sort_span());
174 for buffered_diag in self.diags_buffer.buffered_diags.drain(..) {
175 match buffered_diag {
176 BufferedDiag::Error(diag) => res = Some(diag.emit()),
177 BufferedDiag::NonError(diag) => diag.emit(),
178 }
179 }
180 }
181
182 res
183 }
184
185 pub(crate) fn has_buffered_diags(&self) -> bool {
186 self.diags_buffer.buffered_diags.is_empty()
187 }
188
189 pub(crate) fn has_move_error(
190 &self,
191 move_out_indices: &[MoveOutIndex],
192 ) -> Option<&(PlaceRef<'tcx>, Diag<'infcx>)> {
193 self.diags_buffer.buffered_move_errors.get(move_out_indices)
194 }
195
196 fn local_name(&self, index: Local) -> Option<Symbol> {
198 *self.local_names().get(index)?
199 }
200
201 fn local_names(&self) -> &IndexSlice<Local, Option<Symbol>> {
202 self.local_names.get_or_init(|| {
203 let mut local_names = IndexVec::from_elem(None, &self.body.local_decls);
204 for var_debug_info in &self.body.var_debug_info {
205 if let VarDebugInfoContents::Place(place) = var_debug_info.value {
206 if let Some(local) = place.as_local() {
207 if let Some(prev_name) = local_names[local]
208 && var_debug_info.name != prev_name
209 {
210 span_bug!(
211 var_debug_info.source_info.span,
212 "local {:?} has many names (`{}` vs `{}`)",
213 local,
214 prev_name,
215 var_debug_info.name
216 );
217 }
218 local_names[local] = Some(var_debug_info.name);
219 }
220 }
221 }
222 local_names
223 })
224 }
225}
226
227impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
228 #[allow(rustc::diagnostic_outside_of_impl)] pub(super) fn add_moved_or_invoked_closure_note(
241 &self,
242 location: Location,
243 place: PlaceRef<'tcx>,
244 diag: &mut Diag<'infcx>,
245 ) -> bool {
246 debug!("add_moved_or_invoked_closure_note: location={:?} place={:?}", location, place);
247 let mut target = place.local_or_deref_local();
248 for stmt in &self.body[location.block].statements[location.statement_index..] {
249 debug!("add_moved_or_invoked_closure_note: stmt={:?} target={:?}", stmt, target);
250 if let StatementKind::Assign(box (into, Rvalue::Use(from))) = &stmt.kind {
251 debug!("add_fnonce_closure_note: into={:?} from={:?}", into, from);
252 match from {
253 Operand::Copy(place) | Operand::Move(place)
254 if target == place.local_or_deref_local() =>
255 {
256 target = into.local_or_deref_local()
257 }
258 _ => {}
259 }
260 }
261 }
262
263 let terminator = self.body[location.block].terminator();
265 debug!("add_moved_or_invoked_closure_note: terminator={:?}", terminator);
266 if let TerminatorKind::Call {
267 func: Operand::Constant(box ConstOperand { const_, .. }),
268 args,
269 ..
270 } = &terminator.kind
271 && let ty::FnDef(id, _) = *const_.ty().kind()
272 {
273 debug!("add_moved_or_invoked_closure_note: id={:?}", id);
274 if self.infcx.tcx.is_lang_item(self.infcx.tcx.parent(id), LangItem::FnOnce) {
275 let closure = match args.first() {
276 Some(Spanned { node: Operand::Copy(place) | Operand::Move(place), .. })
277 if target == place.local_or_deref_local() =>
278 {
279 place.local_or_deref_local().unwrap()
280 }
281 _ => return false,
282 };
283
284 debug!("add_moved_or_invoked_closure_note: closure={:?}", closure);
285 if let ty::Closure(did, _) = self.body.local_decls[closure].ty.kind() {
286 let did = did.expect_local();
287 if let Some((span, hir_place)) = self.infcx.tcx.closure_kind_origin(did) {
288 diag.subdiagnostic(OnClosureNote::InvokedTwice {
289 place_name: &ty::place_to_string_for_capture(self.infcx.tcx, hir_place),
290 span: *span,
291 });
292 return true;
293 }
294 }
295 }
296 }
297
298 if let Some(target) = target
300 && let ty::Closure(did, _) = self.body.local_decls[target].ty.kind()
301 {
302 let did = did.expect_local();
303 if let Some((span, hir_place)) = self.infcx.tcx.closure_kind_origin(did) {
304 diag.subdiagnostic(OnClosureNote::MovedTwice {
305 place_name: &ty::place_to_string_for_capture(self.infcx.tcx, hir_place),
306 span: *span,
307 });
308 return true;
309 }
310 }
311 false
312 }
313
314 pub(super) fn describe_any_place(&self, place_ref: PlaceRef<'tcx>) -> String {
317 match self.describe_place(place_ref) {
318 Some(mut descr) => {
319 descr.reserve(2);
321 descr.insert(0, '`');
322 descr.push('`');
323 descr
324 }
325 None => "value".to_string(),
326 }
327 }
328
329 pub(super) fn describe_place(&self, place_ref: PlaceRef<'tcx>) -> Option<String> {
332 self.describe_place_with_options(
333 place_ref,
334 DescribePlaceOpt { including_downcast: false, including_tuple_field: true },
335 )
336 }
337
338 pub(super) fn describe_place_with_options(
343 &self,
344 place: PlaceRef<'tcx>,
345 opt: DescribePlaceOpt,
346 ) -> Option<String> {
347 let local = place.local;
348 if self.body.local_decls[local]
349 .source_info
350 .span
351 .in_external_macro(self.infcx.tcx.sess.source_map())
352 {
353 return None;
354 }
355
356 let mut autoderef_index = None;
357 let mut buf = String::new();
358 let mut ok = self.append_local_to_string(local, &mut buf);
359
360 for (index, elem) in place.projection.into_iter().enumerate() {
361 match elem {
362 ProjectionElem::Deref => {
363 if index == 0 {
364 if self.body.local_decls[local].is_ref_for_guard() {
365 continue;
366 }
367 if let LocalInfo::StaticRef { def_id, .. } =
368 *self.body.local_decls[local].local_info()
369 {
370 buf.push_str(self.infcx.tcx.item_name(def_id).as_str());
371 ok = Ok(());
372 continue;
373 }
374 }
375 if let Some(field) = self.is_upvar_field_projection(PlaceRef {
376 local,
377 projection: place.projection.split_at(index + 1).0,
378 }) {
379 let var_index = field.index();
380 buf = self.upvars[var_index].to_string(self.infcx.tcx);
381 ok = Ok(());
382 if !self.upvars[var_index].is_by_ref() {
383 buf.insert(0, '*');
384 }
385 } else {
386 if autoderef_index.is_none() {
387 autoderef_index = match place.projection.iter().rposition(|elem| {
388 !matches!(
389 elem,
390 ProjectionElem::Deref | ProjectionElem::Downcast(..)
391 )
392 }) {
393 Some(index) => Some(index + 1),
394 None => Some(0),
395 };
396 }
397 if index >= autoderef_index.unwrap() {
398 buf.insert(0, '*');
399 }
400 }
401 }
402 ProjectionElem::Downcast(..) if opt.including_downcast => return None,
403 ProjectionElem::Downcast(..) => (),
404 ProjectionElem::OpaqueCast(..) => (),
405 ProjectionElem::Subtype(..) => (),
406 ProjectionElem::UnwrapUnsafeBinder(_) => (),
407 ProjectionElem::Field(field, _ty) => {
408 if let Some(field) = self.is_upvar_field_projection(PlaceRef {
410 local,
411 projection: place.projection.split_at(index + 1).0,
412 }) {
413 buf = self.upvars[field.index()].to_string(self.infcx.tcx);
414 ok = Ok(());
415 } else {
416 let field_name = self.describe_field(
417 PlaceRef { local, projection: place.projection.split_at(index).0 },
418 *field,
419 IncludingTupleField(opt.including_tuple_field),
420 );
421 if let Some(field_name_str) = field_name {
422 buf.push('.');
423 buf.push_str(&field_name_str);
424 }
425 }
426 }
427 ProjectionElem::Index(index) => {
428 buf.push('[');
429 if self.append_local_to_string(*index, &mut buf).is_err() {
430 buf.push('_');
431 }
432 buf.push(']');
433 }
434 ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
435 buf.push_str("[..]");
439 }
440 }
441 }
442 ok.ok().map(|_| buf)
443 }
444
445 fn describe_name(&self, place: PlaceRef<'tcx>) -> Option<Symbol> {
446 for elem in place.projection.into_iter() {
447 match elem {
448 ProjectionElem::Downcast(Some(name), _) => {
449 return Some(*name);
450 }
451 _ => {}
452 }
453 }
454 None
455 }
456
457 fn append_local_to_string(&self, local: Local, buf: &mut String) -> Result<(), ()> {
460 let decl = &self.body.local_decls[local];
461 match self.local_name(local) {
462 Some(name) if !decl.from_compiler_desugaring() => {
463 buf.push_str(name.as_str());
464 Ok(())
465 }
466 _ => Err(()),
467 }
468 }
469
470 fn describe_field(
472 &self,
473 place: PlaceRef<'tcx>,
474 field: FieldIdx,
475 including_tuple_field: IncludingTupleField,
476 ) -> Option<String> {
477 let place_ty = match place {
478 PlaceRef { local, projection: [] } => PlaceTy::from_ty(self.body.local_decls[local].ty),
479 PlaceRef { local, projection: [proj_base @ .., elem] } => match elem {
480 ProjectionElem::Deref
481 | ProjectionElem::Index(..)
482 | ProjectionElem::ConstantIndex { .. }
483 | ProjectionElem::Subslice { .. } => {
484 PlaceRef { local, projection: proj_base }.ty(self.body, self.infcx.tcx)
485 }
486 ProjectionElem::Downcast(..) => place.ty(self.body, self.infcx.tcx),
487 ProjectionElem::Subtype(ty)
488 | ProjectionElem::OpaqueCast(ty)
489 | ProjectionElem::UnwrapUnsafeBinder(ty) => PlaceTy::from_ty(*ty),
490 ProjectionElem::Field(_, field_type) => PlaceTy::from_ty(*field_type),
491 },
492 };
493 self.describe_field_from_ty(
494 place_ty.ty,
495 field,
496 place_ty.variant_index,
497 including_tuple_field,
498 )
499 }
500
501 fn describe_field_from_ty(
503 &self,
504 ty: Ty<'_>,
505 field: FieldIdx,
506 variant_index: Option<VariantIdx>,
507 including_tuple_field: IncludingTupleField,
508 ) -> Option<String> {
509 if let Some(boxed_ty) = ty.boxed_ty() {
510 self.describe_field_from_ty(boxed_ty, field, variant_index, including_tuple_field)
512 } else {
513 match *ty.kind() {
514 ty::Adt(def, _) => {
515 let variant = if let Some(idx) = variant_index {
516 assert!(def.is_enum());
517 def.variant(idx)
518 } else {
519 def.non_enum_variant()
520 };
521 if !including_tuple_field.0 && variant.ctor_kind() == Some(CtorKind::Fn) {
522 return None;
523 }
524 Some(variant.fields[field].name.to_string())
525 }
526 ty::Tuple(_) => Some(field.index().to_string()),
527 ty::Ref(_, ty, _) | ty::RawPtr(ty, _) => {
528 self.describe_field_from_ty(ty, field, variant_index, including_tuple_field)
529 }
530 ty::Array(ty, _) | ty::Slice(ty) => {
531 self.describe_field_from_ty(ty, field, variant_index, including_tuple_field)
532 }
533 ty::Closure(def_id, _) | ty::Coroutine(def_id, _) => {
534 let def_id = def_id.expect_local();
539 let var_id =
540 self.infcx.tcx.closure_captures(def_id)[field.index()].get_root_variable();
541
542 Some(self.infcx.tcx.hir_name(var_id).to_string())
543 }
544 _ => {
545 bug!("End-user description not implemented for field access on `{:?}`", ty);
548 }
549 }
550 }
551 }
552
553 pub(super) fn borrowed_content_source(
554 &self,
555 deref_base: PlaceRef<'tcx>,
556 ) -> BorrowedContentSource<'tcx> {
557 let tcx = self.infcx.tcx;
558
559 match self.move_data.rev_lookup.find(deref_base) {
563 LookupResult::Exact(mpi) | LookupResult::Parent(Some(mpi)) => {
564 debug!("borrowed_content_source: mpi={:?}", mpi);
565
566 for i in &self.move_data.init_path_map[mpi] {
567 let init = &self.move_data.inits[*i];
568 debug!("borrowed_content_source: init={:?}", init);
569 let InitLocation::Statement(loc) = init.location else { continue };
572
573 let bbd = &self.body[loc.block];
574 let is_terminator = bbd.statements.len() == loc.statement_index;
575 debug!(
576 "borrowed_content_source: loc={:?} is_terminator={:?}",
577 loc, is_terminator,
578 );
579 if !is_terminator {
580 continue;
581 } else if let Some(Terminator {
582 kind:
583 TerminatorKind::Call {
584 func,
585 call_source: CallSource::OverloadedOperator,
586 ..
587 },
588 ..
589 }) = &bbd.terminator
590 {
591 if let Some(source) =
592 BorrowedContentSource::from_call(func.ty(self.body, tcx), tcx)
593 {
594 return source;
595 }
596 }
597 }
598 }
599 _ => (),
601 };
602
603 let base_ty = deref_base.ty(self.body, tcx).ty;
606 if base_ty.is_raw_ptr() {
607 BorrowedContentSource::DerefRawPointer
608 } else if base_ty.is_mutable_ptr() {
609 BorrowedContentSource::DerefMutableRef
610 } else {
611 BorrowedContentSource::DerefSharedRef
612 }
613 }
614
615 pub(super) fn get_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> String {
618 let mut p = ty::print::FmtPrinter::new(self.infcx.tcx, Namespace::TypeNS);
619
620 if let ty::Ref(region, ..) = ty.kind() {
624 match region.kind() {
625 ty::ReBound(_, ty::BoundRegion { kind: br, .. })
626 | ty::RePlaceholder(ty::PlaceholderRegion {
627 bound: ty::BoundRegion { kind: br, .. },
628 ..
629 }) => p.region_highlight_mode.highlighting_bound_region(br, counter),
630 _ => {}
631 }
632 }
633
634 ty.print(&mut p).unwrap();
635 p.into_buffer()
636 }
637
638 pub(super) fn get_region_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> String {
641 let mut p = ty::print::FmtPrinter::new(self.infcx.tcx, Namespace::TypeNS);
642
643 let region = if let ty::Ref(region, ..) = ty.kind() {
644 match region.kind() {
645 ty::ReBound(_, ty::BoundRegion { kind: br, .. })
646 | ty::RePlaceholder(ty::PlaceholderRegion {
647 bound: ty::BoundRegion { kind: br, .. },
648 ..
649 }) => p.region_highlight_mode.highlighting_bound_region(br, counter),
650 _ => {}
651 }
652 region
653 } else {
654 bug!("ty for annotation of borrow region is not a reference");
655 };
656
657 region.print(&mut p).unwrap();
658 p.into_buffer()
659 }
660
661 fn add_placeholder_from_predicate_note<G: EmissionGuarantee>(
667 &self,
668 diag: &mut Diag<'_, G>,
669 path: &[OutlivesConstraint<'tcx>],
670 ) {
671 let tcx = self.infcx.tcx;
672 let Some((gat_hir_id, generics)) = path.iter().find_map(|constraint| {
673 let outlived = constraint.sub;
674 if let Some(origin) = self.regioncx.definitions.get(outlived)
675 && let NllRegionVariableOrigin::Placeholder(placeholder) = origin.origin
676 && let Some(id) = placeholder.bound.kind.get_id()
677 && let Some(placeholder_id) = id.as_local()
678 && let gat_hir_id = tcx.local_def_id_to_hir_id(placeholder_id)
679 && let Some(generics_impl) =
680 tcx.parent_hir_node(tcx.parent_hir_id(gat_hir_id)).generics()
681 {
682 Some((gat_hir_id, generics_impl))
683 } else {
684 None
685 }
686 }) else {
687 return;
688 };
689
690 for pred in generics.predicates {
694 let WherePredicateKind::BoundPredicate(WhereBoundPredicate {
695 bound_generic_params,
696 bounds,
697 ..
698 }) = pred.kind
699 else {
700 continue;
701 };
702 if bound_generic_params
703 .iter()
704 .rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
705 .is_some()
706 {
707 diag.span_note(pred.span, fluent::borrowck_limitations_implies_static);
708 return;
709 }
710 for bound in bounds.iter() {
711 if let GenericBound::Trait(bound) = bound {
712 if bound
713 .bound_generic_params
714 .iter()
715 .rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
716 .is_some()
717 {
718 diag.span_note(bound.span, fluent::borrowck_limitations_implies_static);
719 return;
720 }
721 }
722 }
723 }
724 }
725
726 fn add_sized_or_copy_bound_info<G: EmissionGuarantee>(
729 &self,
730 err: &mut Diag<'_, G>,
731 blamed_category: ConstraintCategory<'tcx>,
732 path: &[OutlivesConstraint<'tcx>],
733 ) {
734 for sought_category in [ConstraintCategory::SizedBound, ConstraintCategory::CopyBound] {
735 if sought_category != blamed_category
736 && let Some(sought_constraint) = path.iter().find(|c| c.category == sought_category)
737 {
738 let label = format!(
739 "requirement occurs due to {}",
740 sought_category.description().trim_end()
741 );
742 err.span_label(sought_constraint.span, label);
743 }
744 }
745 }
746}
747
748#[derive(Copy, Clone, PartialEq, Eq, Debug)]
750pub(super) enum UseSpans<'tcx> {
751 ClosureUse {
753 closure_kind: hir::ClosureKind,
755 args_span: Span,
758 capture_kind_span: Span,
761 path_span: Span,
764 },
765 FnSelfUse {
768 var_span: Span,
770 fn_call_span: Span,
772 fn_span: Span,
774 kind: CallKind<'tcx>,
775 },
776 PatUse(Span),
778 OtherUse(Span),
780}
781
782impl UseSpans<'_> {
783 pub(super) fn args_or_use(self) -> Span {
784 match self {
785 UseSpans::ClosureUse { args_span: span, .. }
786 | UseSpans::PatUse(span)
787 | UseSpans::OtherUse(span) => span,
788 UseSpans::FnSelfUse { var_span, .. } => var_span,
789 }
790 }
791
792 pub(super) fn var_or_use_path_span(self) -> Span {
794 match self {
795 UseSpans::ClosureUse { path_span: span, .. }
796 | UseSpans::PatUse(span)
797 | UseSpans::OtherUse(span) => span,
798 UseSpans::FnSelfUse { var_span, .. } => var_span,
799 }
800 }
801
802 pub(super) fn var_or_use(self) -> Span {
804 match self {
805 UseSpans::ClosureUse { capture_kind_span: span, .. }
806 | UseSpans::PatUse(span)
807 | UseSpans::OtherUse(span) => span,
808 UseSpans::FnSelfUse { var_span, .. } => var_span,
809 }
810 }
811
812 pub(super) fn coroutine_kind(self) -> Option<CoroutineKind> {
814 match self {
815 UseSpans::ClosureUse {
816 closure_kind: hir::ClosureKind::Coroutine(coroutine_kind),
817 ..
818 } => Some(coroutine_kind),
819 _ => None,
820 }
821 }
822
823 #[allow(rustc::diagnostic_outside_of_impl)]
825 pub(super) fn args_subdiag(self, err: &mut Diag<'_>, f: impl FnOnce(Span) -> CaptureArgLabel) {
826 if let UseSpans::ClosureUse { args_span, .. } = self {
827 err.subdiagnostic(f(args_span));
828 }
829 }
830
831 #[allow(rustc::diagnostic_outside_of_impl)]
834 pub(super) fn var_path_only_subdiag(
835 self,
836 err: &mut Diag<'_>,
837 action: crate::InitializationRequiringAction,
838 ) {
839 use CaptureVarPathUseCause::*;
840
841 use crate::InitializationRequiringAction::*;
842 if let UseSpans::ClosureUse { closure_kind, path_span, .. } = self {
843 match closure_kind {
844 hir::ClosureKind::Coroutine(_) => {
845 err.subdiagnostic(match action {
846 Borrow => BorrowInCoroutine { path_span },
847 MatchOn | Use => UseInCoroutine { path_span },
848 Assignment => AssignInCoroutine { path_span },
849 PartialAssignment => AssignPartInCoroutine { path_span },
850 });
851 }
852 hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
853 err.subdiagnostic(match action {
854 Borrow => BorrowInClosure { path_span },
855 MatchOn | Use => UseInClosure { path_span },
856 Assignment => AssignInClosure { path_span },
857 PartialAssignment => AssignPartInClosure { path_span },
858 });
859 }
860 }
861 }
862 }
863
864 #[allow(rustc::diagnostic_outside_of_impl)]
866 pub(super) fn var_subdiag(
867 self,
868 err: &mut Diag<'_>,
869 kind: Option<rustc_middle::mir::BorrowKind>,
870 f: impl FnOnce(hir::ClosureKind, Span) -> CaptureVarCause,
871 ) {
872 if let UseSpans::ClosureUse { closure_kind, capture_kind_span, path_span, .. } = self {
873 if capture_kind_span != path_span {
874 err.subdiagnostic(match kind {
875 Some(kd) => match kd {
876 rustc_middle::mir::BorrowKind::Shared
877 | rustc_middle::mir::BorrowKind::Fake(_) => {
878 CaptureVarKind::Immut { kind_span: capture_kind_span }
879 }
880
881 rustc_middle::mir::BorrowKind::Mut { .. } => {
882 CaptureVarKind::Mut { kind_span: capture_kind_span }
883 }
884 },
885 None => CaptureVarKind::Move { kind_span: capture_kind_span },
886 });
887 };
888 let diag = f(closure_kind, path_span);
889 err.subdiagnostic(diag);
890 }
891 }
892
893 pub(super) fn for_closure(&self) -> bool {
895 match *self {
896 UseSpans::ClosureUse { closure_kind, .. } => {
897 matches!(closure_kind, hir::ClosureKind::Closure)
898 }
899 _ => false,
900 }
901 }
902
903 pub(super) fn for_coroutine(&self) -> bool {
905 match *self {
906 UseSpans::ClosureUse { closure_kind, .. } => {
908 matches!(closure_kind, hir::ClosureKind::Coroutine(..))
909 }
910 _ => false,
911 }
912 }
913
914 pub(super) fn or_else<F>(self, if_other: F) -> Self
915 where
916 F: FnOnce() -> Self,
917 {
918 match self {
919 closure @ UseSpans::ClosureUse { .. } => closure,
920 UseSpans::PatUse(_) | UseSpans::OtherUse(_) => if_other(),
921 fn_self @ UseSpans::FnSelfUse { .. } => fn_self,
922 }
923 }
924}
925
926pub(super) enum BorrowedContentSource<'tcx> {
927 DerefRawPointer,
928 DerefMutableRef,
929 DerefSharedRef,
930 OverloadedDeref(Ty<'tcx>),
931 OverloadedIndex(Ty<'tcx>),
932}
933
934impl<'tcx> BorrowedContentSource<'tcx> {
935 pub(super) fn describe_for_unnamed_place(&self, tcx: TyCtxt<'_>) -> String {
936 match *self {
937 BorrowedContentSource::DerefRawPointer => "a raw pointer".to_string(),
938 BorrowedContentSource::DerefSharedRef => "a shared reference".to_string(),
939 BorrowedContentSource::DerefMutableRef => "a mutable reference".to_string(),
940 BorrowedContentSource::OverloadedDeref(ty) => ty
941 .ty_adt_def()
942 .and_then(|adt| match tcx.get_diagnostic_name(adt.did())? {
943 name @ (sym::Rc | sym::Arc) => Some(format!("an `{name}`")),
944 _ => None,
945 })
946 .unwrap_or_else(|| format!("dereference of `{ty}`")),
947 BorrowedContentSource::OverloadedIndex(ty) => format!("index of `{ty}`"),
948 }
949 }
950
951 pub(super) fn describe_for_named_place(&self) -> Option<&'static str> {
952 match *self {
953 BorrowedContentSource::DerefRawPointer => Some("raw pointer"),
954 BorrowedContentSource::DerefSharedRef => Some("shared reference"),
955 BorrowedContentSource::DerefMutableRef => Some("mutable reference"),
956 BorrowedContentSource::OverloadedDeref(_)
959 | BorrowedContentSource::OverloadedIndex(_) => None,
960 }
961 }
962
963 pub(super) fn describe_for_immutable_place(&self, tcx: TyCtxt<'_>) -> String {
964 match *self {
965 BorrowedContentSource::DerefRawPointer => "a `*const` pointer".to_string(),
966 BorrowedContentSource::DerefSharedRef => "a `&` reference".to_string(),
967 BorrowedContentSource::DerefMutableRef => {
968 bug!("describe_for_immutable_place: DerefMutableRef isn't immutable")
969 }
970 BorrowedContentSource::OverloadedDeref(ty) => ty
971 .ty_adt_def()
972 .and_then(|adt| match tcx.get_diagnostic_name(adt.did())? {
973 name @ (sym::Rc | sym::Arc) => Some(format!("an `{name}`")),
974 _ => None,
975 })
976 .unwrap_or_else(|| format!("dereference of `{ty}`")),
977 BorrowedContentSource::OverloadedIndex(ty) => format!("an index of `{ty}`"),
978 }
979 }
980
981 fn from_call(func: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<Self> {
982 match *func.kind() {
983 ty::FnDef(def_id, args) => {
984 let trait_id = tcx.trait_of_assoc(def_id)?;
985
986 if tcx.is_lang_item(trait_id, LangItem::Deref)
987 || tcx.is_lang_item(trait_id, LangItem::DerefMut)
988 {
989 Some(BorrowedContentSource::OverloadedDeref(args.type_at(0)))
990 } else if tcx.is_lang_item(trait_id, LangItem::Index)
991 || tcx.is_lang_item(trait_id, LangItem::IndexMut)
992 {
993 Some(BorrowedContentSource::OverloadedIndex(args.type_at(0)))
994 } else {
995 None
996 }
997 }
998 _ => None,
999 }
1000 }
1001}
1002
1003struct CapturedMessageOpt {
1005 is_partial_move: bool,
1006 is_loop_message: bool,
1007 is_move_msg: bool,
1008 is_loop_move: bool,
1009 has_suggest_reborrow: bool,
1010 maybe_reinitialized_locations_is_empty: bool,
1011}
1012
1013impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
1014 pub(super) fn move_spans(
1016 &self,
1017 moved_place: PlaceRef<'tcx>, location: Location,
1019 ) -> UseSpans<'tcx> {
1020 use self::UseSpans::*;
1021
1022 let Some(stmt) = self.body[location.block].statements.get(location.statement_index) else {
1023 return OtherUse(self.body.source_info(location).span);
1024 };
1025
1026 debug!("move_spans: moved_place={:?} location={:?} stmt={:?}", moved_place, location, stmt);
1027 if let StatementKind::Assign(box (_, Rvalue::Aggregate(kind, places))) = &stmt.kind
1028 && let AggregateKind::Closure(def_id, _) | AggregateKind::Coroutine(def_id, _) = **kind
1029 {
1030 debug!("move_spans: def_id={:?} places={:?}", def_id, places);
1031 let def_id = def_id.expect_local();
1032 if let Some((args_span, closure_kind, capture_kind_span, path_span)) =
1033 self.closure_span(def_id, moved_place, places)
1034 {
1035 return ClosureUse { closure_kind, args_span, capture_kind_span, path_span };
1036 }
1037 }
1038
1039 if let StatementKind::FakeRead(box (cause, place)) = stmt.kind {
1042 match cause {
1043 FakeReadCause::ForMatchedPlace(Some(closure_def_id))
1044 | FakeReadCause::ForLet(Some(closure_def_id)) => {
1045 debug!("move_spans: def_id={:?} place={:?}", closure_def_id, place);
1046 let places = &[Operand::Move(place)];
1047 if let Some((args_span, closure_kind, capture_kind_span, path_span)) =
1048 self.closure_span(closure_def_id, moved_place, IndexSlice::from_raw(places))
1049 {
1050 return ClosureUse {
1051 closure_kind,
1052 args_span,
1053 capture_kind_span,
1054 path_span,
1055 };
1056 }
1057 }
1058 _ => {}
1059 }
1060 }
1061
1062 let normal_ret =
1063 if moved_place.projection.iter().any(|p| matches!(p, ProjectionElem::Downcast(..))) {
1064 PatUse(stmt.source_info.span)
1065 } else {
1066 OtherUse(stmt.source_info.span)
1067 };
1068
1069 let target_temp = match stmt.kind {
1081 StatementKind::Assign(box (temp, _)) if temp.as_local().is_some() => {
1082 temp.as_local().unwrap()
1083 }
1084 _ => return normal_ret,
1085 };
1086
1087 debug!("move_spans: target_temp = {:?}", target_temp);
1088
1089 if let Some(Terminator {
1090 kind: TerminatorKind::Call { fn_span, call_source, .. }, ..
1091 }) = &self.body[location.block].terminator
1092 {
1093 let Some((method_did, method_args)) =
1094 find_self_call(self.infcx.tcx, self.body, target_temp, location.block)
1095 else {
1096 return normal_ret;
1097 };
1098
1099 let kind = call_kind(
1100 self.infcx.tcx,
1101 self.infcx.typing_env(self.infcx.param_env),
1102 method_did,
1103 method_args,
1104 *fn_span,
1105 call_source.from_hir_call(),
1106 self.infcx.tcx.fn_arg_idents(method_did)[0],
1107 );
1108
1109 return FnSelfUse {
1110 var_span: stmt.source_info.span,
1111 fn_call_span: *fn_span,
1112 fn_span: self.infcx.tcx.def_span(method_did),
1113 kind,
1114 };
1115 }
1116
1117 normal_ret
1118 }
1119
1120 pub(super) fn borrow_spans(&self, use_span: Span, location: Location) -> UseSpans<'tcx> {
1125 use self::UseSpans::*;
1126 debug!("borrow_spans: use_span={:?} location={:?}", use_span, location);
1127
1128 let target = match self.body[location.block].statements.get(location.statement_index) {
1129 Some(Statement { kind: StatementKind::Assign(box (place, _)), .. }) => {
1130 if let Some(local) = place.as_local() {
1131 local
1132 } else {
1133 return OtherUse(use_span);
1134 }
1135 }
1136 _ => return OtherUse(use_span),
1137 };
1138
1139 if self.body.local_kind(target) != LocalKind::Temp {
1140 return OtherUse(use_span);
1142 }
1143
1144 let maybe_additional_statement =
1146 if let TerminatorKind::Drop { target: drop_target, .. } =
1147 self.body[location.block].terminator().kind
1148 {
1149 self.body[drop_target].statements.first()
1150 } else {
1151 None
1152 };
1153
1154 let statements =
1155 self.body[location.block].statements[location.statement_index + 1..].iter();
1156
1157 for stmt in statements.chain(maybe_additional_statement) {
1158 if let StatementKind::Assign(box (_, Rvalue::Aggregate(kind, places))) = &stmt.kind {
1159 let (&def_id, is_coroutine) = match kind {
1160 box AggregateKind::Closure(def_id, _) => (def_id, false),
1161 box AggregateKind::Coroutine(def_id, _) => (def_id, true),
1162 _ => continue,
1163 };
1164 let def_id = def_id.expect_local();
1165
1166 debug!(
1167 "borrow_spans: def_id={:?} is_coroutine={:?} places={:?}",
1168 def_id, is_coroutine, places
1169 );
1170 if let Some((args_span, closure_kind, capture_kind_span, path_span)) =
1171 self.closure_span(def_id, Place::from(target).as_ref(), places)
1172 {
1173 return ClosureUse { closure_kind, args_span, capture_kind_span, path_span };
1174 } else {
1175 return OtherUse(use_span);
1176 }
1177 }
1178
1179 if use_span != stmt.source_info.span {
1180 break;
1181 }
1182 }
1183
1184 OtherUse(use_span)
1185 }
1186
1187 fn closure_span(
1191 &self,
1192 def_id: LocalDefId,
1193 target_place: PlaceRef<'tcx>,
1194 places: &IndexSlice<FieldIdx, Operand<'tcx>>,
1195 ) -> Option<(Span, hir::ClosureKind, Span, Span)> {
1196 debug!(
1197 "closure_span: def_id={:?} target_place={:?} places={:?}",
1198 def_id, target_place, places
1199 );
1200 let hir_id = self.infcx.tcx.local_def_id_to_hir_id(def_id);
1201 let expr = &self.infcx.tcx.hir_expect_expr(hir_id).kind;
1202 debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr);
1203 if let &hir::ExprKind::Closure(&hir::Closure { kind, fn_decl_span, .. }) = expr {
1204 for (captured_place, place) in
1205 self.infcx.tcx.closure_captures(def_id).iter().zip(places)
1206 {
1207 match place {
1208 Operand::Copy(place) | Operand::Move(place)
1209 if target_place == place.as_ref() =>
1210 {
1211 debug!("closure_span: found captured local {:?}", place);
1212 return Some((
1213 fn_decl_span,
1214 kind,
1215 captured_place.get_capture_kind_span(self.infcx.tcx),
1216 captured_place.get_path_span(self.infcx.tcx),
1217 ));
1218 }
1219 _ => {}
1220 }
1221 }
1222 }
1223 None
1224 }
1225
1226 pub(super) fn retrieve_borrow_spans(&self, borrow: &BorrowData<'_>) -> UseSpans<'tcx> {
1229 let span = self.body.source_info(borrow.reserve_location).span;
1230 self.borrow_spans(span, borrow.reserve_location)
1231 }
1232
1233 #[allow(rustc::diagnostic_outside_of_impl)]
1234 #[allow(rustc::untranslatable_diagnostic)] fn explain_captures(
1236 &mut self,
1237 err: &mut Diag<'infcx>,
1238 span: Span,
1239 move_span: Span,
1240 move_spans: UseSpans<'tcx>,
1241 moved_place: Place<'tcx>,
1242 msg_opt: CapturedMessageOpt,
1243 ) {
1244 let CapturedMessageOpt {
1245 is_partial_move: is_partial,
1246 is_loop_message,
1247 is_move_msg,
1248 is_loop_move,
1249 has_suggest_reborrow,
1250 maybe_reinitialized_locations_is_empty,
1251 } = msg_opt;
1252 if let UseSpans::FnSelfUse { var_span, fn_call_span, fn_span, kind } = move_spans {
1253 let place_name = self
1254 .describe_place(moved_place.as_ref())
1255 .map(|n| format!("`{n}`"))
1256 .unwrap_or_else(|| "value".to_owned());
1257 match kind {
1258 CallKind::FnCall { fn_trait_id, self_ty }
1259 if self.infcx.tcx.is_lang_item(fn_trait_id, LangItem::FnOnce) =>
1260 {
1261 err.subdiagnostic(CaptureReasonLabel::Call {
1262 fn_call_span,
1263 place_name: &place_name,
1264 is_partial,
1265 is_loop_message,
1266 });
1267 if let ty::Param(param_ty) = *self_ty.kind()
1289 && let generics = self.infcx.tcx.generics_of(self.mir_def_id())
1290 && let param = generics.type_param(param_ty, self.infcx.tcx)
1291 && let Some(hir_generics) = self
1292 .infcx
1293 .tcx
1294 .typeck_root_def_id(self.mir_def_id().to_def_id())
1295 .as_local()
1296 .and_then(|def_id| self.infcx.tcx.hir_get_generics(def_id))
1297 && let spans = hir_generics
1298 .predicates
1299 .iter()
1300 .filter_map(|pred| match pred.kind {
1301 hir::WherePredicateKind::BoundPredicate(pred) => Some(pred),
1302 _ => None,
1303 })
1304 .filter(|pred| {
1305 if let Some((id, _)) = pred.bounded_ty.as_generic_param() {
1306 id == param.def_id
1307 } else {
1308 false
1309 }
1310 })
1311 .flat_map(|pred| pred.bounds)
1312 .filter_map(|bound| {
1313 if let Some(trait_ref) = bound.trait_ref()
1314 && let Some(trait_def_id) = trait_ref.trait_def_id()
1315 && trait_def_id == fn_trait_id
1316 {
1317 Some(bound.span())
1318 } else {
1319 None
1320 }
1321 })
1322 .collect::<Vec<Span>>()
1323 && !spans.is_empty()
1324 {
1325 let mut span: MultiSpan = spans.clone().into();
1326 err.arg("ty", param_ty.to_string());
1327 let msg = err.dcx.eagerly_translate_to_string(
1328 fluent::borrowck_moved_a_fn_once_in_call_def,
1329 err.args.iter(),
1330 );
1331 err.remove_arg("ty");
1332 for sp in spans {
1333 span.push_span_label(sp, msg.clone());
1334 }
1335 span.push_span_label(
1336 fn_call_span,
1337 fluent::borrowck_moved_a_fn_once_in_call,
1338 );
1339 err.span_note(span, fluent::borrowck_moved_a_fn_once_in_call_call);
1340 } else {
1341 err.subdiagnostic(CaptureReasonNote::FnOnceMoveInCall { var_span });
1342 }
1343 }
1344 CallKind::Operator { self_arg, trait_id, .. } => {
1345 let self_arg = self_arg.unwrap();
1346 err.subdiagnostic(CaptureReasonLabel::OperatorUse {
1347 fn_call_span,
1348 place_name: &place_name,
1349 is_partial,
1350 is_loop_message,
1351 });
1352 if self.fn_self_span_reported.insert(fn_span) {
1353 let lang = self.infcx.tcx.lang_items();
1354 err.subdiagnostic(
1355 if [lang.not_trait(), lang.deref_trait(), lang.neg_trait()]
1356 .contains(&Some(trait_id))
1357 {
1358 CaptureReasonNote::UnOpMoveByOperator { span: self_arg.span }
1359 } else {
1360 CaptureReasonNote::LhsMoveByOperator { span: self_arg.span }
1361 },
1362 );
1363 }
1364 }
1365 CallKind::Normal { self_arg, desugaring, method_did, method_args } => {
1366 let self_arg = self_arg.unwrap();
1367 let mut has_sugg = false;
1368 let tcx = self.infcx.tcx;
1369 if span != DUMMY_SP && self.fn_self_span_reported.insert(self_arg.span) {
1372 self.explain_iterator_advancement_in_for_loop_if_applicable(
1373 err,
1374 span,
1375 &move_spans,
1376 );
1377
1378 let func = tcx.def_path_str(method_did);
1379 err.subdiagnostic(CaptureReasonNote::FuncTakeSelf {
1380 func,
1381 place_name: place_name.clone(),
1382 span: self_arg.span,
1383 });
1384 }
1385 let parent_did = tcx.parent(method_did);
1386 let parent_self_ty =
1387 matches!(tcx.def_kind(parent_did), rustc_hir::def::DefKind::Impl { .. })
1388 .then_some(parent_did)
1389 .and_then(|did| match tcx.type_of(did).instantiate_identity().kind() {
1390 ty::Adt(def, ..) => Some(def.did()),
1391 _ => None,
1392 });
1393 let is_option_or_result = parent_self_ty.is_some_and(|def_id| {
1394 matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
1395 });
1396 if is_option_or_result && maybe_reinitialized_locations_is_empty {
1397 err.subdiagnostic(CaptureReasonLabel::BorrowContent { var_span });
1398 }
1399 if let Some((CallDesugaringKind::ForLoopIntoIter, _)) = desugaring {
1400 let ty = moved_place.ty(self.body, tcx).ty;
1401 let suggest = match tcx.get_diagnostic_item(sym::IntoIterator) {
1402 Some(def_id) => type_known_to_meet_bound_modulo_regions(
1403 self.infcx,
1404 self.infcx.param_env,
1405 Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, ty),
1406 def_id,
1407 ),
1408 _ => false,
1409 };
1410 if suggest {
1411 err.subdiagnostic(CaptureReasonSuggest::IterateSlice {
1412 ty,
1413 span: move_span.shrink_to_lo(),
1414 });
1415 }
1416
1417 err.subdiagnostic(CaptureReasonLabel::ImplicitCall {
1418 fn_call_span,
1419 place_name: &place_name,
1420 is_partial,
1421 is_loop_message,
1422 });
1423 if let ty::Ref(_, _, hir::Mutability::Mut) =
1427 moved_place.ty(self.body, self.infcx.tcx).ty.kind()
1428 {
1429 if !is_loop_move && !has_suggest_reborrow {
1434 self.suggest_reborrow(
1435 err,
1436 move_span.shrink_to_lo(),
1437 moved_place.as_ref(),
1438 );
1439 }
1440 }
1441 } else {
1442 if let Some((CallDesugaringKind::Await, _)) = desugaring {
1443 err.subdiagnostic(CaptureReasonLabel::Await {
1444 fn_call_span,
1445 place_name: &place_name,
1446 is_partial,
1447 is_loop_message,
1448 });
1449 } else {
1450 err.subdiagnostic(CaptureReasonLabel::MethodCall {
1451 fn_call_span,
1452 place_name: &place_name,
1453 is_partial,
1454 is_loop_message,
1455 });
1456 }
1457 let ty = moved_place.ty(self.body, tcx).ty;
1459
1460 if let ty::Adt(def, args) = ty.peel_refs().kind()
1461 && tcx.is_lang_item(def.did(), LangItem::Pin)
1462 && let ty::Ref(_, _, hir::Mutability::Mut) = args.type_at(0).kind()
1463 && let self_ty = self.infcx.instantiate_binder_with_fresh_vars(
1464 fn_call_span,
1465 BoundRegionConversionTime::FnCall,
1466 tcx.fn_sig(method_did).instantiate(tcx, method_args).input(0),
1467 )
1468 && self.infcx.can_eq(self.infcx.param_env, ty, self_ty)
1469 {
1470 err.subdiagnostic(CaptureReasonSuggest::FreshReborrow {
1471 span: move_span.shrink_to_hi(),
1472 });
1473 has_sugg = true;
1474 }
1475 if let Some(clone_trait) = tcx.lang_items().clone_trait() {
1476 let sugg = if moved_place
1477 .iter_projections()
1478 .any(|(_, elem)| matches!(elem, ProjectionElem::Deref))
1479 {
1480 let (start, end) = if let Some(expr) = self.find_expr(move_span)
1481 && let Some(_) = self.clone_on_reference(expr)
1482 && let hir::ExprKind::MethodCall(_, rcvr, _, _) = expr.kind
1483 {
1484 (move_span.shrink_to_lo(), move_span.with_lo(rcvr.span.hi()))
1485 } else {
1486 (move_span.shrink_to_lo(), move_span.shrink_to_hi())
1487 };
1488 vec![
1489 (start, format!("<{ty} as Clone>::clone(&")),
1494 (end, ")".to_string()),
1495 ]
1496 } else {
1497 vec![(move_span.shrink_to_hi(), ".clone()".to_string())]
1498 };
1499 if let Some(errors) = self.infcx.type_implements_trait_shallow(
1500 clone_trait,
1501 ty,
1502 self.infcx.param_env,
1503 ) && !has_sugg
1504 {
1505 let msg = match &errors[..] {
1506 [] => "you can `clone` the value and consume it, but this \
1507 might not be your desired behavior"
1508 .to_string(),
1509 [error] => {
1510 format!(
1511 "you could `clone` the value and consume it, if the \
1512 `{}` trait bound could be satisfied",
1513 error.obligation.predicate,
1514 )
1515 }
1516 _ => {
1517 format!(
1518 "you could `clone` the value and consume it, if the \
1519 following trait bounds could be satisfied: {}",
1520 listify(&errors, |e: &FulfillmentError<'tcx>| format!(
1521 "`{}`",
1522 e.obligation.predicate
1523 ))
1524 .unwrap(),
1525 )
1526 }
1527 };
1528 err.multipart_suggestion_verbose(
1529 msg,
1530 sugg,
1531 Applicability::MaybeIncorrect,
1532 );
1533 for error in errors {
1534 if let FulfillmentErrorCode::Select(
1535 SelectionError::Unimplemented,
1536 ) = error.code
1537 && let ty::PredicateKind::Clause(ty::ClauseKind::Trait(
1538 pred,
1539 )) = error.obligation.predicate.kind().skip_binder()
1540 {
1541 self.infcx.err_ctxt().suggest_derive(
1542 &error.obligation,
1543 err,
1544 error.obligation.predicate.kind().rebind(pred),
1545 );
1546 }
1547 }
1548 }
1549 }
1550 }
1551 }
1552 _ => {}
1554 }
1555 } else {
1556 if move_span != span || is_loop_message {
1557 err.subdiagnostic(CaptureReasonLabel::MovedHere {
1558 move_span,
1559 is_partial,
1560 is_move_msg,
1561 is_loop_message,
1562 });
1563 }
1564 if !is_loop_message {
1567 move_spans.var_subdiag(err, None, |kind, var_span| match kind {
1568 hir::ClosureKind::Coroutine(_) => {
1569 CaptureVarCause::PartialMoveUseInCoroutine { var_span, is_partial }
1570 }
1571 hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
1572 CaptureVarCause::PartialMoveUseInClosure { var_span, is_partial }
1573 }
1574 })
1575 }
1576 }
1577 }
1578
1579 pub(crate) fn local_excluded_from_unused_mut_lint(&self, index: Local) -> bool {
1581 self.local_name(index).is_none_or(|name| name.as_str().starts_with('_'))
1582 }
1583}