rustc_borrowck/diagnostics/
mutability_errors.rs

1#![allow(rustc::diagnostic_outside_of_impl)]
2#![allow(rustc::untranslatable_diagnostic)]
3
4use core::ops::ControlFlow;
5
6use either::Either;
7use hir::{ExprKind, Param};
8use rustc_abi::FieldIdx;
9use rustc_errors::{Applicability, Diag};
10use rustc_hir::intravisit::Visitor;
11use rustc_hir::{self as hir, BindingMode, ByRef, Node};
12use rustc_middle::bug;
13use rustc_middle::hir::place::PlaceBase;
14use rustc_middle::mir::visit::PlaceContext;
15use rustc_middle::mir::{
16    self, BindingForm, Body, BorrowKind, Local, LocalDecl, LocalInfo, LocalKind, Location,
17    Mutability, Operand, Place, PlaceRef, ProjectionElem, RawPtrKind, Rvalue, Statement,
18    StatementKind, TerminatorKind,
19};
20use rustc_middle::ty::{self, InstanceKind, Ty, TyCtxt, Upcast};
21use rustc_span::{BytePos, DesugaringKind, Span, Symbol, kw, sym};
22use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
23use rustc_trait_selection::infer::InferCtxtExt;
24use rustc_trait_selection::traits;
25use tracing::{debug, trace};
26
27use crate::diagnostics::BorrowedContentSource;
28use crate::{MirBorrowckCtxt, session_diagnostics};
29
30#[derive(Copy, Clone, Debug, Eq, PartialEq)]
31pub(crate) enum AccessKind {
32    MutableBorrow,
33    Mutate,
34}
35
36/// Finds all statements that assign directly to local (i.e., X = ...) and returns their
37/// locations.
38fn find_assignments(body: &Body<'_>, local: Local) -> Vec<Location> {
39    use rustc_middle::mir::visit::Visitor;
40
41    struct FindLocalAssignmentVisitor {
42        needle: Local,
43        locations: Vec<Location>,
44    }
45
46    impl<'tcx> Visitor<'tcx> for FindLocalAssignmentVisitor {
47        fn visit_local(&mut self, local: Local, place_context: PlaceContext, location: Location) {
48            if self.needle != local {
49                return;
50            }
51
52            if place_context.is_place_assignment() {
53                self.locations.push(location);
54            }
55        }
56    }
57
58    let mut visitor = FindLocalAssignmentVisitor { needle: local, locations: vec![] };
59    visitor.visit_body(body);
60    visitor.locations
61}
62
63impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
64    pub(crate) fn report_mutability_error(
65        &mut self,
66        access_place: Place<'tcx>,
67        span: Span,
68        the_place_err: PlaceRef<'tcx>,
69        error_access: AccessKind,
70        location: Location,
71    ) {
72        debug!(
73            "report_mutability_error(\
74                access_place={:?}, span={:?}, the_place_err={:?}, error_access={:?}, location={:?},\
75            )",
76            access_place, span, the_place_err, error_access, location,
77        );
78
79        let mut err;
80        let item_msg;
81        let reason;
82        let mut opt_source = None;
83        let access_place_desc = self.describe_any_place(access_place.as_ref());
84        debug!("report_mutability_error: access_place_desc={:?}", access_place_desc);
85
86        match the_place_err {
87            PlaceRef { local, projection: [] } => {
88                item_msg = access_place_desc;
89                if access_place.as_local().is_some() {
90                    reason = ", as it is not declared as mutable".to_string();
91                } else {
92                    let name = self.local_name(local).expect("immutable unnamed local");
93                    reason = format!(", as `{name}` is not declared as mutable");
94                }
95            }
96
97            PlaceRef {
98                local,
99                projection: [proj_base @ .., ProjectionElem::Field(upvar_index, _)],
100            } => {
101                debug_assert!(is_closure_like(
102                    Place::ty_from(local, proj_base, self.body, self.infcx.tcx).ty
103                ));
104
105                let imm_borrow_derefed = self.upvars[upvar_index.index()]
106                    .place
107                    .deref_tys()
108                    .any(|ty| matches!(ty.kind(), ty::Ref(.., hir::Mutability::Not)));
109
110                // If the place is immutable then:
111                //
112                // - Either we deref an immutable ref to get to our final place.
113                //    - We don't capture derefs of raw ptrs
114                // - Or the final place is immut because the root variable of the capture
115                //   isn't marked mut and we should suggest that to the user.
116                if imm_borrow_derefed {
117                    // If we deref an immutable ref then the suggestion here doesn't help.
118                    return;
119                } else {
120                    item_msg = access_place_desc;
121                    if self.is_upvar_field_projection(access_place.as_ref()).is_some() {
122                        reason = ", as it is not declared as mutable".to_string();
123                    } else {
124                        let name = self.upvars[upvar_index.index()].to_string(self.infcx.tcx);
125                        reason = format!(", as `{name}` is not declared as mutable");
126                    }
127                }
128            }
129
130            PlaceRef { local, projection: [ProjectionElem::Deref] }
131                if self.body.local_decls[local].is_ref_for_guard() =>
132            {
133                item_msg = access_place_desc;
134                reason = ", as it is immutable for the pattern guard".to_string();
135            }
136            PlaceRef { local, projection: [ProjectionElem::Deref] }
137                if self.body.local_decls[local].is_ref_to_static() =>
138            {
139                if access_place.projection.len() == 1 {
140                    item_msg = format!("immutable static item {access_place_desc}");
141                    reason = String::new();
142                } else {
143                    item_msg = access_place_desc;
144                    let local_info = self.body.local_decls[local].local_info();
145                    if let LocalInfo::StaticRef { def_id, .. } = *local_info {
146                        let static_name = &self.infcx.tcx.item_name(def_id);
147                        reason = format!(", as `{static_name}` is an immutable static item");
148                    } else {
149                        bug!("is_ref_to_static return true, but not ref to static?");
150                    }
151                }
152            }
153            PlaceRef { local: _, projection: [proj_base @ .., ProjectionElem::Deref] } => {
154                if the_place_err.local == ty::CAPTURE_STRUCT_LOCAL
155                    && proj_base.is_empty()
156                    && !self.upvars.is_empty()
157                {
158                    item_msg = access_place_desc;
159                    debug_assert!(self.body.local_decls[ty::CAPTURE_STRUCT_LOCAL].ty.is_ref());
160                    debug_assert!(is_closure_like(the_place_err.ty(self.body, self.infcx.tcx).ty));
161
162                    reason = if self.is_upvar_field_projection(access_place.as_ref()).is_some() {
163                        ", as it is a captured variable in a `Fn` closure".to_string()
164                    } else {
165                        ", as `Fn` closures cannot mutate their captured variables".to_string()
166                    }
167                } else {
168                    let source = self.borrowed_content_source(PlaceRef {
169                        local: the_place_err.local,
170                        projection: proj_base,
171                    });
172                    let pointer_type = source.describe_for_immutable_place(self.infcx.tcx);
173                    opt_source = Some(source);
174                    if let Some(desc) = self.describe_place(access_place.as_ref()) {
175                        item_msg = format!("`{desc}`");
176                        reason = match error_access {
177                            AccessKind::Mutate => format!(", which is behind {pointer_type}"),
178                            AccessKind::MutableBorrow => {
179                                format!(", as it is behind {pointer_type}")
180                            }
181                        }
182                    } else {
183                        item_msg = format!("data in {pointer_type}");
184                        reason = String::new();
185                    }
186                }
187            }
188
189            PlaceRef {
190                local: _,
191                projection:
192                    [
193                        ..,
194                        ProjectionElem::Index(_)
195                        | ProjectionElem::ConstantIndex { .. }
196                        | ProjectionElem::OpaqueCast { .. }
197                        | ProjectionElem::Subslice { .. }
198                        | ProjectionElem::Downcast(..)
199                        | ProjectionElem::UnwrapUnsafeBinder(_),
200                    ],
201            } => bug!("Unexpected immutable place."),
202        }
203
204        debug!("report_mutability_error: item_msg={:?}, reason={:?}", item_msg, reason);
205
206        // `act` and `acted_on` are strings that let us abstract over
207        // the verbs used in some diagnostic messages.
208        let act;
209        let acted_on;
210        let mut suggest = true;
211        let mut mut_error = None;
212        let mut count = 1;
213
214        let span = match error_access {
215            AccessKind::Mutate => {
216                err = self.cannot_assign(span, &(item_msg + &reason));
217                act = "assign";
218                acted_on = "written";
219                span
220            }
221            AccessKind::MutableBorrow => {
222                act = "borrow as mutable";
223                acted_on = "borrowed as mutable";
224
225                let borrow_spans = self.borrow_spans(span, location);
226                let borrow_span = borrow_spans.args_or_use();
227                match the_place_err {
228                    PlaceRef { local, projection: [] }
229                        if self.body.local_decls[local].can_be_made_mutable() =>
230                    {
231                        let span = self.body.local_decls[local].source_info.span;
232                        mut_error = Some(span);
233                        if let Some((buffered_err, c)) = self.get_buffered_mut_error(span) {
234                            // We've encountered a second (or more) attempt to mutably borrow an
235                            // immutable binding, so the likely problem is with the binding
236                            // declaration, not the use. We collect these in a single diagnostic
237                            // and make the binding the primary span of the error.
238                            err = buffered_err;
239                            count = c + 1;
240                            if count == 2 {
241                                err.replace_span_with(span, false);
242                                err.span_label(span, "not mutable");
243                            }
244                            suggest = false;
245                        } else {
246                            err = self.cannot_borrow_path_as_mutable_because(
247                                borrow_span,
248                                &item_msg,
249                                &reason,
250                            );
251                        }
252                    }
253                    _ => {
254                        err = self.cannot_borrow_path_as_mutable_because(
255                            borrow_span,
256                            &item_msg,
257                            &reason,
258                        );
259                    }
260                }
261                if suggest {
262                    borrow_spans.var_subdiag(
263                        &mut err,
264                        Some(mir::BorrowKind::Mut { kind: mir::MutBorrowKind::Default }),
265                        |_kind, var_span| {
266                            let place = self.describe_any_place(access_place.as_ref());
267                            session_diagnostics::CaptureVarCause::MutableBorrowUsePlaceClosure {
268                                place,
269                                var_span,
270                            }
271                        },
272                    );
273                }
274                borrow_span
275            }
276        };
277
278        debug!("report_mutability_error: act={:?}, acted_on={:?}", act, acted_on);
279
280        match the_place_err {
281            // Suggest making an existing shared borrow in a struct definition a mutable borrow.
282            //
283            // This is applicable when we have a deref of a field access to a deref of a local -
284            // something like `*((*_1).0`. The local that we get will be a reference to the
285            // struct we've got a field access of (it must be a reference since there's a deref
286            // after the field access).
287            PlaceRef {
288                local,
289                projection:
290                    [
291                        proj_base @ ..,
292                        ProjectionElem::Deref,
293                        ProjectionElem::Field(field, _),
294                        ProjectionElem::Deref,
295                    ],
296            } => {
297                err.span_label(span, format!("cannot {act}"));
298
299                let place = Place::ty_from(local, proj_base, self.body, self.infcx.tcx);
300                if let Some(span) = get_mut_span_in_struct_field(self.infcx.tcx, place.ty, *field) {
301                    err.span_suggestion_verbose(
302                        span,
303                        "consider changing this to be mutable",
304                        " mut ",
305                        Applicability::MaybeIncorrect,
306                    );
307                }
308            }
309
310            // Suggest removing a `&mut` from the use of a mutable reference.
311            PlaceRef { local, projection: [] }
312                if self
313                    .body
314                    .local_decls
315                    .get(local)
316                    .is_some_and(|l| mut_borrow_of_mutable_ref(l, self.local_name(local))) =>
317            {
318                let decl = &self.body.local_decls[local];
319                err.span_label(span, format!("cannot {act}"));
320                if let Some(mir::Statement {
321                    source_info,
322                    kind:
323                        mir::StatementKind::Assign(box (
324                            _,
325                            mir::Rvalue::Ref(
326                                _,
327                                mir::BorrowKind::Mut { kind: mir::MutBorrowKind::Default },
328                                _,
329                            ),
330                        )),
331                    ..
332                }) = &self.body[location.block].statements.get(location.statement_index)
333                {
334                    match *decl.local_info() {
335                        LocalInfo::User(BindingForm::Var(mir::VarBindingForm {
336                            binding_mode: BindingMode(ByRef::No, Mutability::Not),
337                            opt_ty_info: Some(sp),
338                            ..
339                        })) => {
340                            if suggest {
341                                err.span_note(sp, "the binding is already a mutable borrow");
342                            }
343                        }
344                        _ => {
345                            err.span_note(
346                                decl.source_info.span,
347                                "the binding is already a mutable borrow",
348                            );
349                        }
350                    }
351                    if let Ok(snippet) =
352                        self.infcx.tcx.sess.source_map().span_to_snippet(source_info.span)
353                    {
354                        if snippet.starts_with("&mut ") {
355                            // We don't have access to the HIR to get accurate spans, but we can
356                            // give a best effort structured suggestion.
357                            err.span_suggestion_verbose(
358                                source_info.span.with_hi(source_info.span.lo() + BytePos(5)),
359                                "try removing `&mut` here",
360                                "",
361                                Applicability::MachineApplicable,
362                            );
363                        } else {
364                            // This can occur with things like `(&mut self).foo()`.
365                            err.span_help(source_info.span, "try removing `&mut` here");
366                        }
367                    } else {
368                        err.span_help(source_info.span, "try removing `&mut` here");
369                    }
370                } else if decl.mutability.is_not() {
371                    if matches!(
372                        decl.local_info(),
373                        LocalInfo::User(BindingForm::ImplicitSelf(hir::ImplicitSelfKind::RefMut))
374                    ) {
375                        err.note(
376                            "as `Self` may be unsized, this call attempts to take `&mut &mut self`",
377                        );
378                        err.note("however, `&mut self` expands to `self: &mut Self`, therefore `self` cannot be borrowed mutably");
379                    } else {
380                        err.span_suggestion_verbose(
381                            decl.source_info.span.shrink_to_lo(),
382                            "consider making the binding mutable",
383                            "mut ",
384                            Applicability::MachineApplicable,
385                        );
386                    };
387                }
388            }
389
390            // We want to suggest users use `let mut` for local (user
391            // variable) mutations...
392            PlaceRef { local, projection: [] }
393                if self.body.local_decls[local].can_be_made_mutable() =>
394            {
395                // ... but it doesn't make sense to suggest it on
396                // variables that are `ref x`, `ref mut x`, `&self`,
397                // or `&mut self` (such variables are simply not
398                // mutable).
399                let local_decl = &self.body.local_decls[local];
400                assert_eq!(local_decl.mutability, Mutability::Not);
401
402                if count < 10 {
403                    err.span_label(span, format!("cannot {act}"));
404                }
405                if suggest {
406                    self.construct_mut_suggestion_for_local_binding_patterns(&mut err, local);
407                    let tcx = self.infcx.tcx;
408                    if let ty::Closure(id, _) = *the_place_err.ty(self.body, tcx).ty.kind() {
409                        self.show_mutating_upvar(tcx, id.expect_local(), the_place_err, &mut err);
410                    }
411                }
412            }
413
414            // Also suggest adding mut for upvars.
415            PlaceRef {
416                local,
417                projection: [proj_base @ .., ProjectionElem::Field(upvar_index, _)],
418            } => {
419                debug_assert!(is_closure_like(
420                    Place::ty_from(local, proj_base, self.body, self.infcx.tcx).ty
421                ));
422
423                let captured_place = self.upvars[upvar_index.index()];
424
425                err.span_label(span, format!("cannot {act}"));
426
427                let upvar_hir_id = captured_place.get_root_variable();
428
429                if let Node::Pat(pat) = self.infcx.tcx.hir_node(upvar_hir_id)
430                    && let hir::PatKind::Binding(hir::BindingMode::NONE, _, upvar_ident, _) =
431                        pat.kind
432                {
433                    if upvar_ident.name == kw::SelfLower {
434                        for (_, node) in self.infcx.tcx.hir_parent_iter(upvar_hir_id) {
435                            if let Some(fn_decl) = node.fn_decl() {
436                                if !matches!(
437                                    fn_decl.implicit_self,
438                                    hir::ImplicitSelfKind::RefImm | hir::ImplicitSelfKind::RefMut
439                                ) {
440                                    err.span_suggestion_verbose(
441                                        upvar_ident.span.shrink_to_lo(),
442                                        "consider changing this to be mutable",
443                                        "mut ",
444                                        Applicability::MachineApplicable,
445                                    );
446                                    break;
447                                }
448                            }
449                        }
450                    } else {
451                        err.span_suggestion_verbose(
452                            upvar_ident.span.shrink_to_lo(),
453                            "consider changing this to be mutable",
454                            "mut ",
455                            Applicability::MachineApplicable,
456                        );
457                    }
458                }
459
460                let tcx = self.infcx.tcx;
461                if let ty::Ref(_, ty, Mutability::Mut) = the_place_err.ty(self.body, tcx).ty.kind()
462                    && let ty::Closure(id, _) = *ty.kind()
463                {
464                    self.show_mutating_upvar(tcx, id.expect_local(), the_place_err, &mut err);
465                }
466            }
467
468            // Complete hack to approximate old AST-borrowck diagnostic: if the span starts
469            // with a mutable borrow of a local variable, then just suggest the user remove it.
470            PlaceRef { local: _, projection: [] }
471                if self
472                    .infcx
473                    .tcx
474                    .sess
475                    .source_map()
476                    .span_to_snippet(span)
477                    .is_ok_and(|snippet| snippet.starts_with("&mut ")) =>
478            {
479                err.span_label(span, format!("cannot {act}"));
480                err.span_suggestion_verbose(
481                    span.with_hi(span.lo() + BytePos(5)),
482                    "try removing `&mut` here",
483                    "",
484                    Applicability::MaybeIncorrect,
485                );
486            }
487
488            PlaceRef { local, projection: [ProjectionElem::Deref] }
489                if self.body.local_decls[local].is_ref_for_guard() =>
490            {
491                err.span_label(span, format!("cannot {act}"));
492                err.note(
493                    "variables bound in patterns are immutable until the end of the pattern guard",
494                );
495            }
496
497            // We want to point out when a `&` can be readily replaced
498            // with an `&mut`.
499            //
500            // FIXME: can this case be generalized to work for an
501            // arbitrary base for the projection?
502            PlaceRef { local, projection: [ProjectionElem::Deref] }
503                if self.body.local_decls[local].is_user_variable() =>
504            {
505                let local_decl = &self.body.local_decls[local];
506
507                let (pointer_sigil, pointer_desc) =
508                    if local_decl.ty.is_ref() { ("&", "reference") } else { ("*const", "pointer") };
509
510                match self.local_name(local) {
511                    Some(name) if !local_decl.from_compiler_desugaring() => {
512                        err.span_label(
513                            span,
514                            format!(
515                                "`{name}` is a `{pointer_sigil}` {pointer_desc}, \
516                                 so the data it refers to cannot be {acted_on}",
517                            ),
518                        );
519
520                        self.suggest_using_iter_mut(&mut err);
521                        self.suggest_make_local_mut(&mut err, local, name);
522                    }
523                    _ => {
524                        err.span_label(
525                            span,
526                            format!("cannot {act} through `{pointer_sigil}` {pointer_desc}"),
527                        );
528                    }
529                }
530            }
531
532            PlaceRef { local, projection: [ProjectionElem::Deref] }
533                if local == ty::CAPTURE_STRUCT_LOCAL && !self.upvars.is_empty() =>
534            {
535                self.expected_fn_found_fn_mut_call(&mut err, span, act);
536            }
537
538            PlaceRef { local: _, projection: [.., ProjectionElem::Deref] } => {
539                err.span_label(span, format!("cannot {act}"));
540
541                match opt_source {
542                    Some(BorrowedContentSource::OverloadedDeref(ty)) => {
543                        err.help(format!(
544                            "trait `DerefMut` is required to modify through a dereference, \
545                             but it is not implemented for `{ty}`",
546                        ));
547                    }
548                    Some(BorrowedContentSource::OverloadedIndex(ty)) => {
549                        err.help(format!(
550                            "trait `IndexMut` is required to modify indexed content, \
551                             but it is not implemented for `{ty}`",
552                        ));
553                        self.suggest_map_index_mut_alternatives(ty, &mut err, span);
554                    }
555                    _ => (),
556                }
557            }
558
559            _ => {
560                err.span_label(span, format!("cannot {act}"));
561            }
562        }
563
564        if let Some(span) = mut_error {
565            self.buffer_mut_error(span, err, count);
566        } else {
567            self.buffer_error(err);
568        }
569    }
570
571    /// Suggest `map[k] = v` => `map.insert(k, v)` and the like.
572    fn suggest_map_index_mut_alternatives(&self, ty: Ty<'tcx>, err: &mut Diag<'infcx>, span: Span) {
573        let Some(adt) = ty.ty_adt_def() else { return };
574        let did = adt.did();
575        if self.infcx.tcx.is_diagnostic_item(sym::HashMap, did)
576            || self.infcx.tcx.is_diagnostic_item(sym::BTreeMap, did)
577        {
578            /// Walks through the HIR, looking for the corresponding span for this error.
579            /// When it finds it, see if it corresponds to assignment operator whose LHS
580            /// is an index expr.
581            struct SuggestIndexOperatorAlternativeVisitor<'a, 'infcx, 'tcx> {
582                assign_span: Span,
583                err: &'a mut Diag<'infcx>,
584                ty: Ty<'tcx>,
585                suggested: bool,
586            }
587            impl<'a, 'infcx, 'tcx> Visitor<'tcx> for SuggestIndexOperatorAlternativeVisitor<'a, 'infcx, 'tcx> {
588                fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt<'tcx>) {
589                    hir::intravisit::walk_stmt(self, stmt);
590                    let expr = match stmt.kind {
591                        hir::StmtKind::Semi(expr) | hir::StmtKind::Expr(expr) => expr,
592                        hir::StmtKind::Let(hir::LetStmt { init: Some(expr), .. }) => expr,
593                        _ => {
594                            return;
595                        }
596                    };
597                    if let hir::ExprKind::Assign(place, rv, _sp) = expr.kind
598                        && let hir::ExprKind::Index(val, index, _) = place.kind
599                        && (expr.span == self.assign_span || place.span == self.assign_span)
600                    {
601                        // val[index] = rv;
602                        // ---------- place
603                        self.err.multipart_suggestions(
604                            format!(
605                                "use `.insert()` to insert a value into a `{}`, `.get_mut()` \
606                                to modify it, or the entry API for more flexibility",
607                                self.ty,
608                            ),
609                            vec![
610                                vec![
611                                    // val.insert(index, rv);
612                                    (
613                                        val.span.shrink_to_hi().with_hi(index.span.lo()),
614                                        ".insert(".to_string(),
615                                    ),
616                                    (
617                                        index.span.shrink_to_hi().with_hi(rv.span.lo()),
618                                        ", ".to_string(),
619                                    ),
620                                    (rv.span.shrink_to_hi(), ")".to_string()),
621                                ],
622                                vec![
623                                    // if let Some(v) = val.get_mut(index) { *v = rv; }
624                                    (val.span.shrink_to_lo(), "if let Some(val) = ".to_string()),
625                                    (
626                                        val.span.shrink_to_hi().with_hi(index.span.lo()),
627                                        ".get_mut(".to_string(),
628                                    ),
629                                    (
630                                        index.span.shrink_to_hi().with_hi(place.span.hi()),
631                                        ") { *val".to_string(),
632                                    ),
633                                    (rv.span.shrink_to_hi(), "; }".to_string()),
634                                ],
635                                vec![
636                                    // let x = val.entry(index).or_insert(rv);
637                                    (val.span.shrink_to_lo(), "let val = ".to_string()),
638                                    (
639                                        val.span.shrink_to_hi().with_hi(index.span.lo()),
640                                        ".entry(".to_string(),
641                                    ),
642                                    (
643                                        index.span.shrink_to_hi().with_hi(rv.span.lo()),
644                                        ").or_insert(".to_string(),
645                                    ),
646                                    (rv.span.shrink_to_hi(), ")".to_string()),
647                                ],
648                            ],
649                            Applicability::MachineApplicable,
650                        );
651                        self.suggested = true;
652                    } else if let hir::ExprKind::MethodCall(_path, receiver, _, sp) = expr.kind
653                        && let hir::ExprKind::Index(val, index, _) = receiver.kind
654                        && receiver.span == self.assign_span
655                    {
656                        // val[index].path(args..);
657                        self.err.multipart_suggestion(
658                            format!("to modify a `{}` use `.get_mut()`", self.ty),
659                            vec![
660                                (val.span.shrink_to_lo(), "if let Some(val) = ".to_string()),
661                                (
662                                    val.span.shrink_to_hi().with_hi(index.span.lo()),
663                                    ".get_mut(".to_string(),
664                                ),
665                                (
666                                    index.span.shrink_to_hi().with_hi(receiver.span.hi()),
667                                    ") { val".to_string(),
668                                ),
669                                (sp.shrink_to_hi(), "; }".to_string()),
670                            ],
671                            Applicability::MachineApplicable,
672                        );
673                        self.suggested = true;
674                    }
675                }
676            }
677            let def_id = self.body.source.def_id();
678            let Some(local_def_id) = def_id.as_local() else { return };
679            let Some(body) = self.infcx.tcx.hir_maybe_body_owned_by(local_def_id) else { return };
680
681            let mut v = SuggestIndexOperatorAlternativeVisitor {
682                assign_span: span,
683                err,
684                ty,
685                suggested: false,
686            };
687            v.visit_body(&body);
688            if !v.suggested {
689                err.help(format!(
690                    "to modify a `{ty}`, use `.get_mut()`, `.insert()` or the entry API",
691                ));
692            }
693        }
694    }
695
696    /// User cannot make signature of a trait mutable without changing the
697    /// trait. So we find if this error belongs to a trait and if so we move
698    /// suggestion to the trait or disable it if it is out of scope of this crate
699    ///
700    /// The returned values are:
701    ///  - is the current item an assoc `fn` of an impl that corresponds to a trait def? if so, we
702    ///    have to suggest changing both the impl `fn` arg and the trait `fn` arg
703    ///  - is the trait from the local crate? If not, we can't suggest changing signatures
704    ///  - `Span` of the argument in the trait definition
705    fn is_error_in_trait(&self, local: Local) -> (bool, bool, Option<Span>) {
706        let tcx = self.infcx.tcx;
707        if self.body.local_kind(local) != LocalKind::Arg {
708            return (false, false, None);
709        }
710        let my_def = self.body.source.def_id();
711        let Some(td) = tcx.trait_impl_of_assoc(my_def).map(|id| self.infcx.tcx.impl_trait_id(id))
712        else {
713            return (false, false, None);
714        };
715
716        let implemented_trait_item = self.infcx.tcx.trait_item_of(my_def);
717
718        (
719            true,
720            td.is_local(),
721            implemented_trait_item.and_then(|f_in_trait| {
722                let f_in_trait = f_in_trait.as_local()?;
723                if let Node::TraitItem(ti) = self.infcx.tcx.hir_node_by_def_id(f_in_trait)
724                    && let hir::TraitItemKind::Fn(sig, _) = ti.kind
725                    && let Some(ty) = sig.decl.inputs.get(local.index() - 1)
726                    && let hir::TyKind::Ref(_, mut_ty) = ty.kind
727                    && let hir::Mutability::Not = mut_ty.mutbl
728                    && sig.decl.implicit_self.has_implicit_self()
729                {
730                    Some(ty.span)
731                } else {
732                    None
733                }
734            }),
735        )
736    }
737
738    fn construct_mut_suggestion_for_local_binding_patterns(
739        &self,
740        err: &mut Diag<'_>,
741        local: Local,
742    ) {
743        let local_decl = &self.body.local_decls[local];
744        debug!("local_decl: {:?}", local_decl);
745        let pat_span = match *local_decl.local_info() {
746            LocalInfo::User(BindingForm::Var(mir::VarBindingForm {
747                binding_mode: BindingMode(ByRef::No, Mutability::Not),
748                opt_ty_info: _,
749                opt_match_place: _,
750                pat_span,
751                introductions: _,
752            })) => pat_span,
753            _ => local_decl.source_info.span,
754        };
755
756        // With ref-binding patterns, the mutability suggestion has to apply to
757        // the binding, not the reference (which would be a type error):
758        //
759        // `let &b = a;` -> `let &(mut b) = a;`
760        // or
761        // `fn foo(&x: &i32)` -> `fn foo(&(mut x): &i32)`
762        let def_id = self.body.source.def_id();
763        if let Some(local_def_id) = def_id.as_local()
764            && let Some(body) = self.infcx.tcx.hir_maybe_body_owned_by(local_def_id)
765            && let Some(hir_id) = (BindingFinder { span: pat_span }).visit_body(&body).break_value()
766            && let node = self.infcx.tcx.hir_node(hir_id)
767            && let hir::Node::LetStmt(hir::LetStmt {
768                pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. },
769                ..
770            })
771            | hir::Node::Param(Param {
772                pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. },
773                ..
774            }) = node
775        {
776            err.multipart_suggestion(
777                "consider changing this to be mutable",
778                vec![
779                    (pat_span.until(local_decl.source_info.span), "&(mut ".to_string()),
780                    (
781                        local_decl.source_info.span.shrink_to_hi().with_hi(pat_span.hi()),
782                        ")".to_string(),
783                    ),
784                ],
785                Applicability::MachineApplicable,
786            );
787            return;
788        }
789
790        err.span_suggestion_verbose(
791            local_decl.source_info.span.shrink_to_lo(),
792            "consider changing this to be mutable",
793            "mut ",
794            Applicability::MachineApplicable,
795        );
796    }
797
798    // Point to span of upvar making closure call that requires a mutable borrow
799    fn show_mutating_upvar(
800        &self,
801        tcx: TyCtxt<'_>,
802        closure_local_def_id: hir::def_id::LocalDefId,
803        the_place_err: PlaceRef<'tcx>,
804        err: &mut Diag<'_>,
805    ) {
806        let tables = tcx.typeck(closure_local_def_id);
807        if let Some((span, closure_kind_origin)) = tcx.closure_kind_origin(closure_local_def_id) {
808            let reason = if let PlaceBase::Upvar(upvar_id) = closure_kind_origin.base {
809                let upvar = ty::place_to_string_for_capture(tcx, closure_kind_origin);
810                let root_hir_id = upvar_id.var_path.hir_id;
811                // We have an origin for this closure kind starting at this root variable so it's
812                // safe to unwrap here.
813                let captured_places =
814                    tables.closure_min_captures[&closure_local_def_id].get(&root_hir_id).unwrap();
815
816                let origin_projection = closure_kind_origin
817                    .projections
818                    .iter()
819                    .map(|proj| proj.kind)
820                    .collect::<Vec<_>>();
821                let mut capture_reason = String::new();
822                for captured_place in captured_places {
823                    let captured_place_kinds = captured_place
824                        .place
825                        .projections
826                        .iter()
827                        .map(|proj| proj.kind)
828                        .collect::<Vec<_>>();
829                    if rustc_middle::ty::is_ancestor_or_same_capture(
830                        &captured_place_kinds,
831                        &origin_projection,
832                    ) {
833                        match captured_place.info.capture_kind {
834                            ty::UpvarCapture::ByRef(
835                                ty::BorrowKind::Mutable | ty::BorrowKind::UniqueImmutable,
836                            ) => {
837                                capture_reason = format!("mutable borrow of `{upvar}`");
838                            }
839                            ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {
840                                capture_reason = format!("possible mutation of `{upvar}`");
841                            }
842                            _ => bug!("upvar `{upvar}` borrowed, but not mutably"),
843                        }
844                        break;
845                    }
846                }
847                if capture_reason.is_empty() {
848                    bug!("upvar `{upvar}` borrowed, but cannot find reason");
849                }
850                capture_reason
851            } else {
852                bug!("not an upvar")
853            };
854            // Sometimes we deliberately don't store the name of a place when coming from a macro in
855            // another crate. We generally want to limit those diagnostics a little, to hide
856            // implementation details (such as those from pin!() or format!()). In that case show a
857            // slightly different error message, or none at all if something else happened. In other
858            // cases the message is likely not useful.
859            if let Some(place_name) = self.describe_place(the_place_err) {
860                err.span_label(
861                    *span,
862                    format!("calling `{place_name}` requires mutable binding due to {reason}"),
863                );
864            } else if span.from_expansion() {
865                err.span_label(
866                    *span,
867                    format!("a call in this macro requires a mutable binding due to {reason}",),
868                );
869            }
870        }
871    }
872
873    // Attempt to search similar mutable associated items for suggestion.
874    // In the future, attempt in all path but initially for RHS of for_loop
875    fn suggest_similar_mut_method_for_for_loop(&self, err: &mut Diag<'_>, span: Span) {
876        use hir::ExprKind::{AddrOf, Block, Call, MethodCall};
877        use hir::{BorrowKind, Expr};
878
879        let tcx = self.infcx.tcx;
880        struct Finder {
881            span: Span,
882        }
883
884        impl<'tcx> Visitor<'tcx> for Finder {
885            type Result = ControlFlow<&'tcx Expr<'tcx>>;
886            fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) -> Self::Result {
887                if e.span == self.span {
888                    ControlFlow::Break(e)
889                } else {
890                    hir::intravisit::walk_expr(self, e)
891                }
892            }
893        }
894        if let Some(body) = tcx.hir_maybe_body_owned_by(self.mir_def_id())
895            && let Block(block, _) = body.value.kind
896        {
897            // `span` corresponds to the expression being iterated, find the `for`-loop desugared
898            // expression with that span in order to identify potential fixes when encountering a
899            // read-only iterator that should be mutable.
900            if let ControlFlow::Break(expr) = (Finder { span }).visit_block(block)
901                && let Call(_, [expr]) = expr.kind
902            {
903                match expr.kind {
904                    MethodCall(path_segment, _, _, span) => {
905                        // We have `for _ in iter.read_only_iter()`, try to
906                        // suggest `for _ in iter.mutable_iter()` instead.
907                        let opt_suggestions = tcx
908                            .typeck(path_segment.hir_id.owner.def_id)
909                            .type_dependent_def_id(expr.hir_id)
910                            .and_then(|def_id| tcx.impl_of_assoc(def_id))
911                            .map(|def_id| tcx.associated_items(def_id))
912                            .map(|assoc_items| {
913                                assoc_items
914                                    .in_definition_order()
915                                    .map(|assoc_item_def| assoc_item_def.ident(tcx))
916                                    .filter(|&ident| {
917                                        let original_method_ident = path_segment.ident;
918                                        original_method_ident != ident
919                                            && ident.as_str().starts_with(
920                                                &original_method_ident.name.to_string(),
921                                            )
922                                    })
923                                    .map(|ident| format!("{ident}()"))
924                                    .peekable()
925                            });
926
927                        if let Some(mut suggestions) = opt_suggestions
928                            && suggestions.peek().is_some()
929                        {
930                            err.span_suggestions(
931                                span,
932                                "use mutable method",
933                                suggestions,
934                                Applicability::MaybeIncorrect,
935                            );
936                        }
937                    }
938                    AddrOf(BorrowKind::Ref, Mutability::Not, expr) => {
939                        // We have `for _ in &i`, suggest `for _ in &mut i`.
940                        err.span_suggestion_verbose(
941                            expr.span.shrink_to_lo(),
942                            "use a mutable iterator instead",
943                            "mut ",
944                            Applicability::MachineApplicable,
945                        );
946                    }
947                    _ => {}
948                }
949            }
950        }
951    }
952
953    /// Targeted error when encountering an `FnMut` closure where an `Fn` closure was expected.
954    fn expected_fn_found_fn_mut_call(&self, err: &mut Diag<'_>, sp: Span, act: &str) {
955        err.span_label(sp, format!("cannot {act}"));
956
957        let tcx = self.infcx.tcx;
958        let closure_id = self.mir_hir_id();
959        let closure_span = tcx.def_span(self.mir_def_id());
960        let fn_call_id = tcx.parent_hir_id(closure_id);
961        let node = tcx.hir_node(fn_call_id);
962        let def_id = tcx.hir_enclosing_body_owner(fn_call_id);
963        let mut look_at_return = true;
964
965        // If the HIR node is a function or method call, get the DefId
966        // of the callee function or method, the span, and args of the call expr
967        let get_call_details = || {
968            let hir::Node::Expr(hir::Expr { hir_id, kind, .. }) = node else {
969                return None;
970            };
971
972            let typeck_results = tcx.typeck(def_id);
973
974            match kind {
975                hir::ExprKind::Call(expr, args) => {
976                    if let Some(ty::FnDef(def_id, _)) =
977                        typeck_results.node_type_opt(expr.hir_id).as_ref().map(|ty| ty.kind())
978                    {
979                        Some((*def_id, expr.span, *args))
980                    } else {
981                        None
982                    }
983                }
984                hir::ExprKind::MethodCall(_, _, args, span) => typeck_results
985                    .type_dependent_def_id(*hir_id)
986                    .map(|def_id| (def_id, *span, *args)),
987                _ => None,
988            }
989        };
990
991        // If we can detect the expression to be a function or method call where the closure was
992        // an argument, we point at the function or method definition argument...
993        if let Some((callee_def_id, call_span, call_args)) = get_call_details() {
994            let arg_pos = call_args
995                .iter()
996                .enumerate()
997                .filter(|(_, arg)| arg.hir_id == closure_id)
998                .map(|(pos, _)| pos)
999                .next();
1000
1001            let arg = match tcx.hir_get_if_local(callee_def_id) {
1002                Some(
1003                    hir::Node::Item(hir::Item {
1004                        kind: hir::ItemKind::Fn { ident, sig, .. }, ..
1005                    })
1006                    | hir::Node::TraitItem(hir::TraitItem {
1007                        ident,
1008                        kind: hir::TraitItemKind::Fn(sig, _),
1009                        ..
1010                    })
1011                    | hir::Node::ImplItem(hir::ImplItem {
1012                        ident,
1013                        kind: hir::ImplItemKind::Fn(sig, _),
1014                        ..
1015                    }),
1016                ) => Some(
1017                    arg_pos
1018                        .and_then(|pos| {
1019                            sig.decl.inputs.get(
1020                                pos + if sig.decl.implicit_self.has_implicit_self() {
1021                                    1
1022                                } else {
1023                                    0
1024                                },
1025                            )
1026                        })
1027                        .map(|arg| arg.span)
1028                        .unwrap_or(ident.span),
1029                ),
1030                _ => None,
1031            };
1032            if let Some(span) = arg {
1033                err.span_label(span, "change this to accept `FnMut` instead of `Fn`");
1034                err.span_label(call_span, "expects `Fn` instead of `FnMut`");
1035                err.span_label(closure_span, "in this closure");
1036                look_at_return = false;
1037            }
1038        }
1039
1040        if look_at_return && tcx.hir_get_fn_id_for_return_block(closure_id).is_some() {
1041            // ...otherwise we are probably in the tail expression of the function, point at the
1042            // return type.
1043            match tcx.hir_node_by_def_id(tcx.hir_get_parent_item(fn_call_id).def_id) {
1044                hir::Node::Item(hir::Item {
1045                    kind: hir::ItemKind::Fn { ident, sig, .. }, ..
1046                })
1047                | hir::Node::TraitItem(hir::TraitItem {
1048                    ident,
1049                    kind: hir::TraitItemKind::Fn(sig, _),
1050                    ..
1051                })
1052                | hir::Node::ImplItem(hir::ImplItem {
1053                    ident,
1054                    kind: hir::ImplItemKind::Fn(sig, _),
1055                    ..
1056                }) => {
1057                    err.span_label(ident.span, "");
1058                    err.span_label(
1059                        sig.decl.output.span(),
1060                        "change this to return `FnMut` instead of `Fn`",
1061                    );
1062                    err.span_label(closure_span, "in this closure");
1063                }
1064                _ => {}
1065            }
1066        }
1067    }
1068
1069    fn suggest_using_iter_mut(&self, err: &mut Diag<'_>) {
1070        let source = self.body.source;
1071        if let InstanceKind::Item(def_id) = source.instance
1072            && let Some(Node::Expr(hir::Expr { hir_id, kind, .. })) =
1073                self.infcx.tcx.hir_get_if_local(def_id)
1074            && let ExprKind::Closure(hir::Closure { kind: hir::ClosureKind::Closure, .. }) = kind
1075            && let Node::Expr(expr) = self.infcx.tcx.parent_hir_node(*hir_id)
1076        {
1077            let mut cur_expr = expr;
1078            while let ExprKind::MethodCall(path_segment, recv, _, _) = cur_expr.kind {
1079                if path_segment.ident.name == sym::iter {
1080                    // Check that the type has an `iter_mut` method.
1081                    let res = self
1082                        .infcx
1083                        .tcx
1084                        .typeck(path_segment.hir_id.owner.def_id)
1085                        .type_dependent_def_id(cur_expr.hir_id)
1086                        .and_then(|def_id| self.infcx.tcx.impl_of_assoc(def_id))
1087                        .map(|def_id| self.infcx.tcx.associated_items(def_id))
1088                        .map(|assoc_items| {
1089                            assoc_items.filter_by_name_unhygienic(sym::iter_mut).peekable()
1090                        });
1091
1092                    if let Some(mut res) = res
1093                        && res.peek().is_some()
1094                    {
1095                        err.span_suggestion_verbose(
1096                            path_segment.ident.span,
1097                            "you may want to use `iter_mut` here",
1098                            "iter_mut",
1099                            Applicability::MaybeIncorrect,
1100                        );
1101                    }
1102                    break;
1103                } else {
1104                    cur_expr = recv;
1105                }
1106            }
1107        }
1108    }
1109
1110    fn suggest_make_local_mut(&self, err: &mut Diag<'_>, local: Local, name: Symbol) {
1111        let local_decl = &self.body.local_decls[local];
1112
1113        let (pointer_sigil, pointer_desc) =
1114            if local_decl.ty.is_ref() { ("&", "reference") } else { ("*const", "pointer") };
1115
1116        let (is_trait_sig, is_local, local_trait) = self.is_error_in_trait(local);
1117
1118        if is_trait_sig && !is_local {
1119            // Do not suggest changing the signature when the trait comes from another crate.
1120            err.span_label(
1121                local_decl.source_info.span,
1122                format!("this is an immutable {pointer_desc}"),
1123            );
1124            return;
1125        }
1126        let decl_span = local_decl.source_info.span;
1127
1128        let (amp_mut_sugg, local_var_ty_info) = match *local_decl.local_info() {
1129            LocalInfo::User(mir::BindingForm::ImplicitSelf(_)) => {
1130                let (span, suggestion) = suggest_ampmut_self(self.infcx.tcx, decl_span);
1131                let additional = local_trait.map(|span| suggest_ampmut_self(self.infcx.tcx, span));
1132                (AmpMutSugg::Type { span, suggestion, additional }, None)
1133            }
1134
1135            LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
1136                binding_mode: BindingMode(ByRef::No, _),
1137                opt_ty_info,
1138                ..
1139            })) => {
1140                // Check if the RHS is from desugaring.
1141                let first_assignment = find_assignments(&self.body, local).first().copied();
1142                let first_assignment_stmt = first_assignment
1143                    .and_then(|loc| self.body[loc.block].statements.get(loc.statement_index));
1144                trace!(?first_assignment_stmt);
1145                let opt_assignment_rhs_span =
1146                    first_assignment.map(|loc| self.body.source_info(loc).span);
1147                let mut source_span = opt_assignment_rhs_span;
1148                if let Some(mir::Statement {
1149                    source_info: _,
1150                    kind:
1151                        mir::StatementKind::Assign(box (_, mir::Rvalue::Use(mir::Operand::Copy(place)))),
1152                    ..
1153                }) = first_assignment_stmt
1154                {
1155                    let local_span = self.body.local_decls[place.local].source_info.span;
1156                    // `&self` in async functions have a `desugaring_kind`, but the local we assign
1157                    // it with does not, so use the local_span for our checks later.
1158                    source_span = Some(local_span);
1159                    if let Some(DesugaringKind::ForLoop) = local_span.desugaring_kind() {
1160                        // On for loops, RHS points to the iterator part.
1161                        self.suggest_similar_mut_method_for_for_loop(err, local_span);
1162                        err.span_label(
1163                            local_span,
1164                            format!("this iterator yields `{pointer_sigil}` {pointer_desc}s",),
1165                        );
1166                        return;
1167                    }
1168                }
1169
1170                // Don't create labels for compiler-generated spans or spans not from users' code.
1171                if source_span.is_some_and(|s| {
1172                    s.desugaring_kind().is_some() || self.infcx.tcx.sess.source_map().is_imported(s)
1173                }) {
1174                    return;
1175                }
1176
1177                // This could be because we're in an `async fn`.
1178                if name == kw::SelfLower && opt_ty_info.is_none() {
1179                    let (span, suggestion) = suggest_ampmut_self(self.infcx.tcx, decl_span);
1180                    (AmpMutSugg::Type { span, suggestion, additional: None }, None)
1181                } else if let Some(sugg) =
1182                    suggest_ampmut(self.infcx, self.body(), first_assignment_stmt)
1183                {
1184                    (sugg, opt_ty_info)
1185                } else {
1186                    return;
1187                }
1188            }
1189
1190            LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
1191                binding_mode: BindingMode(ByRef::Yes(_), _),
1192                ..
1193            })) => {
1194                let pattern_span: Span = local_decl.source_info.span;
1195                let Some(span) = suggest_ref_mut(self.infcx.tcx, pattern_span) else {
1196                    return;
1197                };
1198                (AmpMutSugg::Type { span, suggestion: "mut ".to_owned(), additional: None }, None)
1199            }
1200
1201            _ => unreachable!(),
1202        };
1203
1204        let mut suggest = |suggs: Vec<_>, applicability, extra| {
1205            if suggs.iter().any(|(span, _)| self.infcx.tcx.sess.source_map().is_imported(*span)) {
1206                return;
1207            }
1208
1209            err.multipart_suggestion_verbose(
1210                format!(
1211                    "consider changing this to be a mutable {pointer_desc}{}{extra}",
1212                    if is_trait_sig {
1213                        " in the `impl` method and the `trait` definition"
1214                    } else {
1215                        ""
1216                    }
1217                ),
1218                suggs,
1219                applicability,
1220            );
1221        };
1222
1223        let (mut sugg, add_type_annotation_if_not_exists) = match amp_mut_sugg {
1224            AmpMutSugg::Type { span, suggestion, additional } => {
1225                let mut sugg = vec![(span, suggestion)];
1226                sugg.extend(additional);
1227                suggest(sugg, Applicability::MachineApplicable, "");
1228                return;
1229            }
1230            AmpMutSugg::MapGetMut { span, suggestion } => {
1231                if self.infcx.tcx.sess.source_map().is_imported(span) {
1232                    return;
1233                }
1234                err.multipart_suggestion_verbose(
1235                    "consider using `get_mut`",
1236                    vec![(span, suggestion)],
1237                    Applicability::MaybeIncorrect,
1238                );
1239                return;
1240            }
1241            AmpMutSugg::Expr { span, suggestion } => {
1242                // `Expr` suggestions should change type annotations if they already exist (probably immut),
1243                // but do not add new type annotations.
1244                (vec![(span, suggestion)], false)
1245            }
1246            AmpMutSugg::ChangeBinding => (vec![], true),
1247        };
1248
1249        // Find a binding's type to make mutable.
1250        let (binding_exists, span) = match local_var_ty_info {
1251            // If this is a variable binding with an explicit type,
1252            // then we will suggest changing it to be mutable.
1253            // This is `Applicability::MachineApplicable`.
1254            Some(ty_span) => (true, ty_span),
1255
1256            // Otherwise, we'll suggest *adding* an annotated type, we'll suggest
1257            // the RHS's type for that.
1258            // This is `Applicability::HasPlaceholders`.
1259            None => (false, decl_span),
1260        };
1261
1262        if !binding_exists && !add_type_annotation_if_not_exists {
1263            suggest(sugg, Applicability::MachineApplicable, "");
1264            return;
1265        }
1266
1267        // If the binding already exists and is a reference with an explicit
1268        // lifetime, then we can suggest adding ` mut`. This is special-cased from
1269        // the path without an explicit lifetime.
1270        let (sugg_span, sugg_str, suggest_now) = if let Ok(src) = self.infcx.tcx.sess.source_map().span_to_snippet(span)
1271            && src.starts_with("&'")
1272            // Note that `&' a T` is invalid so this is correct.
1273            && let Some(ws_pos) = src.find(char::is_whitespace)
1274        {
1275            let span = span.with_lo(span.lo() + BytePos(ws_pos as u32)).shrink_to_lo();
1276            (span, " mut".to_owned(), true)
1277        // If there is already a binding, we modify it to be `mut`.
1278        } else if binding_exists {
1279            // Shrink the span to just after the `&` in `&variable`.
1280            let span = span.with_lo(span.lo() + BytePos(1)).shrink_to_lo();
1281            (span, "mut ".to_owned(), true)
1282        } else {
1283            // Otherwise, suggest that the user annotates the binding; We provide the
1284            // type of the local.
1285            let ty = local_decl.ty.builtin_deref(true).unwrap();
1286
1287            (span, format!("{}mut {}", if local_decl.ty.is_ref() { "&" } else { "*" }, ty), false)
1288        };
1289
1290        if suggest_now {
1291            // Suggest changing `&x` to `&mut x` and changing `&T` to `&mut T` at the same time.
1292            let has_change = !sugg.is_empty();
1293            sugg.push((sugg_span, sugg_str));
1294            suggest(
1295                sugg,
1296                Applicability::MachineApplicable,
1297                // FIXME(fee1-dead) this somehow doesn't fire
1298                if has_change { " and changing the binding's type" } else { "" },
1299            );
1300            return;
1301        } else if !sugg.is_empty() {
1302            suggest(sugg, Applicability::MachineApplicable, "");
1303            return;
1304        }
1305
1306        let def_id = self.body.source.def_id();
1307        let hir_id = if let Some(local_def_id) = def_id.as_local()
1308            && let Some(body) = self.infcx.tcx.hir_maybe_body_owned_by(local_def_id)
1309        {
1310            BindingFinder { span: sugg_span }.visit_body(&body).break_value()
1311        } else {
1312            None
1313        };
1314        let node = hir_id.map(|hir_id| self.infcx.tcx.hir_node(hir_id));
1315
1316        let Some(hir::Node::LetStmt(local)) = node else {
1317            err.span_label(
1318                sugg_span,
1319                format!("consider changing this binding's type to be: `{sugg_str}`"),
1320            );
1321            return;
1322        };
1323
1324        let tables = self.infcx.tcx.typeck(def_id.as_local().unwrap());
1325        if let Some(clone_trait) = self.infcx.tcx.lang_items().clone_trait()
1326            && let Some(expr) = local.init
1327            && let ty = tables.node_type_opt(expr.hir_id)
1328            && let Some(ty) = ty
1329            && let ty::Ref(..) = ty.kind()
1330        {
1331            match self
1332                .infcx
1333                .type_implements_trait_shallow(clone_trait, ty.peel_refs(), self.infcx.param_env)
1334                .as_deref()
1335            {
1336                Some([]) => {
1337                    // FIXME: This error message isn't useful, since we're just
1338                    // vaguely suggesting to clone a value that already
1339                    // implements `Clone`.
1340                    //
1341                    // A correct suggestion here would take into account the fact
1342                    // that inference may be affected by missing types on bindings,
1343                    // etc., to improve "tests/ui/borrowck/issue-91206.stderr", for
1344                    // example.
1345                }
1346                None => {
1347                    if let hir::ExprKind::MethodCall(segment, _rcvr, [], span) = expr.kind
1348                        && segment.ident.name == sym::clone
1349                    {
1350                        err.span_help(
1351                            span,
1352                            format!(
1353                                "`{}` doesn't implement `Clone`, so this call clones \
1354                                             the reference `{ty}`",
1355                                ty.peel_refs(),
1356                            ),
1357                        );
1358                    }
1359                    // The type doesn't implement Clone.
1360                    let trait_ref = ty::Binder::dummy(ty::TraitRef::new(
1361                        self.infcx.tcx,
1362                        clone_trait,
1363                        [ty.peel_refs()],
1364                    ));
1365                    let obligation = traits::Obligation::new(
1366                        self.infcx.tcx,
1367                        traits::ObligationCause::dummy(),
1368                        self.infcx.param_env,
1369                        trait_ref,
1370                    );
1371                    self.infcx.err_ctxt().suggest_derive(
1372                        &obligation,
1373                        err,
1374                        trait_ref.upcast(self.infcx.tcx),
1375                    );
1376                }
1377                Some(errors) => {
1378                    if let hir::ExprKind::MethodCall(segment, _rcvr, [], span) = expr.kind
1379                        && segment.ident.name == sym::clone
1380                    {
1381                        err.span_help(
1382                            span,
1383                            format!(
1384                                "`{}` doesn't implement `Clone` because its \
1385                                             implementations trait bounds could not be met, so \
1386                                             this call clones the reference `{ty}`",
1387                                ty.peel_refs(),
1388                            ),
1389                        );
1390                        err.note(format!(
1391                            "the following trait bounds weren't met: {}",
1392                            errors
1393                                .iter()
1394                                .map(|e| e.obligation.predicate.to_string())
1395                                .collect::<Vec<_>>()
1396                                .join("\n"),
1397                        ));
1398                    }
1399                    // The type doesn't implement Clone because of unmet obligations.
1400                    for error in errors {
1401                        if let traits::FulfillmentErrorCode::Select(
1402                            traits::SelectionError::Unimplemented,
1403                        ) = error.code
1404                            && let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
1405                                error.obligation.predicate.kind().skip_binder()
1406                        {
1407                            self.infcx.err_ctxt().suggest_derive(
1408                                &error.obligation,
1409                                err,
1410                                error.obligation.predicate.kind().rebind(pred),
1411                            );
1412                        }
1413                    }
1414                }
1415            }
1416        }
1417        let (changing, span, sugg) = match local.ty {
1418            Some(ty) => ("changing", ty.span, sugg_str),
1419            None => ("specifying", local.pat.span.shrink_to_hi(), format!(": {sugg_str}")),
1420        };
1421        err.span_suggestion_verbose(
1422            span,
1423            format!("consider {changing} this binding's type"),
1424            sugg,
1425            Applicability::HasPlaceholders,
1426        );
1427    }
1428}
1429
1430struct BindingFinder {
1431    span: Span,
1432}
1433
1434impl<'tcx> Visitor<'tcx> for BindingFinder {
1435    type Result = ControlFlow<hir::HirId>;
1436    fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) -> Self::Result {
1437        if let hir::StmtKind::Let(local) = s.kind
1438            && local.pat.span == self.span
1439        {
1440            ControlFlow::Break(local.hir_id)
1441        } else {
1442            hir::intravisit::walk_stmt(self, s)
1443        }
1444    }
1445
1446    fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) -> Self::Result {
1447        if let hir::Pat { kind: hir::PatKind::Ref(_, _), span, .. } = param.pat
1448            && *span == self.span
1449        {
1450            ControlFlow::Break(param.hir_id)
1451        } else {
1452            ControlFlow::Continue(())
1453        }
1454    }
1455}
1456
1457fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symbol>) -> bool {
1458    debug!("local_info: {:?}, ty.kind(): {:?}", local_decl.local_info, local_decl.ty.kind());
1459
1460    match *local_decl.local_info() {
1461        // Check if mutably borrowing a mutable reference.
1462        LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
1463            binding_mode: BindingMode(ByRef::No, Mutability::Not),
1464            ..
1465        })) => matches!(local_decl.ty.kind(), ty::Ref(_, _, hir::Mutability::Mut)),
1466        LocalInfo::User(mir::BindingForm::ImplicitSelf(kind)) => {
1467            // Check if the user variable is a `&mut self` and we can therefore
1468            // suggest removing the `&mut`.
1469            //
1470            // Deliberately fall into this case for all implicit self types,
1471            // so that we don't fall into the next case with them.
1472            kind == hir::ImplicitSelfKind::RefMut
1473        }
1474        _ if Some(kw::SelfLower) == local_name => {
1475            // Otherwise, check if the name is the `self` keyword - in which case
1476            // we have an explicit self. Do the same thing in this case and check
1477            // for a `self: &mut Self` to suggest removing the `&mut`.
1478            matches!(local_decl.ty.kind(), ty::Ref(_, _, hir::Mutability::Mut))
1479        }
1480        _ => false,
1481    }
1482}
1483
1484fn suggest_ampmut_self(tcx: TyCtxt<'_>, span: Span) -> (Span, String) {
1485    match tcx.sess.source_map().span_to_snippet(span) {
1486        Ok(snippet) if snippet.ends_with("self") => {
1487            (span.with_hi(span.hi() - BytePos(4)).shrink_to_hi(), "mut ".to_string())
1488        }
1489        _ => (span, "&mut self".to_string()),
1490    }
1491}
1492
1493enum AmpMutSugg {
1494    /// Type suggestion. Changes `&self` to `&mut self`, `x: &T` to `x: &mut T`,
1495    /// `ref x` to `ref mut x`, etc.
1496    Type {
1497        span: Span,
1498        suggestion: String,
1499        additional: Option<(Span, String)>,
1500    },
1501    /// Suggestion for expressions, `&x` to `&mut x`, `&x[i]` to `&mut x[i]`, etc.
1502    Expr {
1503        span: Span,
1504        suggestion: String,
1505    },
1506    /// Suggests `.get_mut` in the case of `&map[&key]` for Hash/BTreeMap.
1507    MapGetMut {
1508        span: Span,
1509        suggestion: String,
1510    },
1511    ChangeBinding,
1512}
1513
1514// When we want to suggest a user change a local variable to be a `&mut`, there
1515// are three potential "obvious" things to highlight:
1516//
1517// let ident [: Type] [= RightHandSideExpression];
1518//     ^^^^^    ^^^^     ^^^^^^^^^^^^^^^^^^^^^^^
1519//     (1.)     (2.)              (3.)
1520//
1521// We can always fallback on highlighting the first. But chances are good that
1522// the user experience will be better if we highlight one of the others if possible;
1523// for example, if the RHS is present and the Type is not, then the type is going to
1524// be inferred *from* the RHS, which means we should highlight that (and suggest
1525// that they borrow the RHS mutably).
1526//
1527// This implementation attempts to emulate AST-borrowck prioritization
1528// by trying (3.), then (2.) and finally falling back on (1.).
1529fn suggest_ampmut<'tcx>(
1530    infcx: &crate::BorrowckInferCtxt<'tcx>,
1531    body: &Body<'tcx>,
1532    opt_assignment_rhs_stmt: Option<&Statement<'tcx>>,
1533) -> Option<AmpMutSugg> {
1534    let tcx = infcx.tcx;
1535    // If there is a RHS and it starts with a `&` from it, then check if it is
1536    // mutable, and if not, put suggest putting `mut ` to make it mutable.
1537    // We don't have to worry about lifetime annotations here because they are
1538    // not valid when taking a reference. For example, the following is not valid Rust:
1539    //
1540    // let x: &i32 = &'a 5;
1541    //                ^^ lifetime annotation not allowed
1542    //
1543    if let Some(rhs_stmt) = opt_assignment_rhs_stmt
1544        && let StatementKind::Assign(box (lhs, rvalue)) = &rhs_stmt.kind
1545        && let mut rhs_span = rhs_stmt.source_info.span
1546        && let Ok(mut rhs_str) = tcx.sess.source_map().span_to_snippet(rhs_span)
1547    {
1548        let mut rvalue = rvalue;
1549
1550        // Take some special care when handling `let _x = &*_y`:
1551        // We want to know if this is part of an overloaded index, so `let x = &a[0]`,
1552        // or whether this is a usertype ascription (`let _x: &T = y`).
1553        if let Rvalue::Ref(_, BorrowKind::Shared, place) = rvalue
1554            && place.projection.len() == 1
1555            && place.projection[0] == ProjectionElem::Deref
1556            && let Some(assign) = find_assignments(&body, place.local).first()
1557        {
1558            // If this is a usertype ascription (`let _x: &T = _y`) then pierce through it as either we want
1559            // to suggest `&mut` on the expression (handled here) or we return `None` and let the caller
1560            // suggest `&mut` on the type if the expression seems fine (e.g. `let _x: &T = &mut _y`).
1561            if let Some(user_ty_projs) = body.local_decls[lhs.local].user_ty.as_ref()
1562                && let [user_ty_proj] = user_ty_projs.contents.as_slice()
1563                && user_ty_proj.projs.is_empty()
1564                && let Either::Left(rhs_stmt_new) = body.stmt_at(*assign)
1565                && let StatementKind::Assign(box (_, rvalue_new)) = &rhs_stmt_new.kind
1566                && let rhs_span_new = rhs_stmt_new.source_info.span
1567                && let Ok(rhs_str_new) = tcx.sess.source_map().span_to_snippet(rhs_span)
1568            {
1569                (rvalue, rhs_span, rhs_str) = (rvalue_new, rhs_span_new, rhs_str_new);
1570            }
1571
1572            if let Either::Right(call) = body.stmt_at(*assign)
1573                && let TerminatorKind::Call {
1574                    func: Operand::Constant(box const_operand), args, ..
1575                } = &call.kind
1576                && let ty::FnDef(method_def_id, method_args) = *const_operand.ty().kind()
1577                && let Some(trait_) = tcx.trait_of_assoc(method_def_id)
1578                && tcx.is_lang_item(trait_, hir::LangItem::Index)
1579            {
1580                let trait_ref = ty::TraitRef::from_assoc(
1581                    tcx,
1582                    tcx.require_lang_item(hir::LangItem::IndexMut, rhs_span),
1583                    method_args,
1584                );
1585                // The type only implements `Index` but not `IndexMut`, we must not suggest `&mut`.
1586                if !infcx
1587                    .type_implements_trait(trait_ref.def_id, trait_ref.args, infcx.param_env)
1588                    .must_apply_considering_regions()
1589                {
1590                    // Suggest `get_mut` if type is a `BTreeMap` or `HashMap`.
1591                    if let ty::Adt(def, _) = trait_ref.self_ty().kind()
1592                        && [sym::BTreeMap, sym::HashMap]
1593                            .into_iter()
1594                            .any(|s| tcx.is_diagnostic_item(s, def.did()))
1595                        && let [map, key] = &**args
1596                        && let Ok(map) = tcx.sess.source_map().span_to_snippet(map.span)
1597                        && let Ok(key) = tcx.sess.source_map().span_to_snippet(key.span)
1598                    {
1599                        let span = rhs_span;
1600                        let suggestion = format!("{map}.get_mut({key}).unwrap()");
1601                        return Some(AmpMutSugg::MapGetMut { span, suggestion });
1602                    }
1603                    return None;
1604                }
1605            }
1606        }
1607
1608        let sugg = match rvalue {
1609            Rvalue::Ref(_, BorrowKind::Shared, _) if let Some(ref_idx) = rhs_str.find('&') => {
1610                // Shrink the span to just after the `&` in `&variable`.
1611                Some((
1612                    rhs_span.with_lo(rhs_span.lo() + BytePos(ref_idx as u32 + 1)).shrink_to_lo(),
1613                    "mut ".to_owned(),
1614                ))
1615            }
1616            Rvalue::RawPtr(RawPtrKind::Const, _) if let Some(const_idx) = rhs_str.find("const") => {
1617                // Suggest changing `&raw const` to `&raw mut` if applicable.
1618                let const_idx = const_idx as u32;
1619                Some((
1620                    rhs_span
1621                        .with_lo(rhs_span.lo() + BytePos(const_idx))
1622                        .with_hi(rhs_span.lo() + BytePos(const_idx + "const".len() as u32)),
1623                    "mut".to_owned(),
1624                ))
1625            }
1626            _ => None,
1627        };
1628
1629        if let Some((span, suggestion)) = sugg {
1630            return Some(AmpMutSugg::Expr { span, suggestion });
1631        }
1632    }
1633
1634    Some(AmpMutSugg::ChangeBinding)
1635}
1636
1637/// If the type is a `Coroutine`, `Closure`, or `CoroutineClosure`
1638fn is_closure_like(ty: Ty<'_>) -> bool {
1639    ty.is_closure() || ty.is_coroutine() || ty.is_coroutine_closure()
1640}
1641
1642/// Given a field that needs to be mutable, returns a span where the " mut " could go.
1643/// This function expects the local to be a reference to a struct in order to produce a span.
1644///
1645/// ```text
1646/// LL |     s: &'a   String
1647///    |           ^^^ returns a span taking up the space here
1648/// ```
1649fn get_mut_span_in_struct_field<'tcx>(
1650    tcx: TyCtxt<'tcx>,
1651    ty: Ty<'tcx>,
1652    field: FieldIdx,
1653) -> Option<Span> {
1654    // Expect our local to be a reference to a struct of some kind.
1655    if let ty::Ref(_, ty, _) = ty.kind()
1656        && let ty::Adt(def, _) = ty.kind()
1657        && let field = def.all_fields().nth(field.index())?
1658        // Now we're dealing with the actual struct that we're going to suggest a change to,
1659        // we can expect a field that is an immutable reference to a type.
1660        && let hir::Node::Field(field) = tcx.hir_node_by_def_id(field.did.as_local()?)
1661        && let hir::TyKind::Ref(lt, hir::MutTy { mutbl: hir::Mutability::Not, ty }) = field.ty.kind
1662    {
1663        return Some(lt.ident.span.between(ty.span));
1664    }
1665
1666    None
1667}
1668
1669/// If possible, suggest replacing `ref` with `ref mut`.
1670fn suggest_ref_mut(tcx: TyCtxt<'_>, span: Span) -> Option<Span> {
1671    let pattern_str = tcx.sess.source_map().span_to_snippet(span).ok()?;
1672    if let Some(rest) = pattern_str.strip_prefix("ref")
1673        && rest.starts_with(rustc_lexer::is_whitespace)
1674    {
1675        let span = span.with_lo(span.lo() + BytePos(4)).shrink_to_lo();
1676        Some(span)
1677    } else {
1678        None
1679    }
1680}