]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Rollup merge of #106640 - lcnr:update-test, r=jackh726
[rust.git] / compiler / rustc_borrowck / src / diagnostics / explain_borrow.rs
index 00f5e8a83972f9c6456c51635867e2a7bfa59227..2095747097b7660870ecdff8b32fc8820525652b 100644 (file)
@@ -77,7 +77,7 @@ pub(crate) fn add_explanation_to_diagnostic(
                     if borrow_span.map(|sp| !sp.overlaps(var_or_use_span)).unwrap_or(true) {
                         err.span_label(
                             var_or_use_span,
-                            format!("{}borrow later {}", borrow_desc, message),
+                            format!("{borrow_desc}borrow later {message}"),
                         );
                     }
                 } else {
@@ -90,7 +90,7 @@ pub(crate) fn add_explanation_to_diagnostic(
                         let capture_kind_label = message;
                         err.span_label(
                             var_or_use_span,
-                            format!("{}borrow later {}", borrow_desc, capture_kind_label),
+                            format!("{borrow_desc}borrow later {capture_kind_label}"),
                         );
                         err.span_label(path_span, path_label);
                     }
@@ -110,7 +110,7 @@ pub(crate) fn add_explanation_to_diagnostic(
                 };
                 // We can use `var_or_use_span` if either `path_span` is not present, or both spans are the same
                 if path_span.map(|path_span| path_span == var_or_use_span).unwrap_or(true) {
-                    err.span_label(var_or_use_span, format!("{}{}", borrow_desc, message));
+                    err.span_label(var_or_use_span, format!("{borrow_desc}{message}"));
                 } else {
                     // path_span must be `Some` as otherwise the if condition is true
                     let path_span = path_span.unwrap();
@@ -121,7 +121,7 @@ pub(crate) fn add_explanation_to_diagnostic(
                         let capture_kind_label = message;
                         err.span_label(
                             var_or_use_span,
-                            format!("{}borrow later {}", borrow_desc, capture_kind_label),
+                            format!("{borrow_desc}borrow later {capture_kind_label}"),
                         );
                         err.span_label(path_span, path_label);
                     }
@@ -160,12 +160,8 @@ pub(crate) fn add_explanation_to_diagnostic(
                 match local_names[dropped_local] {
                     Some(local_name) if !local_decl.from_compiler_desugaring() => {
                         let message = format!(
-                            "{B}borrow might be used here, when `{LOC}` is dropped \
-                             and runs the {DTOR} for {TYPE}",
-                            B = borrow_desc,
-                            LOC = local_name,
-                            TYPE = type_desc,
-                            DTOR = dtor_desc
+                            "{borrow_desc}borrow might be used here, when `{local_name}` is dropped \
+                             and runs the {dtor_desc} for {type_desc}",
                         );
                         err.span_label(body.source_info(drop_loc).span, message);
 
@@ -180,18 +176,14 @@ pub(crate) fn add_explanation_to_diagnostic(
                         err.span_label(
                             local_decl.source_info.span,
                             format!(
-                                "a temporary with access to the {B}borrow \
+                                "a temporary with access to the {borrow_desc}borrow \
                                  is created here ...",
-                                B = borrow_desc
                             ),
                         );
                         let message = format!(
-                            "... and the {B}borrow might be used here, \
+                            "... and the {borrow_desc}borrow might be used here, \
                              when that temporary is dropped \
-                             and runs the {DTOR} for {TYPE}",
-                            B = borrow_desc,
-                            TYPE = type_desc,
-                            DTOR = dtor_desc
+                             and runs the {dtor_desc} for {type_desc}",
                         );
                         err.span_label(body.source_info(drop_loc).span, message);
 
@@ -249,20 +241,16 @@ pub(crate) fn add_explanation_to_diagnostic(
                     err.span_label(
                         span,
                         format!(
-                            "{}requires that `{}` is borrowed for `{}`",
+                            "{}requires that `{desc}` is borrowed for `{region_name}`",
                             category.description(),
-                            desc,
-                            region_name,
                         ),
                     );
                 } else {
                     err.span_label(
                         span,
                         format!(
-                            "{}requires that {}borrow lasts for `{}`",
+                            "{}requires that {borrow_desc}borrow lasts for `{region_name}`",
                             category.description(),
-                            borrow_desc,
-                            region_name,
                         ),
                     );
                 };
@@ -296,15 +284,14 @@ fn add_lifetime_bound_suggestion_to_diagnostic(
                 if region_name.was_named() { region_name.name } else { kw::UnderscoreLifetime };
 
             let msg = format!(
-                "you can add a bound to the {}to make it last less than `'static` and match `{}`",
+                "you can add a bound to the {}to make it last less than `'static` and match `{region_name}`",
                 category.description(),
-                region_name,
             );
 
             err.span_suggestion_verbose(
                 span.shrink_to_hi(),
                 &msg,
-                format!(" + {}", suggestable_name),
+                format!(" + {suggestable_name}"),
                 Applicability::Unspecified,
             );
         }
@@ -444,6 +431,7 @@ pub(crate) fn explain_why_borrow_contains_point(
     /// First span returned points to the location of the conflicting use
     /// Second span if `Some` is returned in the case of closures and points
     /// to the use of the path
+    #[instrument(level = "debug", skip(self))]
     fn later_use_kind(
         &self,
         borrow: &BorrowData<'tcx>,
@@ -461,11 +449,18 @@ fn later_use_kind(
                 let block = &self.body.basic_blocks[location.block];
 
                 let kind = if let Some(&Statement {
-                    kind: StatementKind::FakeRead(box (FakeReadCause::ForLet(_), _)),
+                    kind: StatementKind::FakeRead(box (FakeReadCause::ForLet(_), place)),
                     ..
                 }) = block.statements.get(location.statement_index)
                 {
-                    LaterUseKind::FakeLetRead
+                    if let Some(l) = place.as_local()
+                        && let local_decl = &self.body.local_decls[l]
+                        && local_decl.ty.is_closure()
+                    {
+                        LaterUseKind::ClosureCapture
+                    } else {
+                        LaterUseKind::FakeLetRead
+                    }
                 } else if self.was_captured_by_trait_object(borrow) {
                     LaterUseKind::TraitCapture
                 } else if location.statement_index == block.statements.len() {