rustc_borrowck/diagnostics/
explain_borrow.rs

1//! Print diagnostics to explain why values are borrowed.
2
3#![allow(rustc::diagnostic_outside_of_impl)]
4#![allow(rustc::untranslatable_diagnostic)]
5
6use std::assert_matches::assert_matches;
7
8use rustc_errors::{Applicability, Diag, EmissionGuarantee};
9use rustc_hir as hir;
10use rustc_hir::intravisit::Visitor;
11use rustc_infer::infer::NllRegionVariableOrigin;
12use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
13use rustc_middle::mir::{
14    Body, CallSource, CastKind, ConstraintCategory, FakeReadCause, Local, LocalInfo, Location,
15    Operand, Place, Rvalue, Statement, StatementKind, TerminatorKind,
16};
17use rustc_middle::ty::adjustment::PointerCoercion;
18use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
19use rustc_span::{DesugaringKind, Span, kw, sym};
20use rustc_trait_selection::error_reporting::traits::FindExprBySpan;
21use rustc_trait_selection::error_reporting::traits::call_kind::CallKind;
22use tracing::{debug, instrument};
23
24use super::{RegionName, UseSpans, find_use};
25use crate::borrow_set::BorrowData;
26use crate::constraints::OutlivesConstraint;
27use crate::nll::ConstraintDescription;
28use crate::region_infer::{BlameConstraint, Cause};
29use crate::{MirBorrowckCtxt, WriteKind};
30
31#[derive(Debug)]
32pub(crate) enum BorrowExplanation<'tcx> {
33    UsedLater(Local, LaterUseKind, Span, Option<Span>),
34    UsedLaterInLoop(LaterUseKind, Span, Option<Span>),
35    UsedLaterWhenDropped {
36        drop_loc: Location,
37        dropped_local: Local,
38        should_note_order: bool,
39    },
40    MustBeValidFor {
41        category: ConstraintCategory<'tcx>,
42        from_closure: bool,
43        span: Span,
44        region_name: RegionName,
45        opt_place_desc: Option<String>,
46        path: Vec<OutlivesConstraint<'tcx>>,
47    },
48    Unexplained,
49}
50
51#[derive(Clone, Copy, Debug)]
52pub(crate) enum LaterUseKind {
53    TraitCapture,
54    ClosureCapture,
55    Call,
56    FakeLetRead,
57    Other,
58}
59
60impl<'tcx> BorrowExplanation<'tcx> {
61    pub(crate) fn is_explained(&self) -> bool {
62        !matches!(self, BorrowExplanation::Unexplained)
63    }
64    pub(crate) fn add_explanation_to_diagnostic<G: EmissionGuarantee>(
65        &self,
66        cx: &MirBorrowckCtxt<'_, '_, 'tcx>,
67        err: &mut Diag<'_, G>,
68        borrow_desc: &str,
69        borrow_span: Option<Span>,
70        multiple_borrow_span: Option<(Span, Span)>,
71    ) {
72        let tcx = cx.infcx.tcx;
73        let body = cx.body;
74
75        if let Some(span) = borrow_span {
76            let def_id = body.source.def_id();
77            if let Some(node) = tcx.hir_get_if_local(def_id)
78                && let Some(body_id) = node.body_id()
79            {
80                let body = tcx.hir_body(body_id);
81                let mut expr_finder = FindExprBySpan::new(span, tcx);
82                expr_finder.visit_expr(body.value);
83                if let Some(mut expr) = expr_finder.result {
84                    while let hir::ExprKind::AddrOf(_, _, inner)
85                    | hir::ExprKind::Unary(hir::UnOp::Deref, inner)
86                    | hir::ExprKind::Field(inner, _)
87                    | hir::ExprKind::MethodCall(_, inner, _, _)
88                    | hir::ExprKind::Index(inner, _, _) = &expr.kind
89                    {
90                        expr = inner;
91                    }
92                    if let hir::ExprKind::Path(hir::QPath::Resolved(None, p)) = expr.kind
93                        && let [hir::PathSegment { ident, args: None, .. }] = p.segments
94                        && let hir::def::Res::Local(hir_id) = p.res
95                        && let hir::Node::Pat(pat) = tcx.hir_node(hir_id)
96                    {
97                        if !ident.span.in_external_macro(tcx.sess.source_map()) {
98                            err.span_label(pat.span, format!("binding `{ident}` declared here"));
99                        }
100                    }
101                }
102            }
103        }
104        match *self {
105            BorrowExplanation::UsedLater(
106                dropped_local,
107                later_use_kind,
108                var_or_use_span,
109                path_span,
110            ) => {
111                let message = match later_use_kind {
112                    LaterUseKind::TraitCapture => "captured here by trait object",
113                    LaterUseKind::ClosureCapture => "captured here by closure",
114                    LaterUseKind::Call => "used by call",
115                    LaterUseKind::FakeLetRead => "stored here",
116                    LaterUseKind::Other => "used here",
117                };
118                let local_decl = &body.local_decls[dropped_local];
119
120                if let &LocalInfo::IfThenRescopeTemp { if_then } = local_decl.local_info()
121                    && let Some((_, hir::Node::Expr(expr))) = tcx.hir_parent_iter(if_then).next()
122                    && let hir::ExprKind::If(cond, conseq, alt) = expr.kind
123                    && let hir::ExprKind::Let(&hir::LetExpr {
124                        span: _,
125                        pat,
126                        init,
127                        // FIXME(#101728): enable rewrite when type ascription is stabilized again
128                        ty: None,
129                        recovered: _,
130                    }) = cond.kind
131                    && pat.span.can_be_used_for_suggestions()
132                    && let Ok(pat) = tcx.sess.source_map().span_to_snippet(pat.span)
133                {
134                    suggest_rewrite_if_let(tcx, expr, &pat, init, conseq, alt, err);
135                } else if path_span.is_none_or(|path_span| path_span == var_or_use_span) {
136                    // We can use `var_or_use_span` if either `path_span` is not present, or both
137                    // spans are the same.
138                    if borrow_span.is_none_or(|sp| !sp.overlaps(var_or_use_span)) {
139                        err.span_label(
140                            var_or_use_span,
141                            format!("{borrow_desc}borrow later {message}"),
142                        );
143                    }
144                } else {
145                    // path_span must be `Some` as otherwise the if condition is true
146                    let path_span = path_span.unwrap();
147                    // path_span is only present in the case of closure capture
148                    assert_matches!(later_use_kind, LaterUseKind::ClosureCapture);
149                    if !borrow_span.is_some_and(|sp| sp.overlaps(var_or_use_span)) {
150                        let path_label = "used here by closure";
151                        let capture_kind_label = message;
152                        err.span_label(
153                            var_or_use_span,
154                            format!("{borrow_desc}borrow later {capture_kind_label}"),
155                        );
156                        err.span_label(path_span, path_label);
157                    }
158                }
159            }
160            BorrowExplanation::UsedLaterInLoop(later_use_kind, var_or_use_span, path_span) => {
161                let message = match later_use_kind {
162                    LaterUseKind::TraitCapture => {
163                        "borrow captured here by trait object, in later iteration of loop"
164                    }
165                    LaterUseKind::ClosureCapture => {
166                        "borrow captured here by closure, in later iteration of loop"
167                    }
168                    LaterUseKind::Call => "borrow used by call, in later iteration of loop",
169                    LaterUseKind::FakeLetRead => "borrow later stored here",
170                    LaterUseKind::Other => "borrow used here, in later iteration of loop",
171                };
172                // We can use `var_or_use_span` if either `path_span` is not present, or both spans
173                // are the same.
174                if path_span.map(|path_span| path_span == var_or_use_span).unwrap_or(true) {
175                    err.span_label(var_or_use_span, format!("{borrow_desc}{message}"));
176                } else {
177                    // path_span must be `Some` as otherwise the if condition is true
178                    let path_span = path_span.unwrap();
179                    // path_span is only present in the case of closure capture
180                    assert_matches!(later_use_kind, LaterUseKind::ClosureCapture);
181                    if borrow_span.map(|sp| !sp.overlaps(var_or_use_span)).unwrap_or(true) {
182                        let path_label = "used here by closure";
183                        let capture_kind_label = message;
184                        err.span_label(
185                            var_or_use_span,
186                            format!("{borrow_desc}borrow later {capture_kind_label}"),
187                        );
188                        err.span_label(path_span, path_label);
189                    }
190                }
191            }
192            BorrowExplanation::UsedLaterWhenDropped {
193                drop_loc,
194                dropped_local,
195                should_note_order,
196            } => {
197                let local_decl = &body.local_decls[dropped_local];
198                let mut ty = local_decl.ty;
199                if local_decl.source_info.span.desugaring_kind() == Some(DesugaringKind::ForLoop) {
200                    if let ty::Adt(adt, args) = local_decl.ty.kind() {
201                        if tcx.is_diagnostic_item(sym::Option, adt.did()) {
202                            // in for loop desugaring, only look at the `Some(..)` inner type
203                            ty = args.type_at(0);
204                        }
205                    }
206                }
207                let (dtor_desc, type_desc) = match ty.kind() {
208                    // If type is an ADT that implements Drop, then
209                    // simplify output by reporting just the ADT name.
210                    ty::Adt(adt, _args) if adt.has_dtor(tcx) && !adt.is_box() => {
211                        ("`Drop` code", format!("type `{}`", tcx.def_path_str(adt.did())))
212                    }
213
214                    // Otherwise, just report the whole type (and use
215                    // the intentionally fuzzy phrase "destructor")
216                    ty::Closure(..) => ("destructor", "closure".to_owned()),
217                    ty::Coroutine(..) => ("destructor", "coroutine".to_owned()),
218
219                    _ => ("destructor", format!("type `{}`", local_decl.ty)),
220                };
221
222                match cx.local_name(dropped_local) {
223                    Some(local_name) if !local_decl.from_compiler_desugaring() => {
224                        let message = format!(
225                            "{borrow_desc}borrow might be used here, when `{local_name}` is dropped \
226                             and runs the {dtor_desc} for {type_desc}",
227                        );
228                        err.span_label(body.source_info(drop_loc).span, message);
229
230                        if should_note_order {
231                            err.note(
232                                "values in a scope are dropped \
233                                 in the opposite order they are defined",
234                            );
235                        }
236                    }
237                    _ => {
238                        err.span_label(
239                            local_decl.source_info.span,
240                            format!(
241                                "a temporary with access to the {borrow_desc}borrow \
242                                 is created here ...",
243                            ),
244                        );
245                        let message = format!(
246                            "... and the {borrow_desc}borrow might be used here, \
247                             when that temporary is dropped \
248                             and runs the {dtor_desc} for {type_desc}",
249                        );
250                        err.span_label(body.source_info(drop_loc).span, message);
251
252                        struct FindLetExpr<'hir> {
253                            span: Span,
254                            result: Option<(Span, &'hir hir::Pat<'hir>, &'hir hir::Expr<'hir>)>,
255                            tcx: TyCtxt<'hir>,
256                        }
257
258                        impl<'hir> rustc_hir::intravisit::Visitor<'hir> for FindLetExpr<'hir> {
259                            type NestedFilter = rustc_middle::hir::nested_filter::OnlyBodies;
260                            fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
261                                self.tcx
262                            }
263                            fn visit_expr(&mut self, expr: &'hir hir::Expr<'hir>) {
264                                if let hir::ExprKind::If(cond, _conseq, _alt)
265                                | hir::ExprKind::Loop(
266                                    &hir::Block {
267                                        expr:
268                                            Some(&hir::Expr {
269                                                kind: hir::ExprKind::If(cond, _conseq, _alt),
270                                                ..
271                                            }),
272                                        ..
273                                    },
274                                    _,
275                                    hir::LoopSource::While,
276                                    _,
277                                ) = expr.kind
278                                    && let hir::ExprKind::Let(hir::LetExpr {
279                                        init: let_expr_init,
280                                        span: let_expr_span,
281                                        pat: let_expr_pat,
282                                        ..
283                                    }) = cond.kind
284                                    && let_expr_init.span.contains(self.span)
285                                {
286                                    self.result =
287                                        Some((*let_expr_span, let_expr_pat, let_expr_init))
288                                } else {
289                                    hir::intravisit::walk_expr(self, expr);
290                                }
291                            }
292                        }
293
294                        if let &LocalInfo::IfThenRescopeTemp { if_then } = local_decl.local_info()
295                            && let hir::Node::Expr(expr) = tcx.hir_node(if_then)
296                            && let hir::ExprKind::If(cond, conseq, alt) = expr.kind
297                            && let hir::ExprKind::Let(&hir::LetExpr {
298                                span: _,
299                                pat,
300                                init,
301                                // FIXME(#101728): enable rewrite when type ascription is
302                                // stabilized again.
303                                ty: None,
304                                recovered: _,
305                            }) = cond.kind
306                            && pat.span.can_be_used_for_suggestions()
307                            && let Ok(pat) = tcx.sess.source_map().span_to_snippet(pat.span)
308                        {
309                            suggest_rewrite_if_let(tcx, expr, &pat, init, conseq, alt, err);
310                        } else if let Some((old, new)) = multiple_borrow_span
311                            && let def_id = body.source.def_id()
312                            && let Some(node) = tcx.hir_get_if_local(def_id)
313                            && let Some(body_id) = node.body_id()
314                            && let hir_body = tcx.hir_body(body_id)
315                            && let mut expr_finder = (FindLetExpr { span: old, result: None, tcx })
316                            && let Some((let_expr_span, let_expr_pat, let_expr_init)) = {
317                                expr_finder.visit_expr(hir_body.value);
318                                expr_finder.result
319                            }
320                            && !let_expr_span.contains(new)
321                        {
322                            // #133941: The `old` expression is at the conditional part of an
323                            // if/while let expression. Adding a semicolon won't work.
324                            // Instead, try suggesting the `matches!` macro or a temporary.
325                            if let_expr_pat
326                                .walk_short(|pat| !matches!(pat.kind, hir::PatKind::Binding(..)))
327                            {
328                                if let Ok(pat_snippet) =
329                                    tcx.sess.source_map().span_to_snippet(let_expr_pat.span)
330                                    && let Ok(init_snippet) =
331                                        tcx.sess.source_map().span_to_snippet(let_expr_init.span)
332                                {
333                                    err.span_suggestion_verbose(
334                                        let_expr_span,
335                                        "consider using the `matches!` macro",
336                                        format!("matches!({init_snippet}, {pat_snippet})"),
337                                        Applicability::MaybeIncorrect,
338                                    );
339                                } else {
340                                    err.note("consider using the `matches!` macro");
341                                }
342                            }
343                        } else if let LocalInfo::BlockTailTemp(info) = local_decl.local_info() {
344                            let sp = info.span.find_ancestor_not_from_macro().unwrap_or(info.span);
345                            if info.tail_result_is_ignored {
346                                // #85581: If the first mutable borrow's scope contains
347                                // the second borrow, this suggestion isn't helpful.
348                                if !multiple_borrow_span.is_some_and(|(old, new)| {
349                                    old.to(info.span.shrink_to_hi()).contains(new)
350                                }) {
351                                    err.span_suggestion_verbose(
352                                        sp.shrink_to_hi(),
353                                        "consider adding semicolon after the expression so its \
354                                        temporaries are dropped sooner, before the local variables \
355                                        declared by the block are dropped",
356                                        ";",
357                                        Applicability::MaybeIncorrect,
358                                    );
359                                }
360                            } else {
361                                err.note(
362                                    "the temporary is part of an expression at the end of a \
363                                     block;\nconsider forcing this temporary to be dropped sooner, \
364                                     before the block's local variables are dropped",
365                                );
366                                err.multipart_suggestion(
367                                    "for example, you could save the expression's value in a new \
368                                     local variable `x` and then make `x` be the expression at the \
369                                     end of the block",
370                                    vec![
371                                        (sp.shrink_to_lo(), "let x = ".to_string()),
372                                        (sp.shrink_to_hi(), "; x".to_string()),
373                                    ],
374                                    Applicability::MaybeIncorrect,
375                                );
376                            };
377                        }
378                    }
379                }
380            }
381            BorrowExplanation::MustBeValidFor {
382                category,
383                span,
384                ref region_name,
385                ref opt_place_desc,
386                from_closure: _,
387                ref path,
388            } => {
389                region_name.highlight_region_name(err);
390
391                if let Some(desc) = opt_place_desc {
392                    err.span_label(
393                        span,
394                        format!(
395                            "{}requires that `{desc}` is borrowed for `{region_name}`",
396                            category.description(),
397                        ),
398                    );
399                } else {
400                    err.span_label(
401                        span,
402                        format!(
403                            "{}requires that {borrow_desc}borrow lasts for `{region_name}`",
404                            category.description(),
405                        ),
406                    );
407                };
408
409                cx.add_placeholder_from_predicate_note(err, &path);
410                cx.add_sized_or_copy_bound_info(err, category, &path);
411
412                if let ConstraintCategory::Cast {
413                    is_raw_ptr_dyn_type_cast: _,
414                    is_implicit_coercion: true,
415                    unsize_to: Some(unsize_ty),
416                } = category
417                {
418                    self.add_object_lifetime_default_note(tcx, err, unsize_ty);
419                }
420
421                let mut preds = path
422                    .iter()
423                    .filter_map(|constraint| match constraint.category {
424                        ConstraintCategory::Predicate(pred) if !pred.is_dummy() => Some(pred),
425                        _ => None,
426                    })
427                    .collect::<Vec<Span>>();
428                preds.sort();
429                preds.dedup();
430                if !preds.is_empty() {
431                    let s = if preds.len() == 1 { "" } else { "s" };
432                    err.span_note(
433                        preds,
434                        format!(
435                            "requirement{s} that the value outlives `{region_name}` introduced here"
436                        ),
437                    );
438                }
439
440                self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name);
441            }
442            _ => {}
443        }
444    }
445
446    fn add_object_lifetime_default_note<G: EmissionGuarantee>(
447        &self,
448        tcx: TyCtxt<'tcx>,
449        err: &mut Diag<'_, G>,
450        unsize_ty: Ty<'tcx>,
451    ) {
452        if let ty::Adt(def, args) = unsize_ty.kind() {
453            // We try to elaborate the object lifetime defaults and present those to the user. This
454            // should make it clear where the region constraint is coming from.
455            let generics = tcx.generics_of(def.did());
456
457            let mut has_dyn = false;
458            let mut failed = false;
459
460            let elaborated_args =
461                std::iter::zip(*args, &generics.own_params).map(|(arg, param)| {
462                    if let Some(ty::Dynamic(obj, _)) = arg.as_type().map(Ty::kind) {
463                        let default = tcx.object_lifetime_default(param.def_id);
464
465                        let re_static = tcx.lifetimes.re_static;
466
467                        let implied_region = match default {
468                            // This is not entirely precise.
469                            ObjectLifetimeDefault::Empty => re_static,
470                            ObjectLifetimeDefault::Ambiguous => {
471                                failed = true;
472                                re_static
473                            }
474                            ObjectLifetimeDefault::Param(param_def_id) => {
475                                let index = generics.param_def_id_to_index[&param_def_id] as usize;
476                                args.get(index).and_then(|arg| arg.as_region()).unwrap_or_else(
477                                    || {
478                                        failed = true;
479                                        re_static
480                                    },
481                                )
482                            }
483                            ObjectLifetimeDefault::Static => re_static,
484                        };
485
486                        has_dyn = true;
487
488                        Ty::new_dynamic(tcx, obj, implied_region).into()
489                    } else {
490                        arg
491                    }
492                });
493            let elaborated_ty = Ty::new_adt(tcx, *def, tcx.mk_args_from_iter(elaborated_args));
494
495            if has_dyn && !failed {
496                err.note(format!(
497                    "due to object lifetime defaults, `{unsize_ty}` actually means `{elaborated_ty}`"
498                ));
499            }
500        }
501    }
502
503    fn add_lifetime_bound_suggestion_to_diagnostic<G: EmissionGuarantee>(
504        &self,
505        err: &mut Diag<'_, G>,
506        category: &ConstraintCategory<'tcx>,
507        span: Span,
508        region_name: &RegionName,
509    ) {
510        if !span.is_desugaring(DesugaringKind::OpaqueTy) {
511            return;
512        }
513        if let ConstraintCategory::OpaqueType = category {
514            let suggestable_name =
515                if region_name.was_named() { region_name.name } else { kw::UnderscoreLifetime };
516
517            let msg = format!(
518                "you can add a bound to the {}to make it last less than `'static` and match `{region_name}`",
519                category.description(),
520            );
521
522            err.span_suggestion_verbose(
523                span.shrink_to_hi(),
524                msg,
525                format!(" + {suggestable_name}"),
526                Applicability::Unspecified,
527            );
528        }
529    }
530}
531
532fn suggest_rewrite_if_let<G: EmissionGuarantee>(
533    tcx: TyCtxt<'_>,
534    expr: &hir::Expr<'_>,
535    pat: &str,
536    init: &hir::Expr<'_>,
537    conseq: &hir::Expr<'_>,
538    alt: Option<&hir::Expr<'_>>,
539    err: &mut Diag<'_, G>,
540) {
541    let source_map = tcx.sess.source_map();
542    err.span_note(
543        source_map.end_point(conseq.span),
544        "lifetimes for temporaries generated in `if let`s have been shortened in Edition 2024 so that they are dropped here instead",
545    );
546    if expr.span.can_be_used_for_suggestions() && conseq.span.can_be_used_for_suggestions() {
547        let needs_block = if let Some(hir::Node::Expr(expr)) =
548            alt.and_then(|alt| tcx.hir_parent_iter(alt.hir_id).next()).map(|(_, node)| node)
549        {
550            matches!(expr.kind, hir::ExprKind::If(..))
551        } else {
552            false
553        };
554        let mut sugg = vec![
555            (
556                expr.span.shrink_to_lo().between(init.span),
557                if needs_block { "{ match ".into() } else { "match ".into() },
558            ),
559            (conseq.span.shrink_to_lo(), format!(" {{ {pat} => ")),
560        ];
561        let expr_end = expr.span.shrink_to_hi();
562        let mut expr_end_code;
563        if let Some(alt) = alt {
564            sugg.push((conseq.span.between(alt.span), " _ => ".into()));
565            expr_end_code = "}".to_string();
566        } else {
567            expr_end_code = " _ => {} }".into();
568        }
569        expr_end_code.push('}');
570        sugg.push((expr_end, expr_end_code));
571        err.multipart_suggestion(
572            "consider rewriting the `if` into `match` which preserves the extended lifetime",
573            sugg,
574            Applicability::MaybeIncorrect,
575        );
576    }
577}
578
579impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
580    fn free_region_constraint_info(
581        &self,
582        borrow_region: RegionVid,
583        outlived_region: RegionVid,
584    ) -> (ConstraintCategory<'tcx>, bool, Span, Option<RegionName>, Vec<OutlivesConstraint<'tcx>>)
585    {
586        let (blame_constraint, path) = self.regioncx.best_blame_constraint(
587            borrow_region,
588            NllRegionVariableOrigin::FreeRegion,
589            outlived_region,
590        );
591        let BlameConstraint { category, from_closure, cause, .. } = blame_constraint;
592
593        let outlived_fr_name = self.give_region_a_name(outlived_region);
594
595        (category, from_closure, cause.span, outlived_fr_name, path)
596    }
597
598    /// Returns structured explanation for *why* the borrow contains the
599    /// point from `location`. This is key for the "3-point errors"
600    /// [described in the NLL RFC][d].
601    ///
602    /// # Parameters
603    ///
604    /// - `borrow`: the borrow in question
605    /// - `location`: where the borrow occurs
606    /// - `kind_place`: if Some, this describes the statement that triggered the error.
607    ///   - first half is the kind of write, if any, being performed
608    ///   - second half is the place being accessed
609    ///
610    /// [d]: https://rust-lang.github.io/rfcs/2094-nll.html#leveraging-intuition-framing-errors-in-terms-of-points
611    #[instrument(level = "debug", skip(self))]
612    pub(crate) fn explain_why_borrow_contains_point(
613        &self,
614        location: Location,
615        borrow: &BorrowData<'tcx>,
616        kind_place: Option<(WriteKind, Place<'tcx>)>,
617    ) -> BorrowExplanation<'tcx> {
618        let regioncx = &self.regioncx;
619        let body: &Body<'_> = self.body;
620        let tcx = self.infcx.tcx;
621
622        let borrow_region_vid = borrow.region;
623        debug!(?borrow_region_vid);
624
625        let mut region_sub = self.regioncx.find_sub_region_live_at(borrow_region_vid, location);
626        debug!(?region_sub);
627
628        let mut use_location = location;
629        let mut use_in_later_iteration_of_loop = false;
630
631        if region_sub == borrow_region_vid {
632            // When `region_sub` is the same as `borrow_region_vid` (the location where the borrow
633            // is issued is the same location that invalidates the reference), this is likely a
634            // loop iteration. In this case, try using the loop terminator location in
635            // `find_sub_region_live_at`.
636            if let Some(loop_terminator_location) =
637                regioncx.find_loop_terminator_location(borrow.region, body)
638            {
639                region_sub = self
640                    .regioncx
641                    .find_sub_region_live_at(borrow_region_vid, loop_terminator_location);
642                debug!("explain_why_borrow_contains_point: region_sub in loop={:?}", region_sub);
643                use_location = loop_terminator_location;
644                use_in_later_iteration_of_loop = true;
645            }
646        }
647
648        // NLL doesn't consider boring locals for liveness, and wouldn't encounter a
649        // `Cause::LiveVar` for such a local. Polonius can't avoid computing liveness for boring
650        // locals yet, and will encounter them when trying to explain why a borrow contains a given
651        // point.
652        //
653        // We want to focus on relevant live locals in diagnostics, so when polonius is enabled, we
654        // ensure that we don't emit live boring locals as explanations.
655        let is_local_boring = |local| {
656            if let Some(polonius_diagnostics) = self.polonius_diagnostics {
657                polonius_diagnostics.boring_nll_locals.contains(&local)
658            } else {
659                assert!(!tcx.sess.opts.unstable_opts.polonius.is_next_enabled());
660
661                // Boring locals are never the cause of a borrow explanation in NLLs.
662                false
663            }
664        };
665        match find_use::find(body, regioncx, tcx, region_sub, use_location) {
666            Some(Cause::LiveVar(local, location)) if !is_local_boring(local) => {
667                let span = body.source_info(location).span;
668                let spans = self
669                    .move_spans(Place::from(local).as_ref(), location)
670                    .or_else(|| self.borrow_spans(span, location));
671
672                if use_in_later_iteration_of_loop {
673                    let (later_use_kind, var_or_use_span, path_span) =
674                        self.later_use_kind(borrow, spans, use_location);
675                    BorrowExplanation::UsedLaterInLoop(later_use_kind, var_or_use_span, path_span)
676                } else {
677                    // Check if the location represents a `FakeRead`, and adapt the error
678                    // message to the `FakeReadCause` it is from: in particular,
679                    // the ones inserted in optimized `let var = <expr>` patterns.
680                    let (later_use_kind, var_or_use_span, path_span) =
681                        self.later_use_kind(borrow, spans, location);
682                    BorrowExplanation::UsedLater(
683                        borrow.borrowed_place.local,
684                        later_use_kind,
685                        var_or_use_span,
686                        path_span,
687                    )
688                }
689            }
690
691            Some(Cause::DropVar(local, location)) if !is_local_boring(local) => {
692                let mut should_note_order = false;
693                if self.local_name(local).is_some()
694                    && let Some((WriteKind::StorageDeadOrDrop, place)) = kind_place
695                    && let Some(borrowed_local) = place.as_local()
696                    && self.local_name(borrowed_local).is_some()
697                    && local != borrowed_local
698                {
699                    should_note_order = true;
700                }
701
702                BorrowExplanation::UsedLaterWhenDropped {
703                    drop_loc: location,
704                    dropped_local: local,
705                    should_note_order,
706                }
707            }
708
709            Some(Cause::LiveVar(..) | Cause::DropVar(..)) | None => {
710                // Here, under NLL: no cause was found. Under polonius: no cause was found, or a
711                // boring local was found, which we ignore like NLLs do to match its diagnostics.
712                if let Some(region) = self.to_error_region_vid(borrow_region_vid) {
713                    let (category, from_closure, span, region_name, path) =
714                        self.free_region_constraint_info(borrow_region_vid, region);
715                    if let Some(region_name) = region_name {
716                        let opt_place_desc = self.describe_place(borrow.borrowed_place.as_ref());
717                        BorrowExplanation::MustBeValidFor {
718                            category,
719                            from_closure,
720                            span,
721                            region_name,
722                            opt_place_desc,
723                            path,
724                        }
725                    } else {
726                        debug!("Could not generate a region name");
727                        BorrowExplanation::Unexplained
728                    }
729                } else {
730                    debug!("Could not generate an error region vid");
731                    BorrowExplanation::Unexplained
732                }
733            }
734        }
735    }
736
737    /// Determine how the borrow was later used.
738    /// First span returned points to the location of the conflicting use
739    /// Second span if `Some` is returned in the case of closures and points
740    /// to the use of the path
741    #[instrument(level = "debug", skip(self))]
742    fn later_use_kind(
743        &self,
744        borrow: &BorrowData<'tcx>,
745        use_spans: UseSpans<'tcx>,
746        location: Location,
747    ) -> (LaterUseKind, Span, Option<Span>) {
748        match use_spans {
749            UseSpans::ClosureUse { capture_kind_span, path_span, .. } => {
750                // Used in a closure.
751                (LaterUseKind::ClosureCapture, capture_kind_span, Some(path_span))
752            }
753            // In the case that the borrowed value (probably a temporary)
754            // overlaps with the method's receiver, then point at the method.
755            UseSpans::FnSelfUse {
756                var_span: span,
757                kind: CallKind::Normal { desugaring: None, .. },
758                ..
759            } if span
760                .overlaps(self.body.local_decls[borrow.assigned_place.local].source_info.span) =>
761            {
762                if let TerminatorKind::Call { func, call_source: CallSource::Normal, .. } =
763                    &self.body.basic_blocks[location.block].terminator().kind
764                {
765                    // Just point to the function, to reduce the chance of overlapping spans.
766                    let function_span = match func {
767                        Operand::Constant(c) => c.span,
768                        Operand::Copy(place) | Operand::Move(place) => {
769                            if let Some(l) = place.as_local() {
770                                let local_decl = &self.body.local_decls[l];
771                                if self.local_name(l).is_none() {
772                                    local_decl.source_info.span
773                                } else {
774                                    span
775                                }
776                            } else {
777                                span
778                            }
779                        }
780                    };
781                    (LaterUseKind::Call, function_span, None)
782                } else {
783                    (LaterUseKind::Other, span, None)
784                }
785            }
786            UseSpans::PatUse(span)
787            | UseSpans::OtherUse(span)
788            | UseSpans::FnSelfUse { var_span: span, .. } => {
789                let block = &self.body.basic_blocks[location.block];
790
791                let kind = if let Some(&Statement {
792                    kind: StatementKind::FakeRead(box (FakeReadCause::ForLet(_), place)),
793                    ..
794                }) = block.statements.get(location.statement_index)
795                {
796                    if let Some(l) = place.as_local()
797                        && let local_decl = &self.body.local_decls[l]
798                        && local_decl.ty.is_closure()
799                    {
800                        LaterUseKind::ClosureCapture
801                    } else {
802                        LaterUseKind::FakeLetRead
803                    }
804                } else if self.was_captured_by_trait_object(borrow) {
805                    LaterUseKind::TraitCapture
806                } else if location.statement_index == block.statements.len() {
807                    if let TerminatorKind::Call { func, call_source: CallSource::Normal, .. } =
808                        &block.terminator().kind
809                    {
810                        // Just point to the function, to reduce the chance of overlapping spans.
811                        let function_span = match func {
812                            Operand::Constant(c) => c.span,
813                            Operand::Copy(place) | Operand::Move(place) => {
814                                if let Some(l) = place.as_local() {
815                                    let local_decl = &self.body.local_decls[l];
816                                    if self.local_name(l).is_none() {
817                                        local_decl.source_info.span
818                                    } else {
819                                        span
820                                    }
821                                } else {
822                                    span
823                                }
824                            }
825                        };
826                        return (LaterUseKind::Call, function_span, None);
827                    } else {
828                        LaterUseKind::Other
829                    }
830                } else {
831                    LaterUseKind::Other
832                };
833
834                (kind, span, None)
835            }
836        }
837    }
838
839    /// Checks if a borrowed value was captured by a trait object. We do this by
840    /// looking forward in the MIR from the reserve location and checking if we see
841    /// an unsized cast to a trait object on our data.
842    fn was_captured_by_trait_object(&self, borrow: &BorrowData<'tcx>) -> bool {
843        // Start at the reserve location, find the place that we want to see cast to a trait object.
844        let location = borrow.reserve_location;
845        let block = &self.body[location.block];
846        let stmt = block.statements.get(location.statement_index);
847        debug!("was_captured_by_trait_object: location={:?} stmt={:?}", location, stmt);
848
849        // We make a `queue` vector that has the locations we want to visit. As of writing, this
850        // will only ever have one item at any given time, but by using a vector, we can pop from
851        // it which simplifies the termination logic.
852        let mut queue = vec![location];
853        let Some(Statement { kind: StatementKind::Assign(box (place, _)), .. }) = stmt else {
854            return false;
855        };
856        let Some(mut target) = place.as_local() else { return false };
857
858        debug!("was_captured_by_trait: target={:?} queue={:?}", target, queue);
859        while let Some(current_location) = queue.pop() {
860            debug!("was_captured_by_trait: target={:?}", target);
861            let block = &self.body[current_location.block];
862            // We need to check the current location to find out if it is a terminator.
863            let is_terminator = current_location.statement_index == block.statements.len();
864            if !is_terminator {
865                let stmt = &block.statements[current_location.statement_index];
866                debug!("was_captured_by_trait_object: stmt={:?}", stmt);
867
868                // The only kind of statement that we care about is assignments...
869                if let StatementKind::Assign(box (place, rvalue)) = &stmt.kind {
870                    let Some(into) = place.local_or_deref_local() else {
871                        // Continue at the next location.
872                        queue.push(current_location.successor_within_block());
873                        continue;
874                    };
875
876                    match rvalue {
877                        // If we see a use, we should check whether it is our data, and if so
878                        // update the place that we're looking for to that new place.
879                        Rvalue::Use(operand) => match operand {
880                            Operand::Copy(place) | Operand::Move(place) => {
881                                if let Some(from) = place.as_local() {
882                                    if from == target {
883                                        target = into;
884                                    }
885                                }
886                            }
887                            _ => {}
888                        },
889                        // If we see an unsized cast, then if it is our data we should check
890                        // whether it is being cast to a trait object.
891                        Rvalue::Cast(
892                            CastKind::PointerCoercion(PointerCoercion::Unsize, _),
893                            operand,
894                            ty,
895                        ) => {
896                            match operand {
897                                Operand::Copy(place) | Operand::Move(place) => {
898                                    if let Some(from) = place.as_local() {
899                                        if from == target {
900                                            debug!("was_captured_by_trait_object: ty={:?}", ty);
901                                            // Check the type for a trait object.
902                                            return match ty.kind() {
903                                                // `&dyn Trait`
904                                                ty::Ref(_, ty, _) if ty.is_trait() => true,
905                                                // `Box<dyn Trait>`
906                                                _ if ty.boxed_ty().is_some_and(Ty::is_trait) => {
907                                                    true
908                                                }
909
910                                                // `dyn Trait`
911                                                _ if ty.is_trait() => true,
912                                                // Anything else.
913                                                _ => false,
914                                            };
915                                        }
916                                    }
917                                    return false;
918                                }
919                                _ => return false,
920                            }
921                        }
922                        _ => {}
923                    }
924                }
925
926                // Continue at the next location.
927                queue.push(current_location.successor_within_block());
928            } else {
929                // The only thing we need to do for terminators is progress to the next block.
930                let terminator = block.terminator();
931                debug!("was_captured_by_trait_object: terminator={:?}", terminator);
932
933                if let TerminatorKind::Call { destination, target: Some(block), args, .. } =
934                    &terminator.kind
935                    && let Some(dest) = destination.as_local()
936                {
937                    debug!(
938                        "was_captured_by_trait_object: target={:?} dest={:?} args={:?}",
939                        target, dest, args
940                    );
941                    // Check if one of the arguments to this function is the target place.
942                    let found_target = args.iter().any(|arg| {
943                        if let Operand::Move(place) = arg.node {
944                            if let Some(potential) = place.as_local() {
945                                potential == target
946                            } else {
947                                false
948                            }
949                        } else {
950                            false
951                        }
952                    });
953
954                    // If it is, follow this to the next block and update the target.
955                    if found_target {
956                        target = dest;
957                        queue.push(block.start_location());
958                    }
959                }
960            }
961
962            debug!("was_captured_by_trait: queue={:?}", queue);
963        }
964
965        // We didn't find anything and ran out of locations to check.
966        false
967    }
968}