Skip to content

Commit 58e901b

Browse files
committed
Port "BorrowedTooLong" diagnostic
1 parent 8fc5ba6 commit 58e901b

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ infer_region_explanation = {$pref_kind ->
142142
*[should_not_happen] [{$pref_kind}]
143143
[ref_valid_for] ...the reference is valid for
144144
[content_valid_for] ...but the borrowed content is only valid for
145-
[type_valid_for] object type is valid for
145+
[type_obj_valid_for] object type is valid for
146146
[source_pointer_valid_for] source pointer is only valid for
147147
[type_satisfy] type must satisfy
148148
[type_outlive] type must outlive
149149
[lf_instantiated_with] lifetime parameter instantiated with
150150
[lf_must_outlive] but lifetime parameter must outlive
151+
[type_valid_for] the type is valid for
152+
[borrow_lasts_for] but the borrow lasts for
151153
[empty] {""}
152154
}{$pref_kind ->
153155
[empty] {""}
@@ -156,7 +158,6 @@ infer_region_explanation = {$pref_kind ->
156158
*[should_not_happen] [{$desc_kind}]
157159
[restatic] the static lifetime
158160
[revar] lifetime {$desc_arg}
159-
160161
[as_defined] the lifetime `{$desc_arg}` as defined here
161162
[as_defined_anon] the anonymous lifetime as defined here
162163
[defined_here] the anonymous lifetime defined here
@@ -173,6 +174,7 @@ infer_outlives_content = lifetime of reference outlives lifetime of borrowed con
173174
infer_outlives_bound = lifetime of the source pointer does not outlive lifetime bound of the object type
174175
infer_fullfill_req_lifetime = the type `{$ty}` does not fulfill the required lifetime
175176
infer_lf_bound_not_satisfied = lifetime bound not satisfied
177+
infer_borrowed_too_long = a value of type `{$ty}` is borrowed for too long
176178
177179
infer_mismatched_static_lifetime = incompatible lifetime on type
178180
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
@@ -970,3 +970,13 @@ pub struct LfBoundNotSatisfied<'a> {
970970
#[subdiagnostic]
971971
pub notes: Vec<note_and_explain::RegionExplanation<'a>>,
972972
}
973+
974+
#[derive(Diagnostic)]
975+
#[diag(infer_borrowed_too_long, code = "E0490")]
976+
pub struct BorrowedTooLong<'a> {
977+
#[primary_span]
978+
pub span: Span,
979+
pub ty: Ty<'a>,
980+
#[subdiagnostic]
981+
pub notes: Vec<note_and_explain::RegionExplanation<'a>>,
982+
}

compiler/rustc_infer/src/errors/note_and_explain.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,14 @@ pub enum PrefixKind {
123123
Empty,
124124
RefValidFor,
125125
ContentValidFor,
126-
TypeValidFor,
126+
TypeObjValidFor,
127127
SourcePointerValidFor,
128128
TypeSatisfy,
129129
TypeOutlive,
130130
LfInstantiatedWith,
131131
LfMustOutlive,
132+
TypeValidFor,
133+
BorrowLastsFor,
132134
}
133135

134136
pub enum SuffixKind {
@@ -143,12 +145,14 @@ impl IntoDiagnosticArg for PrefixKind {
143145
Self::Empty => "empty",
144146
Self::RefValidFor => "ref_valid_for",
145147
Self::ContentValidFor => "content_valid_for",
146-
Self::TypeValidFor => "type_valid_for",
148+
Self::TypeObjValidFor => "type_obj_valid_for",
147149
Self::SourcePointerValidFor => "source_pointer_valid_for",
148150
Self::TypeSatisfy => "type_satisfy",
149151
Self::TypeOutlive => "type_outlive",
150152
Self::LfInstantiatedWith => "lf_instantiated_with",
151153
Self::LfMustOutlive => "lf_must_outlive",
154+
Self::TypeValidFor => "type_valid_for",
155+
Self::BorrowLastsFor => "borrow_lasts_for",
152156
}
153157
.into();
154158
rustc_errors::DiagnosticArgValue::Str(kind)

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::errors::{
2-
note_and_explain, FullfillReqLifetime, LfBoundNotSatisfied, OutlivesBound, OutlivesContent,
3-
RegionOriginNote,
2+
note_and_explain, BorrowedTooLong, FullfillReqLifetime, LfBoundNotSatisfied, OutlivesBound,
3+
OutlivesContent, RegionOriginNote,
44
};
55
use crate::infer::error_reporting::{note_and_explain_region, TypeErrCtxt};
66
use crate::infer::{self, SubregionOrigin};
@@ -147,7 +147,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
147147
self.tcx,
148148
sub,
149149
None,
150-
note_and_explain::PrefixKind::TypeValidFor,
150+
note_and_explain::PrefixKind::TypeObjValidFor,
151151
note_and_explain::SuffixKind::Empty,
152152
);
153153
let pointer_valid = note_and_explain::RegionExplanation::new(
@@ -200,6 +200,28 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
200200
}
201201
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic)
202202
}
203+
infer::DataBorrowed(ty, span) => {
204+
let type_valid = note_and_explain::RegionExplanation::new(
205+
self.tcx,
206+
sub,
207+
None,
208+
note_and_explain::PrefixKind::TypeValidFor,
209+
note_and_explain::SuffixKind::Empty,
210+
);
211+
let borrow_lasts_for = note_and_explain::RegionExplanation::new(
212+
self.tcx,
213+
sup,
214+
None,
215+
note_and_explain::PrefixKind::BorrowLastsFor,
216+
note_and_explain::SuffixKind::Empty,
217+
);
218+
BorrowedTooLong {
219+
span,
220+
ty: self.resolve_vars_if_possible(ty),
221+
notes: type_valid.into_iter().chain(borrow_lasts_for).collect(),
222+
}
223+
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic)
224+
}
203225
infer::ReferenceOutlivesReferent(ty, span) => {
204226
let mut err = struct_span_err!(
205227
self.tcx.sess,

0 commit comments

Comments
 (0)