Skip to content

Commit

Permalink
Port RefLongerThanData
Browse files Browse the repository at this point in the history
  • Loading branch information
IntQuant committed Feb 14, 2023
1 parent 58e901b commit cb8ea01
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/infer.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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] {""}
Expand All @@ -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`
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_infer/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
}
4 changes: 4 additions & 0 deletions compiler/rustc_infer/src/errors/note_and_explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ pub enum PrefixKind {
LfMustOutlive,
TypeValidFor,
BorrowLastsFor,
PointerValidFor,
DataValidFor,
}

pub enum SuffixKind {
Expand All @@ -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)
Expand Down
30 changes: 13 additions & 17 deletions compiler/rustc_infer/src/infer/error_reporting/note.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit cb8ea01

Please sign in to comment.