Skip to content

Commit cb8ea01

Browse files
committed
Port RefLongerThanData
1 parent 58e901b commit cb8ea01

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

compiler/rustc_error_messages/locales/en-US/infer.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ infer_region_explanation = {$pref_kind ->
150150
[lf_must_outlive] but lifetime parameter must outlive
151151
[type_valid_for] the type is valid for
152152
[borrow_lasts_for] but the borrow lasts for
153+
[pointer_valid_for] the pointer is valid for
154+
[data_valid_for] but the referenced data is only valid for
153155
[empty] {""}
154156
}{$pref_kind ->
155157
[empty] {""}
@@ -175,6 +177,7 @@ infer_outlives_bound = lifetime of the source pointer does not outlive lifetime
175177
infer_fullfill_req_lifetime = the type `{$ty}` does not fulfill the required lifetime
176178
infer_lf_bound_not_satisfied = lifetime bound not satisfied
177179
infer_borrowed_too_long = a value of type `{$ty}` is borrowed for too long
180+
infer_ref_longer_than_data = in type `{$ty}`, reference has a longer lifetime than the data it references
178181
179182
infer_mismatched_static_lifetime = incompatible lifetime on type
180183
infer_does_not_outlive_static_from_impl = ...does not necessarily outlive the static lifetime introduced by the compatible `impl`

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,3 +980,13 @@ pub struct BorrowedTooLong<'a> {
980980
#[subdiagnostic]
981981
pub notes: Vec<note_and_explain::RegionExplanation<'a>>,
982982
}
983+
984+
#[derive(Diagnostic)]
985+
#[diag(infer_ref_longer_than_data, code = "E0491")]
986+
pub struct RefLongerThanData<'a> {
987+
#[primary_span]
988+
pub span: Span,
989+
pub ty: Ty<'a>,
990+
#[subdiagnostic]
991+
pub notes: Vec<note_and_explain::RegionExplanation<'a>>,
992+
}

compiler/rustc_infer/src/errors/note_and_explain.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ pub enum PrefixKind {
131131
LfMustOutlive,
132132
TypeValidFor,
133133
BorrowLastsFor,
134+
PointerValidFor,
135+
DataValidFor,
134136
}
135137

136138
pub enum SuffixKind {
@@ -153,6 +155,8 @@ impl IntoDiagnosticArg for PrefixKind {
153155
Self::LfMustOutlive => "lf_must_outlive",
154156
Self::TypeValidFor => "type_valid_for",
155157
Self::BorrowLastsFor => "borrow_lasts_for",
158+
Self::PointerValidFor => "pointer_valid_for",
159+
Self::DataValidFor => "data_valid_for",
156160
}
157161
.into();
158162
rustc_errors::DiagnosticArgValue::Str(kind)

compiler/rustc_infer/src/infer/error_reporting/note.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::errors::{
22
note_and_explain, BorrowedTooLong, FullfillReqLifetime, LfBoundNotSatisfied, OutlivesBound,
3-
OutlivesContent, RegionOriginNote,
3+
OutlivesContent, RefLongerThanData, RegionOriginNote,
44
};
55
use crate::infer::error_reporting::{note_and_explain_region, TypeErrCtxt};
66
use crate::infer::{self, SubregionOrigin};
@@ -223,30 +223,26 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
223223
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic)
224224
}
225225
infer::ReferenceOutlivesReferent(ty, span) => {
226-
let mut err = struct_span_err!(
227-
self.tcx.sess,
228-
span,
229-
E0491,
230-
"in type `{}`, reference has a longer lifetime than the data it references",
231-
self.ty_to_string(ty)
232-
);
233-
note_and_explain_region(
226+
let pointer_valid = note_and_explain::RegionExplanation::new(
234227
self.tcx,
235-
&mut err,
236-
"the pointer is valid for ",
237228
sub,
238-
"",
239229
None,
230+
note_and_explain::PrefixKind::PointerValidFor,
231+
note_and_explain::SuffixKind::Empty,
240232
);
241-
note_and_explain_region(
233+
let data_valid = note_and_explain::RegionExplanation::new(
242234
self.tcx,
243-
&mut err,
244-
"but the referenced data is only valid for ",
245235
sup,
246-
"",
247236
None,
237+
note_and_explain::PrefixKind::DataValidFor,
238+
note_and_explain::SuffixKind::Empty,
248239
);
249-
err
240+
RefLongerThanData {
241+
span,
242+
ty: self.resolve_vars_if_possible(ty),
243+
notes: pointer_valid.into_iter().chain(data_valid).collect(),
244+
}
245+
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic)
250246
}
251247
infer::CompareImplItemObligation { span, impl_item_def_id, trait_item_def_id } => {
252248
let mut err = self.report_extra_impl_obligation(

0 commit comments

Comments
 (0)