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