1 use rustc_errors::{Applicability, Diagnostic};
3 use rustc_hir::intravisit::Visitor;
5 use rustc_middle::hir::map::Map;
6 use rustc_middle::mir::{Mutability, Place, PlaceRef, ProjectionElem};
7 use rustc_middle::ty::{self, Ty, TyCtxt};
10 mir::{self, BindingForm, ClearCrossCrate, Local, LocalDecl, LocalInfo, LocalKind, Location},
12 use rustc_span::source_map::DesugaringKind;
13 use rustc_span::symbol::{kw, Symbol};
14 use rustc_span::{sym, BytePos, Span};
16 use crate::diagnostics::BorrowedContentSource;
17 use crate::MirBorrowckCtxt;
18 use rustc_const_eval::util::collect_writes::FindAssignments;
20 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
21 pub(crate) enum AccessKind {
26 impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
27 pub(crate) fn report_mutability_error(
29 access_place: Place<'tcx>,
31 the_place_err: PlaceRef<'tcx>,
32 error_access: AccessKind,
36 "report_mutability_error(\
37 access_place={:?}, span={:?}, the_place_err={:?}, error_access={:?}, location={:?},\
39 access_place, span, the_place_err, error_access, location,
45 let mut opt_source = None;
46 let access_place_desc = self.describe_any_place(access_place.as_ref());
47 debug!("report_mutability_error: access_place_desc={:?}", access_place_desc);
50 PlaceRef { local, projection: [] } => {
51 item_msg = access_place_desc;
52 if access_place.as_local().is_some() {
53 reason = ", as it is not declared as mutable".to_string();
55 let name = self.local_names[local].expect("immutable unnamed local");
56 reason = format!(", as `{name}` is not declared as mutable");
62 projection: [proj_base @ .., ProjectionElem::Field(upvar_index, _)],
64 debug_assert!(is_closure_or_generator(
65 Place::ty_from(local, proj_base, self.body, self.infcx.tcx).ty
68 let imm_borrow_derefed = self.upvars[upvar_index.index()]
72 .any(|ty| matches!(ty.kind(), ty::Ref(.., hir::Mutability::Not)));
74 // If the place is immutable then:
76 // - Either we deref an immutable ref to get to our final place.
77 // - We don't capture derefs of raw ptrs
78 // - Or the final place is immut because the root variable of the capture
79 // isn't marked mut and we should suggest that to the user.
80 if imm_borrow_derefed {
81 // If we deref an immutable ref then the suggestion here doesn't help.
84 item_msg = access_place_desc;
85 if self.is_upvar_field_projection(access_place.as_ref()).is_some() {
86 reason = ", as it is not declared as mutable".to_string();
88 let name = self.upvars[upvar_index.index()].place.to_string(self.infcx.tcx);
89 reason = format!(", as `{name}` is not declared as mutable");
94 PlaceRef { local, projection: [ProjectionElem::Deref] }
95 if self.body.local_decls[local].is_ref_for_guard() =>
97 item_msg = access_place_desc;
98 reason = ", as it is immutable for the pattern guard".to_string();
100 PlaceRef { local, projection: [ProjectionElem::Deref] }
101 if self.body.local_decls[local].is_ref_to_static() =>
103 if access_place.projection.len() == 1 {
104 item_msg = format!("immutable static item {access_place_desc}");
105 reason = String::new();
107 item_msg = access_place_desc;
108 let local_info = &self.body.local_decls[local].local_info;
109 if let Some(box LocalInfo::StaticRef { def_id, .. }) = *local_info {
110 let static_name = &self.infcx.tcx.item_name(def_id);
111 reason = format!(", as `{static_name}` is an immutable static item");
113 bug!("is_ref_to_static return true, but not ref to static?");
117 PlaceRef { local: _, projection: [proj_base @ .., ProjectionElem::Deref] } => {
118 if the_place_err.local == ty::CAPTURE_STRUCT_LOCAL
119 && proj_base.is_empty()
120 && !self.upvars.is_empty()
122 item_msg = access_place_desc;
124 self.body.local_decls[ty::CAPTURE_STRUCT_LOCAL].ty.is_region_ptr()
126 debug_assert!(is_closure_or_generator(
129 the_place_err.projection,
136 reason = if self.is_upvar_field_projection(access_place.as_ref()).is_some() {
137 ", as it is a captured variable in a `Fn` closure".to_string()
139 ", as `Fn` closures cannot mutate their captured variables".to_string()
142 let source = self.borrowed_content_source(PlaceRef {
143 local: the_place_err.local,
144 projection: proj_base,
146 let pointer_type = source.describe_for_immutable_place(self.infcx.tcx);
147 opt_source = Some(source);
148 if let Some(desc) = self.describe_place(access_place.as_ref()) {
149 item_msg = format!("`{desc}`");
150 reason = match error_access {
151 AccessKind::Mutate => format!(", which is behind {pointer_type}"),
152 AccessKind::MutableBorrow => {
153 format!(", as it is behind {pointer_type}")
157 item_msg = format!("data in {pointer_type}");
158 reason = String::new();
168 ProjectionElem::Index(_)
169 | ProjectionElem::ConstantIndex { .. }
170 | ProjectionElem::OpaqueCast { .. }
171 | ProjectionElem::Subslice { .. }
172 | ProjectionElem::Downcast(..),
174 } => bug!("Unexpected immutable place."),
177 debug!("report_mutability_error: item_msg={:?}, reason={:?}", item_msg, reason);
179 // `act` and `acted_on` are strings that let us abstract over
180 // the verbs used in some diagnostic messages.
183 let mut suggest = true;
184 let mut mut_error = None;
187 let span = match error_access {
188 AccessKind::Mutate => {
189 err = self.cannot_assign(span, &(item_msg + &reason));
191 acted_on = "written";
194 AccessKind::MutableBorrow => {
195 act = "borrow as mutable";
196 acted_on = "borrowed as mutable";
198 let borrow_spans = self.borrow_spans(span, location);
199 let borrow_span = borrow_spans.args_or_use();
200 match the_place_err {
201 PlaceRef { local, projection: [] }
202 if self.body.local_decls[local].can_be_made_mutable() =>
204 let span = self.body.local_decls[local].source_info.span;
205 mut_error = Some(span);
206 if let Some((buffer, c)) = self.get_buffered_mut_error(span) {
207 // We've encountered a second (or more) attempt to mutably borrow an
208 // immutable binding, so the likely problem is with the binding
209 // declaration, not the use. We collect these in a single diagnostic
210 // and make the binding the primary span of the error.
214 err.replace_span_with(span, false);
215 err.span_label(span, "not mutable");
219 err = self.cannot_borrow_path_as_mutable_because(
227 err = self.cannot_borrow_path_as_mutable_because(
235 borrow_spans.var_span_label(
238 "mutable borrow occurs due to use of {} in closure",
239 self.describe_any_place(access_place.as_ref()),
248 debug!("report_mutability_error: act={:?}, acted_on={:?}", act, acted_on);
250 match the_place_err {
251 // Suggest making an existing shared borrow in a struct definition a mutable borrow.
253 // This is applicable when we have a deref of a field access to a deref of a local -
254 // something like `*((*_1).0`. The local that we get will be a reference to the
255 // struct we've got a field access of (it must be a reference since there's a deref
256 // after the field access).
262 ProjectionElem::Deref,
263 ProjectionElem::Field(field, _),
264 ProjectionElem::Deref,
267 err.span_label(span, format!("cannot {ACT}", ACT = act));
269 if let Some(span) = get_mut_span_in_struct_field(
271 Place::ty_from(local, proj_base, self.body, self.infcx.tcx).ty,
274 err.span_suggestion_verbose(
276 "consider changing this to be mutable",
278 Applicability::MaybeIncorrect,
283 // Suggest removing a `&mut` from the use of a mutable reference.
284 PlaceRef { local, projection: [] }
289 .map(|l| mut_borrow_of_mutable_ref(l, self.local_names[local]))
292 let decl = &self.body.local_decls[local];
293 err.span_label(span, format!("cannot {ACT}", ACT = act));
294 if let Some(mir::Statement {
297 mir::StatementKind::Assign(box (
301 mir::BorrowKind::Mut { allow_two_phase_borrow: false },
306 }) = &self.body[location.block].statements.get(location.statement_index)
308 match decl.local_info {
309 Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
310 mir::VarBindingForm {
311 binding_mode: ty::BindingMode::BindByValue(Mutability::Not),
312 opt_ty_info: Some(sp),
318 err.span_note(sp, "the binding is already a mutable borrow");
323 decl.source_info.span,
324 "the binding is already a mutable borrow",
329 self.infcx.tcx.sess.source_map().span_to_snippet(source_info.span)
331 if snippet.starts_with("&mut ") {
332 // We don't have access to the HIR to get accurate spans, but we can
333 // give a best effort structured suggestion.
334 err.span_suggestion_verbose(
335 source_info.span.with_hi(source_info.span.lo() + BytePos(5)),
336 "try removing `&mut` here",
338 Applicability::MachineApplicable,
341 // This can occur with things like `(&mut self).foo()`.
342 err.span_help(source_info.span, "try removing `&mut` here");
345 err.span_help(source_info.span, "try removing `&mut` here");
347 } else if decl.mutability == Mutability::Not
350 Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(
351 hir::ImplicitSelfKind::MutRef
355 err.span_suggestion_verbose(
356 decl.source_info.span.shrink_to_lo(),
357 "consider making the binding mutable",
359 Applicability::MachineApplicable,
364 // We want to suggest users use `let mut` for local (user
365 // variable) mutations...
366 PlaceRef { local, projection: [] }
367 if self.body.local_decls[local].can_be_made_mutable() =>
369 // ... but it doesn't make sense to suggest it on
370 // variables that are `ref x`, `ref mut x`, `&self`,
371 // or `&mut self` (such variables are simply not
373 let local_decl = &self.body.local_decls[local];
374 assert_eq!(local_decl.mutability, Mutability::Not);
377 err.span_label(span, format!("cannot {act}"));
380 err.span_suggestion_verbose(
381 local_decl.source_info.span.shrink_to_lo(),
382 "consider changing this to be mutable",
384 Applicability::MachineApplicable,
386 let tcx = self.infcx.tcx;
387 if let ty::Closure(id, _) = *the_place_err.ty(self.body, tcx).ty.kind() {
388 self.show_mutating_upvar(tcx, id.expect_local(), the_place_err, &mut err);
393 // Also suggest adding mut for upvars
396 projection: [proj_base @ .., ProjectionElem::Field(upvar_index, _)],
398 debug_assert!(is_closure_or_generator(
399 Place::ty_from(local, proj_base, self.body, self.infcx.tcx).ty
402 let captured_place = &self.upvars[upvar_index.index()].place;
404 err.span_label(span, format!("cannot {act}"));
406 let upvar_hir_id = captured_place.get_root_variable();
408 if let Some(Node::Pat(pat)) = self.infcx.tcx.hir().find(upvar_hir_id)
409 && let hir::PatKind::Binding(
410 hir::BindingAnnotation::NONE,
418 "consider changing this to be mutable",
419 format!("mut {}", upvar_ident.name),
420 Applicability::MachineApplicable,
424 let tcx = self.infcx.tcx;
425 if let ty::Ref(_, ty, Mutability::Mut) = the_place_err.ty(self.body, tcx).ty.kind()
426 && let ty::Closure(id, _) = *ty.kind()
428 self.show_mutating_upvar(tcx, id.expect_local(), the_place_err, &mut err);
432 // complete hack to approximate old AST-borrowck
433 // diagnostic: if the span starts with a mutable borrow of
434 // a local variable, then just suggest the user remove it.
435 PlaceRef { local: _, projection: [] }
441 .span_to_snippet(span)
442 .map_or(false, |snippet| snippet.starts_with("&mut ")) =>
444 err.span_label(span, format!("cannot {act}"));
447 "try removing `&mut` here",
449 Applicability::MaybeIncorrect,
453 PlaceRef { local, projection: [ProjectionElem::Deref] }
454 if self.body.local_decls[local].is_ref_for_guard() =>
456 err.span_label(span, format!("cannot {act}"));
458 "variables bound in patterns are immutable until the end of the pattern guard",
462 // We want to point out when a `&` can be readily replaced
465 // FIXME: can this case be generalized to work for an
466 // arbitrary base for the projection?
467 PlaceRef { local, projection: [ProjectionElem::Deref] }
468 if self.body.local_decls[local].is_user_variable() =>
470 let local_decl = &self.body.local_decls[local];
472 let (pointer_sigil, pointer_desc) = if local_decl.ty.is_region_ptr() {
475 ("*const", "pointer")
478 match self.local_names[local] {
479 Some(name) if !local_decl.from_compiler_desugaring() => {
480 let label = match local_decl.local_info.as_deref().unwrap() {
481 LocalInfo::User(ClearCrossCrate::Set(
482 mir::BindingForm::ImplicitSelf(_),
484 let (span, suggestion) =
485 suggest_ampmut_self(self.infcx.tcx, local_decl);
486 Some((true, span, suggestion))
489 LocalInfo::User(ClearCrossCrate::Set(mir::BindingForm::Var(
490 mir::VarBindingForm {
491 binding_mode: ty::BindingMode::BindByValue(_),
496 // check if the RHS is from desugaring
497 let opt_assignment_rhs_span =
498 self.body.find_assignments(local).first().map(|&location| {
499 if let Some(mir::Statement {
502 mir::StatementKind::Assign(box (
504 mir::Rvalue::Use(mir::Operand::Copy(place)),
506 }) = self.body[location.block]
508 .get(location.statement_index)
510 self.body.local_decls[place.local].source_info.span
512 self.body.source_info(location).span
515 match opt_assignment_rhs_span.and_then(|s| s.desugaring_kind()) {
516 // on for loops, RHS points to the iterator part
517 Some(DesugaringKind::ForLoop) => {
518 self.suggest_similar_mut_method_for_for_loop(&mut err);
519 err.span_label(opt_assignment_rhs_span.unwrap(), format!(
520 "this iterator yields `{pointer_sigil}` {pointer_desc}s",
524 // don't create labels for compiler-generated spans
527 let label = if name != kw::SelfLower {
531 opt_assignment_rhs_span,
535 match local_decl.local_info.as_deref() {
536 Some(LocalInfo::User(ClearCrossCrate::Set(
537 mir::BindingForm::Var(mir::VarBindingForm {
542 let (span, sugg) = suggest_ampmut_self(
548 // explicit self (eg `self: &'a Self`)
552 opt_assignment_rhs_span,
562 LocalInfo::User(ClearCrossCrate::Set(mir::BindingForm::Var(
563 mir::VarBindingForm {
564 binding_mode: ty::BindingMode::BindByReference(_),
568 let pattern_span = local_decl.source_info.span;
569 suggest_ref_mut(self.infcx.tcx, pattern_span)
570 .map(|replacement| (true, pattern_span, replacement))
573 LocalInfo::User(ClearCrossCrate::Clear) => {
574 bug!("saw cleared local state")
581 Some((true, err_help_span, suggested_code)) => {
582 let (is_trait_sig, local_trait) = self.is_error_in_trait(local);
584 err.span_suggestion_verbose(
587 "consider changing this to be a mutable {pointer_desc}"
590 Applicability::MachineApplicable,
592 } else if let Some(x) = local_trait {
593 err.span_suggestion_verbose(
596 "consider changing that to be a mutable {pointer_desc}"
599 Applicability::MachineApplicable,
603 Some((false, err_label_span, message)) => {
607 "consider changing this binding's type to be: `{message}`"
616 "`{name}` is a `{pointer_sigil}` {pointer_desc}, \
617 so the data it refers to cannot be {acted_on}",
624 format!("cannot {act} through `{pointer_sigil}` {pointer_desc}"),
630 PlaceRef { local, projection: [ProjectionElem::Deref] }
631 if local == ty::CAPTURE_STRUCT_LOCAL && !self.upvars.is_empty() =>
633 self.expected_fn_found_fn_mut_call(&mut err, span, act);
636 PlaceRef { local: _, projection: [.., ProjectionElem::Deref] } => {
637 err.span_label(span, format!("cannot {ACT}", ACT = act));
640 Some(BorrowedContentSource::OverloadedDeref(ty)) => {
642 "trait `DerefMut` is required to modify through a dereference, \
643 but it is not implemented for `{ty}`",
646 Some(BorrowedContentSource::OverloadedIndex(ty)) => {
648 "trait `IndexMut` is required to modify indexed content, \
649 but it is not implemented for `{ty}`",
651 self.suggest_map_index_mut_alternatives(ty, &mut err, span);
658 err.span_label(span, format!("cannot {act}"));
662 if let Some(span) = mut_error {
663 self.buffer_mut_error(span, err, count);
665 self.buffer_error(err);
669 fn suggest_map_index_mut_alternatives(&self, ty: Ty<'tcx>, err: &mut Diagnostic, span: Span) {
670 let Some(adt) = ty.ty_adt_def() else { return };
672 if self.infcx.tcx.is_diagnostic_item(sym::HashMap, did)
673 || self.infcx.tcx.is_diagnostic_item(sym::BTreeMap, did)
677 err: &'a mut Diagnostic,
681 impl<'a, 'tcx> Visitor<'tcx> for V<'a, 'tcx> {
682 fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt<'tcx>) {
683 hir::intravisit::walk_stmt(self, stmt);
684 let expr = match stmt.kind {
685 hir::StmtKind::Semi(expr) | hir::StmtKind::Expr(expr) => expr,
686 hir::StmtKind::Local(hir::Local { init: Some(expr), .. }) => expr,
691 if let hir::ExprKind::Assign(place, rv, _sp) = expr.kind
692 && let hir::ExprKind::Index(val, index) = place.kind
693 && (expr.span == self.assign_span || place.span == self.assign_span)
697 self.err.multipart_suggestions(
699 "to modify a `{}`, use `.get_mut()`, `.insert()` or the entry API",
703 vec![ // val.insert(index, rv);
705 val.span.shrink_to_hi().with_hi(index.span.lo()),
706 ".insert(".to_string(),
709 index.span.shrink_to_hi().with_hi(rv.span.lo()),
712 (rv.span.shrink_to_hi(), ")".to_string()),
714 vec![ // val.get_mut(index).map(|v| { *v = rv; });
716 val.span.shrink_to_hi().with_hi(index.span.lo()),
717 ".get_mut(".to_string(),
720 index.span.shrink_to_hi().with_hi(place.span.hi()),
721 ").map(|val| { *val".to_string(),
724 rv.span.shrink_to_hi(),
728 vec![ // let x = val.entry(index).or_insert(rv);
729 (val.span.shrink_to_lo(), "let val = ".to_string()),
731 val.span.shrink_to_hi().with_hi(index.span.lo()),
732 ".entry(".to_string(),
735 index.span.shrink_to_hi().with_hi(rv.span.lo()),
736 ").or_insert(".to_string(),
738 (rv.span.shrink_to_hi(), ")".to_string()),
741 Applicability::MachineApplicable,
743 self.suggested = true;
744 } else if let hir::ExprKind::MethodCall(_path, receiver, _, sp) = expr.kind
745 && let hir::ExprKind::Index(val, index) = receiver.kind
746 && expr.span == self.assign_span
748 // val[index].path(args..);
749 self.err.multipart_suggestion(
750 &format!("to modify a `{}` use `.get_mut()`", self.ty),
753 val.span.shrink_to_hi().with_hi(index.span.lo()),
754 ".get_mut(".to_string(),
757 index.span.shrink_to_hi().with_hi(receiver.span.hi()),
758 ").map(|val| val".to_string(),
760 (sp.shrink_to_hi(), ")".to_string()),
762 Applicability::MachineApplicable,
764 self.suggested = true;
768 let hir_map = self.infcx.tcx.hir();
769 let def_id = self.body.source.def_id();
770 let hir_id = hir_map.local_def_id_to_hir_id(def_id.as_local().unwrap());
771 let node = hir_map.find(hir_id);
772 let Some(hir::Node::Item(item)) = node else { return; };
773 let hir::ItemKind::Fn(.., body_id) = item.kind else { return; };
774 let body = self.infcx.tcx.hir().body(body_id);
775 let mut v = V { assign_span: span, err, ty, suggested: false };
779 "to modify a `{ty}`, use `.get_mut()`, `.insert()` or the entry API",
785 /// User cannot make signature of a trait mutable without changing the
786 /// trait. So we find if this error belongs to a trait and if so we move
787 /// suggestion to the trait or disable it if it is out of scope of this crate
788 fn is_error_in_trait(&self, local: Local) -> (bool, Option<Span>) {
789 if self.body.local_kind(local) != LocalKind::Arg {
790 return (false, None);
792 let hir_map = self.infcx.tcx.hir();
793 let my_def = self.body.source.def_id();
794 let my_hir = hir_map.local_def_id_to_hir_id(my_def.as_local().unwrap());
796 self.infcx.tcx.impl_of_method(my_def).and_then(|x| self.infcx.tcx.trait_id_of_impl(x))
798 return (false, None);
802 td.as_local().and_then(|tld| match hir_map.find_by_def_id(tld) {
803 Some(Node::Item(hir::Item {
804 kind: hir::ItemKind::Trait(_, _, _, _, items),
807 let mut f_in_trait_opt = None;
808 for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
809 let hi = fi.hir_id();
810 if !matches!(k, hir::AssocItemKind::Fn { .. }) {
813 if hir_map.name(hi) != hir_map.name(my_hir) {
816 f_in_trait_opt = Some(hi);
819 f_in_trait_opt.and_then(|f_in_trait| match hir_map.find(f_in_trait) {
820 Some(Node::TraitItem(hir::TraitItem {
822 hir::TraitItemKind::Fn(
823 hir::FnSig { decl: hir::FnDecl { inputs, .. }, .. },
828 let hir::Ty { span, .. } = inputs[local.index() - 1];
839 // point to span of upvar making closure call require mutable borrow
840 fn show_mutating_upvar(
843 closure_local_def_id: hir::def_id::LocalDefId,
844 the_place_err: PlaceRef<'tcx>,
845 err: &mut Diagnostic,
847 let tables = tcx.typeck(closure_local_def_id);
848 let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_local_def_id);
849 if let Some((span, closure_kind_origin)) =
850 &tables.closure_kind_origins().get(closure_hir_id)
852 let reason = if let PlaceBase::Upvar(upvar_id) = closure_kind_origin.base {
853 let upvar = ty::place_to_string_for_capture(tcx, closure_kind_origin);
854 let root_hir_id = upvar_id.var_path.hir_id;
855 // we have an origin for this closure kind starting at this root variable so it's safe to unwrap here
856 let captured_places =
857 tables.closure_min_captures[&closure_local_def_id].get(&root_hir_id).unwrap();
859 let origin_projection = closure_kind_origin
862 .map(|proj| proj.kind)
863 .collect::<Vec<_>>();
864 let mut capture_reason = String::new();
865 for captured_place in captured_places {
866 let captured_place_kinds = captured_place
870 .map(|proj| proj.kind)
871 .collect::<Vec<_>>();
872 if rustc_middle::ty::is_ancestor_or_same_capture(
873 &captured_place_kinds,
876 match captured_place.info.capture_kind {
877 ty::UpvarCapture::ByRef(
878 ty::BorrowKind::MutBorrow | ty::BorrowKind::UniqueImmBorrow,
880 capture_reason = format!("mutable borrow of `{upvar}`");
882 ty::UpvarCapture::ByValue => {
883 capture_reason = format!("possible mutation of `{upvar}`");
885 _ => bug!("upvar `{upvar}` borrowed, but not mutably"),
890 if capture_reason.is_empty() {
891 bug!("upvar `{upvar}` borrowed, but cannot find reason");
900 "calling `{}` requires mutable binding due to {}",
901 self.describe_place(the_place_err).unwrap(),
908 // Attempt to search similar mutable associated items for suggestion.
909 // In the future, attempt in all path but initially for RHS of for_loop
910 fn suggest_similar_mut_method_for_for_loop(&self, err: &mut Diagnostic) {
913 ExprKind::{Block, Call, DropTemps, Match, MethodCall},
914 HirId, ImplItem, ImplItemKind, Item, ItemKind,
917 fn maybe_body_id_of_fn(hir_map: Map<'_>, id: HirId) -> Option<BodyId> {
918 match hir_map.find(id) {
919 Some(Node::Item(Item { kind: ItemKind::Fn(_, _, body_id), .. }))
920 | Some(Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(_, body_id), .. })) => {
926 let hir_map = self.infcx.tcx.hir();
927 let mir_body_hir_id = self.mir_hir_id();
928 if let Some(fn_body_id) = maybe_body_id_of_fn(hir_map, mir_body_hir_id) {
944 MethodCall(path_segment, _, _, span),
962 ) = hir_map.body(fn_body_id).value.kind
964 let opt_suggestions = self
967 .typeck(path_segment.hir_id.owner.def_id)
968 .type_dependent_def_id(*hir_id)
969 .and_then(|def_id| self.infcx.tcx.impl_of_method(def_id))
970 .map(|def_id| self.infcx.tcx.associated_items(def_id))
973 .in_definition_order()
974 .map(|assoc_item_def| assoc_item_def.ident(self.infcx.tcx))
976 let original_method_ident = path_segment.ident;
977 original_method_ident != ident
980 .starts_with(&original_method_ident.name.to_string())
982 .map(|ident| format!("{ident}()"))
986 if let Some(mut suggestions) = opt_suggestions
987 && suggestions.peek().is_some()
989 err.span_suggestions(
991 "use mutable method",
993 Applicability::MaybeIncorrect,
1000 /// Targeted error when encountering an `FnMut` closure where an `Fn` closure was expected.
1001 fn expected_fn_found_fn_mut_call(&self, err: &mut Diagnostic, sp: Span, act: &str) {
1002 err.span_label(sp, format!("cannot {act}"));
1004 let hir = self.infcx.tcx.hir();
1005 let closure_id = self.mir_hir_id();
1006 let closure_span = self.infcx.tcx.def_span(self.mir_def_id());
1007 let fn_call_id = hir.get_parent_node(closure_id);
1008 let node = hir.get(fn_call_id);
1009 let def_id = hir.enclosing_body_owner(fn_call_id);
1010 let mut look_at_return = true;
1011 // If we can detect the expression to be an `fn` call where the closure was an argument,
1012 // we point at the `fn` definition argument...
1013 if let hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Call(func, args), .. }) = node {
1017 .filter(|(_, arg)| arg.hir_id == closure_id)
1018 .map(|(pos, _)| pos)
1020 let tables = self.infcx.tcx.typeck(def_id);
1021 if let Some(ty::FnDef(def_id, _)) =
1022 tables.node_type_opt(func.hir_id).as_ref().map(|ty| ty.kind())
1024 let arg = match hir.get_if_local(*def_id) {
1026 hir::Node::Item(hir::Item {
1027 ident, kind: hir::ItemKind::Fn(sig, ..), ..
1029 | hir::Node::TraitItem(hir::TraitItem {
1031 kind: hir::TraitItemKind::Fn(sig, _),
1034 | hir::Node::ImplItem(hir::ImplItem {
1036 kind: hir::ImplItemKind::Fn(sig, _),
1042 sig.decl.inputs.get(
1043 pos + if sig.decl.implicit_self.has_implicit_self() {
1050 .map(|arg| arg.span)
1051 .unwrap_or(ident.span),
1055 if let Some(span) = arg {
1056 err.span_label(span, "change this to accept `FnMut` instead of `Fn`");
1057 err.span_label(func.span, "expects `Fn` instead of `FnMut`");
1058 err.span_label(closure_span, "in this closure");
1059 look_at_return = false;
1064 if look_at_return && hir.get_return_block(closure_id).is_some() {
1065 // ...otherwise we are probably in the tail expression of the function, point at the
1067 match hir.get_by_def_id(hir.get_parent_item(fn_call_id).def_id) {
1068 hir::Node::Item(hir::Item { ident, kind: hir::ItemKind::Fn(sig, ..), .. })
1069 | hir::Node::TraitItem(hir::TraitItem {
1071 kind: hir::TraitItemKind::Fn(sig, _),
1074 | hir::Node::ImplItem(hir::ImplItem {
1076 kind: hir::ImplItemKind::Fn(sig, _),
1079 err.span_label(ident.span, "");
1081 sig.decl.output.span(),
1082 "change this to return `FnMut` instead of `Fn`",
1084 err.span_label(closure_span, "in this closure");
1092 fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symbol>) -> bool {
1093 debug!("local_info: {:?}, ty.kind(): {:?}", local_decl.local_info, local_decl.ty.kind());
1095 match local_decl.local_info.as_deref() {
1096 // Check if mutably borrowing a mutable reference.
1097 Some(LocalInfo::User(ClearCrossCrate::Set(mir::BindingForm::Var(
1098 mir::VarBindingForm {
1099 binding_mode: ty::BindingMode::BindByValue(Mutability::Not), ..
1101 )))) => matches!(local_decl.ty.kind(), ty::Ref(_, _, hir::Mutability::Mut)),
1102 Some(LocalInfo::User(ClearCrossCrate::Set(mir::BindingForm::ImplicitSelf(kind)))) => {
1103 // Check if the user variable is a `&mut self` and we can therefore
1104 // suggest removing the `&mut`.
1106 // Deliberately fall into this case for all implicit self types,
1107 // so that we don't fall in to the next case with them.
1108 *kind == hir::ImplicitSelfKind::MutRef
1110 _ if Some(kw::SelfLower) == local_name => {
1111 // Otherwise, check if the name is the `self` keyword - in which case
1112 // we have an explicit self. Do the same thing in this case and check
1113 // for a `self: &mut Self` to suggest removing the `&mut`.
1114 matches!(local_decl.ty.kind(), ty::Ref(_, _, hir::Mutability::Mut))
1120 fn suggest_ampmut_self<'tcx>(
1122 local_decl: &mir::LocalDecl<'tcx>,
1123 ) -> (Span, String) {
1124 let sp = local_decl.source_info.span;
1127 match tcx.sess.source_map().span_to_snippet(sp) {
1129 let lt_pos = snippet.find('\'');
1130 if let Some(lt_pos) = lt_pos {
1131 format!("&{}mut self", &snippet[lt_pos..snippet.len() - 4])
1133 "&mut self".to_string()
1136 _ => "&mut self".to_string(),
1141 // When we want to suggest a user change a local variable to be a `&mut`, there
1142 // are three potential "obvious" things to highlight:
1144 // let ident [: Type] [= RightHandSideExpression];
1145 // ^^^^^ ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^
1148 // We can always fallback on highlighting the first. But chances are good that
1149 // the user experience will be better if we highlight one of the others if possible;
1150 // for example, if the RHS is present and the Type is not, then the type is going to
1151 // be inferred *from* the RHS, which means we should highlight that (and suggest
1152 // that they borrow the RHS mutably).
1154 // This implementation attempts to emulate AST-borrowck prioritization
1155 // by trying (3.), then (2.) and finally falling back on (1.).
1156 fn suggest_ampmut<'tcx>(
1158 local_decl: &mir::LocalDecl<'tcx>,
1159 opt_assignment_rhs_span: Option<Span>,
1160 opt_ty_info: Option<Span>,
1161 ) -> (bool, Span, String) {
1162 if let Some(assignment_rhs_span) = opt_assignment_rhs_span
1163 && let Ok(src) = tcx.sess.source_map().span_to_snippet(assignment_rhs_span)
1165 let is_mutbl = |ty: &str| -> bool {
1166 if let Some(rest) = ty.strip_prefix("mut") {
1167 match rest.chars().next() {
1169 Some(c) if c.is_whitespace() => true,
1174 // e.g. `&mutablevar`
1181 if let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace)) {
1182 let lt_name = &src[1..ws_pos];
1183 let ty = src[ws_pos..].trim_start();
1185 return (true, assignment_rhs_span, format!("&{lt_name} mut {ty}"));
1187 } else if let Some(stripped) = src.strip_prefix('&') {
1188 let stripped = stripped.trim_start();
1189 if !is_mutbl(stripped) {
1190 return (true, assignment_rhs_span, format!("&mut {stripped}"));
1195 let (suggestability, highlight_span) = match opt_ty_info {
1196 // if this is a variable binding with an explicit type,
1197 // try to highlight that for the suggestion.
1198 Some(ty_span) => (true, ty_span),
1200 // otherwise, just highlight the span associated with
1201 // the (MIR) LocalDecl.
1202 None => (false, local_decl.source_info.span),
1205 if let Ok(src) = tcx.sess.source_map().span_to_snippet(highlight_span)
1206 && let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace))
1208 let lt_name = &src[1..ws_pos];
1209 let ty = &src[ws_pos..];
1210 return (true, highlight_span, format!("&{} mut{}", lt_name, ty));
1213 let ty_mut = local_decl.ty.builtin_deref(true).unwrap();
1214 assert_eq!(ty_mut.mutbl, hir::Mutability::Not);
1218 if local_decl.ty.is_region_ptr() {
1219 format!("&mut {}", ty_mut.ty)
1221 format!("*mut {}", ty_mut.ty)
1226 fn is_closure_or_generator(ty: Ty<'_>) -> bool {
1227 ty.is_closure() || ty.is_generator()
1230 /// Given a field that needs to be mutable, returns a span where the " mut " could go.
1231 /// This function expects the local to be a reference to a struct in order to produce a span.
1234 /// LL | s: &'a String
1235 /// | ^^^ returns a span taking up the space here
1237 fn get_mut_span_in_struct_field<'tcx>(
1242 // Expect our local to be a reference to a struct of some kind.
1243 if let ty::Ref(_, ty, _) = ty.kind()
1244 && let ty::Adt(def, _) = ty.kind()
1245 && let field = def.all_fields().nth(field.index())?
1246 // Use the HIR types to construct the diagnostic message.
1247 && let node = tcx.hir().find_by_def_id(field.did.as_local()?)?
1248 // Now we're dealing with the actual struct that we're going to suggest a change to,
1249 // we can expect a field that is an immutable reference to a type.
1250 && let hir::Node::Field(field) = node
1251 && let hir::TyKind::Ref(lt, hir::MutTy { mutbl: hir::Mutability::Not, ty }) = field.ty.kind
1253 return Some(lt.ident.span.between(ty.span));
1259 /// If possible, suggest replacing `ref` with `ref mut`.
1260 fn suggest_ref_mut(tcx: TyCtxt<'_>, binding_span: Span) -> Option<String> {
1261 let hi_src = tcx.sess.source_map().span_to_snippet(binding_span).ok()?;
1262 if hi_src.starts_with("ref") && hi_src["ref".len()..].starts_with(rustc_lexer::is_whitespace) {
1263 let replacement = format!("ref mut{}", &hi_src["ref".len()..]);