about summary refs log tree commit diff
path: root/compiler/rustc_infer/src
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2024-07-11 14:14:17 -0400
committerMichael Goulet <michael@errs.io>2024-07-11 14:14:17 -0400
commit42653c0045e46c26d2468b2aa2bba97802c08795 (patch)
treef4da594fa595a696b9f0636266d34ce980f096d1 /compiler/rustc_infer/src
parent03bee1e1e56b10a3bff0e8eb524faacdb745cabc (diff)
downloadrust-42653c0045e46c26d2468b2aa2bba97802c08795.tar.gz
rust-42653c0045e46c26d2468b2aa2bba97802c08795.zip
Make it translatable too
Diffstat (limited to 'compiler/rustc_infer/src')
-rw-r--r--compiler/rustc_infer/src/errors/mod.rs29
-rw-r--r--compiler/rustc_infer/src/infer/error_reporting/region.rs23
2 files changed, 39 insertions, 13 deletions
diff --git a/compiler/rustc_infer/src/errors/mod.rs b/compiler/rustc_infer/src/errors/mod.rs
index a801001eaf9..ce1b0f86d03 100644
--- a/compiler/rustc_infer/src/errors/mod.rs
+++ b/compiler/rustc_infer/src/errors/mod.rs
@@ -1581,3 +1581,32 @@ pub enum ObligationCauseFailureCode {
         subdiags: Vec<TypeErrorAdditionalDiags>,
     },
 }
+
+#[derive(Subdiagnostic)]
+pub enum AddPreciseCapturing {
+    #[suggestion(
+        infer_precise_capturing_new,
+        style = "verbose",
+        code = " + use<{concatenated_bounds}>",
+        applicability = "machine-applicable"
+    )]
+    New {
+        #[primary_span]
+        span: Span,
+        new_lifetime: Symbol,
+        concatenated_bounds: String,
+    },
+    #[suggestion(
+        infer_precise_capturing_existing,
+        style = "verbose",
+        code = "{pre}{new_lifetime}{post}",
+        applicability = "machine-applicable"
+    )]
+    Existing {
+        #[primary_span]
+        span: Span,
+        new_lifetime: Symbol,
+        pre: &'static str,
+        post: &'static str,
+    },
+}
diff --git a/compiler/rustc_infer/src/infer/error_reporting/region.rs b/compiler/rustc_infer/src/infer/error_reporting/region.rs
index 793e0c70d3c..191cb23184d 100644
--- a/compiler/rustc_infer/src/infer/error_reporting/region.rs
+++ b/compiler/rustc_infer/src/infer/error_reporting/region.rs
@@ -1288,20 +1288,18 @@ fn suggest_precise_capturing<'tcx>(
             _ => None,
         });
 
-        let (insertion_span, pre, post) = if let Some(last_lifetime_span) = last_lifetime_span {
+        let (span, pre, post) = if let Some(last_lifetime_span) = last_lifetime_span {
             (last_lifetime_span.shrink_to_hi(), ", ", "")
         } else if let Some(first_param_span) = first_param_span {
             (first_param_span.shrink_to_lo(), "", ", ")
         } else {
+            // If we have no args, then have `use<>` and need to fall back to using
+            // span math. This sucks, but should be reliable due to the construction
+            // of the `use<>` span.
             (span.with_hi(span.hi() - BytePos(1)).shrink_to_hi(), "", "")
         };
 
-        diag.span_suggestion_verbose(
-            insertion_span,
-            format!("add `{new_lifetime}` to the `use<...>` bound to explicitly capture it",),
-            format!("{pre}{new_lifetime}{post}"),
-            Applicability::MachineApplicable,
-        );
+        diag.subdiagnostic(errors::AddPreciseCapturing::Existing { span, new_lifetime, pre, post });
     } else {
         let mut captured_lifetimes = FxIndexSet::default();
         let mut captured_non_lifetimes = FxIndexSet::default();
@@ -1349,11 +1347,10 @@ fn suggest_precise_capturing<'tcx>(
             .collect::<Vec<_>>()
             .join(", ");
 
-        diag.span_suggestion_verbose(
-            tcx.def_span(opaque_def_id).shrink_to_hi(),
-            format!("add a `use<...>` bound to explicitly capture `{new_lifetime}`",),
-            format!(" + use<{concatenated_bounds}>"),
-            Applicability::MachineApplicable,
-        );
+        diag.subdiagnostic(errors::AddPreciseCapturing::New {
+            span: tcx.def_span(opaque_def_id).shrink_to_hi(),
+            new_lifetime,
+            concatenated_bounds,
+        });
     }
 }