1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use infer
::{self, InferCtxt, SubregionOrigin}
;
13 use ty
::error
::TypeError
;
14 use errors
::DiagnosticBuilder
;
16 impl<'a
, 'gcx
, 'tcx
> InferCtxt
<'a
, 'gcx
, 'tcx
> {
17 pub(super) fn note_region_origin(&self,
18 err
: &mut DiagnosticBuilder
,
19 origin
: &SubregionOrigin
<'tcx
>) {
21 infer
::Subtype(ref trace
) => {
22 if let Some((expected
, found
)) = self.values_str(&trace
.values
) {
23 // FIXME: do we want a "the" here?
24 err
.span_note(trace
.cause
.span
,
25 &format
!("...so that {} (expected {}, found {})",
26 trace
.cause
.as_requirement_str(),
30 // FIXME: this really should be handled at some earlier stage. Our
31 // handling of region checking when type errors are present is
34 err
.span_note(trace
.cause
.span
,
35 &format
!("...so that {}", trace
.cause
.as_requirement_str()));
38 infer
::Reborrow(span
) => {
40 "...so that reference does not outlive borrowed content");
42 infer
::ReborrowUpvar(span
, ref upvar_id
) => {
44 &format
!("...so that closure can access `{}`",
46 .local_var_name_str(upvar_id
.var_id
)
49 infer
::InfStackClosure(span
) => {
50 err
.span_note(span
, "...so that closure does not outlive its stack frame");
52 infer
::InvokeClosure(span
) => {
54 "...so that closure is not invoked outside its lifetime");
56 infer
::DerefPointer(span
) => {
58 "...so that pointer is not dereferenced outside its lifetime");
60 infer
::FreeVariable(span
, id
) => {
62 &format
!("...so that captured variable `{}` does not outlive the \
64 self.tcx
.local_var_name_str(id
)));
66 infer
::IndexSlice(span
) => {
67 err
.span_note(span
, "...so that slice is not indexed outside the lifetime");
69 infer
::RelateObjectBound(span
) => {
70 err
.span_note(span
, "...so that it can be closed over into an object");
72 infer
::CallRcvr(span
) => {
74 "...so that method receiver is valid for the method call");
76 infer
::CallArg(span
) => {
77 err
.span_note(span
, "...so that argument is valid for the call");
79 infer
::CallReturn(span
) => {
80 err
.span_note(span
, "...so that return value is valid for the call");
82 infer
::Operand(span
) => {
83 err
.span_note(span
, "...so that operand is valid for operation");
85 infer
::AddrOf(span
) => {
86 err
.span_note(span
, "...so that reference is valid at the time of borrow");
88 infer
::AutoBorrow(span
) => {
90 "...so that auto-reference is valid at the time of borrow");
92 infer
::ExprTypeIsNotInScope(t
, span
) => {
94 &format
!("...so type `{}` of expression is valid during the \
96 self.ty_to_string(t
)));
98 infer
::BindingTypeIsNotValidAtDecl(span
) => {
100 "...so that variable is valid at time of its declaration");
102 infer
::ParameterInScope(_
, span
) => {
104 "...so that a type/lifetime parameter is in scope here");
106 infer
::DataBorrowed(ty
, span
) => {
108 &format
!("...so that the type `{}` is not borrowed for too long",
109 self.ty_to_string(ty
)));
111 infer
::ReferenceOutlivesReferent(ty
, span
) => {
113 &format
!("...so that the reference type `{}` does not outlive the \
115 self.ty_to_string(ty
)));
117 infer
::RelateParamBound(span
, t
) => {
119 &format
!("...so that the type `{}` will meet its required \
121 self.ty_to_string(t
)));
123 infer
::RelateDefaultParamBound(span
, t
) => {
125 &format
!("...so that type parameter instantiated with `{}`, will \
126 meet its declared lifetime bounds",
127 self.ty_to_string(t
)));
129 infer
::RelateRegionParamBound(span
) => {
131 "...so that the declared lifetime parameter bounds are satisfied");
133 infer
::SafeDestructor(span
) => {
135 "...so that references are valid when the destructor runs");
137 infer
::CompareImplMethodObligation { span, .. }
=> {
139 "...so that the definition in impl matches the definition from the \
145 pub(super) fn report_concrete_failure(&self,
146 origin
: SubregionOrigin
<'tcx
>,
149 -> DiagnosticBuilder
<'tcx
> {
151 infer
::Subtype(trace
) => {
152 let terr
= TypeError
::RegionsDoesNotOutlive(sup
, sub
);
153 self.report_and_explain_type_error(trace
, &terr
)
155 infer
::Reborrow(span
) => {
156 let mut err
= struct_span_err
!(self.tcx
.sess
,
159 "lifetime of reference outlives lifetime of \
160 borrowed content...");
161 self.tcx
.note_and_explain_region(&mut err
,
162 "...the reference is valid for ",
165 self.tcx
.note_and_explain_region(&mut err
,
166 "...but the borrowed content is only valid for ",
171 infer
::ReborrowUpvar(span
, ref upvar_id
) => {
172 let mut err
= struct_span_err
!(self.tcx
.sess
,
175 "lifetime of borrowed pointer outlives lifetime \
176 of captured variable `{}`...",
177 self.tcx
.local_var_name_str(upvar_id
.var_id
));
178 self.tcx
.note_and_explain_region(&mut err
,
179 "...the borrowed pointer is valid for ",
183 .note_and_explain_region(&mut err
,
184 &format
!("...but `{}` is only valid for ",
186 .local_var_name_str(upvar_id
.var_id
)),
191 infer
::InfStackClosure(span
) => {
193 struct_span_err
!(self.tcx
.sess
, span
, E0314
, "closure outlives stack frame");
194 self.tcx
.note_and_explain_region(&mut err
,
195 "...the closure must be valid for ",
198 self.tcx
.note_and_explain_region(&mut err
,
199 "...but the closure's stack frame is only valid \
205 infer
::InvokeClosure(span
) => {
206 let mut err
= struct_span_err
!(self.tcx
.sess
,
209 "cannot invoke closure outside of its lifetime");
211 .note_and_explain_region(&mut err
, "the closure is only valid for ", sup
, "");
214 infer
::DerefPointer(span
) => {
215 let mut err
= struct_span_err
!(self.tcx
.sess
,
218 "dereference of reference outside its lifetime");
220 .note_and_explain_region(&mut err
, "the reference is only valid for ", sup
, "");
223 infer
::FreeVariable(span
, id
) => {
224 let mut err
= struct_span_err
!(self.tcx
.sess
,
227 "captured variable `{}` does not outlive the \
229 self.tcx
.local_var_name_str(id
));
231 .note_and_explain_region(&mut err
, "captured variable is valid for ", sup
, "");
232 self.tcx
.note_and_explain_region(&mut err
, "closure is valid for ", sub
, "");
235 infer
::IndexSlice(span
) => {
236 let mut err
= struct_span_err
!(self.tcx
.sess
,
239 "index of slice outside its lifetime");
240 self.tcx
.note_and_explain_region(&mut err
, "the slice is only valid for ", sup
, "");
243 infer
::RelateObjectBound(span
) => {
244 let mut err
= struct_span_err
!(self.tcx
.sess
,
247 "lifetime of the source pointer does not outlive \
248 lifetime bound of the object type");
249 self.tcx
.note_and_explain_region(&mut err
, "object type is valid for ", sub
, "");
250 self.tcx
.note_and_explain_region(&mut err
,
251 "source pointer is only valid for ",
256 infer
::RelateParamBound(span
, ty
) => {
257 let mut err
= struct_span_err
!(self.tcx
.sess
,
260 "the type `{}` does not fulfill the required \
262 self.ty_to_string(ty
));
263 self.tcx
.note_and_explain_region(&mut err
, "type must outlive ", sub
, "");
266 infer
::RelateRegionParamBound(span
) => {
268 struct_span_err
!(self.tcx
.sess
, span
, E0478
, "lifetime bound not satisfied");
269 self.tcx
.note_and_explain_region(&mut err
,
270 "lifetime parameter instantiated with ",
273 self.tcx
.note_and_explain_region(&mut err
,
274 "but lifetime parameter must outlive ",
279 infer
::RelateDefaultParamBound(span
, ty
) => {
280 let mut err
= struct_span_err
!(self.tcx
.sess
,
283 "the type `{}` (provided as the value of a type \
284 parameter) is not valid at this point",
285 self.ty_to_string(ty
));
286 self.tcx
.note_and_explain_region(&mut err
, "type must outlive ", sub
, "");
289 infer
::CallRcvr(span
) => {
290 let mut err
= struct_span_err
!(self.tcx
.sess
,
293 "lifetime of method receiver does not outlive the \
296 .note_and_explain_region(&mut err
, "the receiver is only valid for ", sup
, "");
299 infer
::CallArg(span
) => {
300 let mut err
= struct_span_err
!(self.tcx
.sess
,
303 "lifetime of function argument does not outlive \
305 self.tcx
.note_and_explain_region(&mut err
,
306 "the function argument is only valid for ",
311 infer
::CallReturn(span
) => {
312 let mut err
= struct_span_err
!(self.tcx
.sess
,
315 "lifetime of return value does not outlive the \
317 self.tcx
.note_and_explain_region(&mut err
,
318 "the return value is only valid for ",
323 infer
::Operand(span
) => {
324 let mut err
= struct_span_err
!(self.tcx
.sess
,
327 "lifetime of operand does not outlive the \
330 .note_and_explain_region(&mut err
, "the operand is only valid for ", sup
, "");
333 infer
::AddrOf(span
) => {
334 let mut err
= struct_span_err
!(self.tcx
.sess
,
337 "reference is not valid at the time of borrow");
339 .note_and_explain_region(&mut err
, "the borrow is only valid for ", sup
, "");
342 infer
::AutoBorrow(span
) => {
343 let mut err
= struct_span_err
!(self.tcx
.sess
,
346 "automatically reference is not valid at the time \
348 self.tcx
.note_and_explain_region(&mut err
,
349 "the automatic borrow is only valid for ",
354 infer
::ExprTypeIsNotInScope(t
, span
) => {
355 let mut err
= struct_span_err
!(self.tcx
.sess
,
358 "type of expression contains references that are \
359 not valid during the expression: `{}`",
360 self.ty_to_string(t
));
361 self.tcx
.note_and_explain_region(&mut err
, "type is only valid for ", sup
, "");
364 infer
::SafeDestructor(span
) => {
365 let mut err
= struct_span_err
!(self.tcx
.sess
,
368 "unsafe use of destructor: destructor might be \
369 called while references are dead");
370 // FIXME (22171): terms "super/subregion" are suboptimal
371 self.tcx
.note_and_explain_region(&mut err
, "superregion: ", sup
, "");
372 self.tcx
.note_and_explain_region(&mut err
, "subregion: ", sub
, "");
375 infer
::BindingTypeIsNotValidAtDecl(span
) => {
376 let mut err
= struct_span_err
!(self.tcx
.sess
,
379 "lifetime of variable does not enclose its \
382 .note_and_explain_region(&mut err
, "the variable is only valid for ", sup
, "");
385 infer
::ParameterInScope(_
, span
) => {
386 let mut err
= struct_span_err
!(self.tcx
.sess
,
389 "type/lifetime parameter not in scope here");
391 .note_and_explain_region(&mut err
, "the parameter is only valid for ", sub
, "");
394 infer
::DataBorrowed(ty
, span
) => {
395 let mut err
= struct_span_err
!(self.tcx
.sess
,
398 "a value of type `{}` is borrowed for too long",
399 self.ty_to_string(ty
));
400 self.tcx
.note_and_explain_region(&mut err
, "the type is valid for ", sub
, "");
401 self.tcx
.note_and_explain_region(&mut err
, "but the borrow lasts for ", sup
, "");
404 infer
::ReferenceOutlivesReferent(ty
, span
) => {
405 let mut err
= struct_span_err
!(self.tcx
.sess
,
408 "in type `{}`, reference has a longer lifetime \
409 than the data it references",
410 self.ty_to_string(ty
));
411 self.tcx
.note_and_explain_region(&mut err
, "the pointer is valid for ", sub
, "");
412 self.tcx
.note_and_explain_region(&mut err
,
413 "but the referenced data is only valid for ",
418 infer
::CompareImplMethodObligation
{ span
,
423 self.report_extra_impl_obligation(span
,
427 &format
!("`{}: {}`", sup
, sub
),