about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNikita Tomashevich <quant3234@gmail.com>2023-01-21 18:16:53 +0300
committerIQuant <quant3234@gmail.com>2023-02-14 18:31:45 +0300
commitcb8ea01096fb14ea25bbe69fd0b92f7e7752cb78 (patch)
tree78b684ad6694dc739921631086363e54c946d309
parent58e901b6fd1163172149fd422565523b17eed5f0 (diff)
downloadrust-cb8ea01096fb14ea25bbe69fd0b92f7e7752cb78.tar.gz
rust-cb8ea01096fb14ea25bbe69fd0b92f7e7752cb78.zip
Port RefLongerThanData
-rw-r--r--compiler/rustc_error_messages/locales/en-US/infer.ftl3
-rw-r--r--compiler/rustc_infer/src/errors/mod.rs10
-rw-r--r--compiler/rustc_infer/src/errors/note_and_explain.rs4
-rw-r--r--compiler/rustc_infer/src/infer/error_reporting/note.rs30
4 files changed, 30 insertions, 17 deletions
diff --git a/compiler/rustc_error_messages/locales/en-US/infer.ftl b/compiler/rustc_error_messages/locales/en-US/infer.ftl
index 0fcde811740..c012973f1dd 100644
--- a/compiler/rustc_error_messages/locales/en-US/infer.ftl
+++ b/compiler/rustc_error_messages/locales/en-US/infer.ftl
@@ -150,6 +150,8 @@ infer_region_explanation = {$pref_kind ->
     [lf_must_outlive] but lifetime parameter must outlive
     [type_valid_for] the type is valid for
     [borrow_lasts_for] but the borrow lasts for
+    [pointer_valid_for] the pointer is valid for
+    [data_valid_for] but the referenced data is only valid for
     [empty] {""}
 }{$pref_kind ->
     [empty] {""}
@@ -175,6 +177,7 @@ infer_outlives_bound = lifetime of the source pointer does not outlive lifetime
 infer_fullfill_req_lifetime = the type `{$ty}` does not fulfill the required lifetime
 infer_lf_bound_not_satisfied = lifetime bound not satisfied
 infer_borrowed_too_long = a value of type `{$ty}` is borrowed for too long
+infer_ref_longer_than_data = in type `{$ty}`, reference has a longer lifetime than the data it references
 
 infer_mismatched_static_lifetime = incompatible lifetime on type
 infer_does_not_outlive_static_from_impl = ...does not necessarily outlive the static lifetime introduced by the compatible `impl`
diff --git a/compiler/rustc_infer/src/errors/mod.rs b/compiler/rustc_infer/src/errors/mod.rs
index 6efe72bfc36..7088be05ef7 100644
--- a/compiler/rustc_infer/src/errors/mod.rs
+++ b/compiler/rustc_infer/src/errors/mod.rs
@@ -980,3 +980,13 @@ pub struct BorrowedTooLong<'a> {
     #[subdiagnostic]
     pub notes: Vec<note_and_explain::RegionExplanation<'a>>,
 }
+
+#[derive(Diagnostic)]
+#[diag(infer_ref_longer_than_data, code = "E0491")]
+pub struct RefLongerThanData<'a> {
+    #[primary_span]
+    pub span: Span,
+    pub ty: Ty<'a>,
+    #[subdiagnostic]
+    pub notes: Vec<note_and_explain::RegionExplanation<'a>>,
+}
diff --git a/compiler/rustc_infer/src/errors/note_and_explain.rs b/compiler/rustc_infer/src/errors/note_and_explain.rs
index e779fdd6e55..3516517dcc3 100644
--- a/compiler/rustc_infer/src/errors/note_and_explain.rs
+++ b/compiler/rustc_infer/src/errors/note_and_explain.rs
@@ -131,6 +131,8 @@ pub enum PrefixKind {
     LfMustOutlive,
     TypeValidFor,
     BorrowLastsFor,
+    PointerValidFor,
+    DataValidFor,
 }
 
 pub enum SuffixKind {
@@ -153,6 +155,8 @@ impl IntoDiagnosticArg for PrefixKind {
             Self::LfMustOutlive => "lf_must_outlive",
             Self::TypeValidFor => "type_valid_for",
             Self::BorrowLastsFor => "borrow_lasts_for",
+            Self::PointerValidFor => "pointer_valid_for",
+            Self::DataValidFor => "data_valid_for",
         }
         .into();
         rustc_errors::DiagnosticArgValue::Str(kind)
diff --git a/compiler/rustc_infer/src/infer/error_reporting/note.rs b/compiler/rustc_infer/src/infer/error_reporting/note.rs
index e470d9d9053..4c07cc0b6a2 100644
--- a/compiler/rustc_infer/src/infer/error_reporting/note.rs
+++ b/compiler/rustc_infer/src/infer/error_reporting/note.rs
@@ -1,6 +1,6 @@
 use crate::errors::{
     note_and_explain, BorrowedTooLong, FullfillReqLifetime, LfBoundNotSatisfied, OutlivesBound,
-    OutlivesContent, RegionOriginNote,
+    OutlivesContent, RefLongerThanData, RegionOriginNote,
 };
 use crate::infer::error_reporting::{note_and_explain_region, TypeErrCtxt};
 use crate::infer::{self, SubregionOrigin};
@@ -223,30 +223,26 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
                 .into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic)
             }
             infer::ReferenceOutlivesReferent(ty, span) => {
-                let mut err = struct_span_err!(
-                    self.tcx.sess,
-                    span,
-                    E0491,
-                    "in type `{}`, reference has a longer lifetime than the data it references",
-                    self.ty_to_string(ty)
-                );
-                note_and_explain_region(
+                let pointer_valid = note_and_explain::RegionExplanation::new(
                     self.tcx,
-                    &mut err,
-                    "the pointer is valid for ",
                     sub,
-                    "",
                     None,
+                    note_and_explain::PrefixKind::PointerValidFor,
+                    note_and_explain::SuffixKind::Empty,
                 );
-                note_and_explain_region(
+                let data_valid = note_and_explain::RegionExplanation::new(
                     self.tcx,
-                    &mut err,
-                    "but the referenced data is only valid for ",
                     sup,
-                    "",
                     None,
+                    note_and_explain::PrefixKind::DataValidFor,
+                    note_and_explain::SuffixKind::Empty,
                 );
-                err
+                RefLongerThanData {
+                    span,
+                    ty: self.resolve_vars_if_possible(ty),
+                    notes: pointer_valid.into_iter().chain(data_valid).collect(),
+                }
+                .into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic)
             }
             infer::CompareImplItemObligation { span, impl_item_def_id, trait_item_def_id } => {
                 let mut err = self.report_extra_impl_obligation(