about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorNikita Tomashevich <quant3234@gmail.com>2022-09-08 22:28:00 +0300
committerNikita Tomashevich <quant3234@gmail.com>2022-12-28 14:53:47 +0300
commit8360a40a8a2ff2098eed561dd89c577349dbbb76 (patch)
tree06112d8f43aec7b1def49a9f5797d7b5049ff3d2 /compiler
parent9b889e53e78a186a861a8407c225f9d8e0d436f5 (diff)
downloadrust-8360a40a8a2ff2098eed561dd89c577349dbbb76.tar.gz
rust-8360a40a8a2ff2098eed561dd89c577349dbbb76.zip
Migrate named_anon_conflict.rs
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_error_messages/locales/en-US/infer.ftl11
-rw-r--r--compiler/rustc_infer/src/errors/mod.rs24
-rw-r--r--compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs38
3 files changed, 47 insertions, 26 deletions
diff --git a/compiler/rustc_error_messages/locales/en-US/infer.ftl b/compiler/rustc_error_messages/locales/en-US/infer.ftl
index c9d83746d54..40ca2c1cd16 100644
--- a/compiler/rustc_error_messages/locales/en-US/infer.ftl
+++ b/compiler/rustc_error_messages/locales/en-US/infer.ftl
@@ -172,3 +172,14 @@ infer_msl_unmet_req = because this has an unmet lifetime requirement
 infer_msl_trait_note = this has an implicit `'static` lifetime requirement
 infer_msl_trait_sugg = consider relaxing the implicit `'static` requirement
 infer_suggest_add_let_for_letchains = consider adding `let`
+
+infer_explicit_lifetime_required = explicit lifetime required in {$ident_kind ->
+    [ident] the type of `{$simple_ident}`
+    *[param_type] parameter type
+}
+    .label = lifetime `{$named}` required
+
+infer_explicit_lifetime_required_sugg = add explicit lifetime `{$named}` to {$ident_kind ->
+    [ident] the type of `{$simple_ident}`
+    *[param_type] type
+}
diff --git a/compiler/rustc_infer/src/errors/mod.rs b/compiler/rustc_infer/src/errors/mod.rs
index c4f11472d55..99112397c7f 100644
--- a/compiler/rustc_infer/src/errors/mod.rs
+++ b/compiler/rustc_infer/src/errors/mod.rs
@@ -357,8 +357,8 @@ impl AddToDiagnostic for LifetimeMismatchLabels {
 pub struct AddLifetimeParamsSuggestion<'a> {
     pub tcx: TyCtxt<'a>,
     pub sub: Region<'a>,
-    pub ty_sup: &'a Ty<'a>,
-    pub ty_sub: &'a Ty<'a>,
+    pub ty_sup: &'a hir::Ty<'a>,
+    pub ty_sub: &'a hir::Ty<'a>,
     pub add_note: bool,
 }
 
@@ -520,3 +520,23 @@ pub struct MismatchedStaticLifetime<'a> {
     #[subdiagnostic]
     pub implicit_static_lifetimes: Vec<ImplicitStaticLifetimeSubdiag>,
 }
+
+#[derive(SessionDiagnostic)]
+#[diag(infer::explicit_lifetime_required, code = "E0621")]
+pub struct ExplicitLifetimeRequired<'a> {
+    #[primary_span]
+    #[label]
+    pub span: Span,
+    pub ident_kind: &'static str,
+    pub simple_ident: String,
+    pub named: String,
+
+    #[suggestion(
+        infer::explicit_lifetime_required_sugg,
+        code = "{new_ty}",
+        applicability = "unspecified"
+    )]
+    pub new_ty_span: Span,
+    #[skip_arg]
+    pub new_ty: Ty<'a>,
+}
diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs
index 3fe7c1598fc..d7751158902 100644
--- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs
+++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs
@@ -1,8 +1,11 @@
 //! Error Reporting for Anonymous Region Lifetime Errors
 //! where one region is named and the other is anonymous.
-use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
 use crate::infer::error_reporting::nice_region_error::NiceRegionError;
-use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
+use crate::{
+    errors::ExplicitLifetimeRequired,
+    infer::error_reporting::nice_region_error::find_anon_type::find_anon_type,
+};
+use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
 use rustc_middle::ty;
 use rustc_span::symbol::kw;
 
@@ -87,30 +90,17 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
             return None;
         }
 
-        let (error_var, span_label_var) = match param.pat.simple_ident() {
-            Some(simple_ident) => (
-                format!("the type of `{}`", simple_ident),
-                format!("the type of `{}`", simple_ident),
-            ),
-            None => ("parameter type".to_owned(), "type".to_owned()),
+        let simple_ident = param.pat.simple_ident();
+        let (ident_kind, simple_ident) = match simple_ident {
+            Some(ident) => ("ident", ident.to_string()),
+            None => ("param_type", String::new()),
         };
 
-        let mut diag = struct_span_err!(
-            self.tcx().sess,
-            span,
-            E0621,
-            "explicit lifetime required in {}",
-            error_var
-        );
-
-        diag.span_label(span, format!("lifetime `{}` required", named));
-        diag.span_suggestion(
-            new_ty_span,
-            &format!("add explicit lifetime `{}` to {}", named, span_label_var),
-            new_ty,
-            Applicability::Unspecified,
-        );
+        let named = named.to_string();
 
-        Some(diag)
+        let err =
+            ExplicitLifetimeRequired { span, ident_kind, simple_ident, named, new_ty_span, new_ty };
+        let err = self.tcx().sess.parse_sess.create_err(err);
+        Some(err)
     }
 }