]> git.proxmox.com Git - rustc.git/blob - src/librustc/infer/error_reporting/note.rs
New upstream version 1.17.0+dfsg1
[rustc.git] / src / librustc / infer / error_reporting / note.rs
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.
4 //
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.
10
11 use infer::{self, InferCtxt, SubregionOrigin};
12 use ty::Region;
13 use ty::error::TypeError;
14 use errors::DiagnosticBuilder;
15
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>) {
20 match *origin {
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(),
27 expected,
28 found));
29 } else {
30 // FIXME: this really should be handled at some earlier stage. Our
31 // handling of region checking when type errors are present is
32 // *terrible*.
33
34 err.span_note(trace.cause.span,
35 &format!("...so that {}", trace.cause.as_requirement_str()));
36 }
37 }
38 infer::Reborrow(span) => {
39 err.span_note(span,
40 "...so that reference does not outlive borrowed content");
41 }
42 infer::ReborrowUpvar(span, ref upvar_id) => {
43 err.span_note(span,
44 &format!("...so that closure can access `{}`",
45 self.tcx
46 .local_var_name_str(upvar_id.var_id)
47 .to_string()));
48 }
49 infer::InfStackClosure(span) => {
50 err.span_note(span, "...so that closure does not outlive its stack frame");
51 }
52 infer::InvokeClosure(span) => {
53 err.span_note(span,
54 "...so that closure is not invoked outside its lifetime");
55 }
56 infer::DerefPointer(span) => {
57 err.span_note(span,
58 "...so that pointer is not dereferenced outside its lifetime");
59 }
60 infer::FreeVariable(span, id) => {
61 err.span_note(span,
62 &format!("...so that captured variable `{}` does not outlive the \
63 enclosing closure",
64 self.tcx.local_var_name_str(id)));
65 }
66 infer::IndexSlice(span) => {
67 err.span_note(span, "...so that slice is not indexed outside the lifetime");
68 }
69 infer::RelateObjectBound(span) => {
70 err.span_note(span, "...so that it can be closed over into an object");
71 }
72 infer::CallRcvr(span) => {
73 err.span_note(span,
74 "...so that method receiver is valid for the method call");
75 }
76 infer::CallArg(span) => {
77 err.span_note(span, "...so that argument is valid for the call");
78 }
79 infer::CallReturn(span) => {
80 err.span_note(span, "...so that return value is valid for the call");
81 }
82 infer::Operand(span) => {
83 err.span_note(span, "...so that operand is valid for operation");
84 }
85 infer::AddrOf(span) => {
86 err.span_note(span, "...so that reference is valid at the time of borrow");
87 }
88 infer::AutoBorrow(span) => {
89 err.span_note(span,
90 "...so that auto-reference is valid at the time of borrow");
91 }
92 infer::ExprTypeIsNotInScope(t, span) => {
93 err.span_note(span,
94 &format!("...so type `{}` of expression is valid during the \
95 expression",
96 self.ty_to_string(t)));
97 }
98 infer::BindingTypeIsNotValidAtDecl(span) => {
99 err.span_note(span,
100 "...so that variable is valid at time of its declaration");
101 }
102 infer::ParameterInScope(_, span) => {
103 err.span_note(span,
104 "...so that a type/lifetime parameter is in scope here");
105 }
106 infer::DataBorrowed(ty, span) => {
107 err.span_note(span,
108 &format!("...so that the type `{}` is not borrowed for too long",
109 self.ty_to_string(ty)));
110 }
111 infer::ReferenceOutlivesReferent(ty, span) => {
112 err.span_note(span,
113 &format!("...so that the reference type `{}` does not outlive the \
114 data it points at",
115 self.ty_to_string(ty)));
116 }
117 infer::RelateParamBound(span, t) => {
118 err.span_note(span,
119 &format!("...so that the type `{}` will meet its required \
120 lifetime bounds",
121 self.ty_to_string(t)));
122 }
123 infer::RelateDefaultParamBound(span, t) => {
124 err.span_note(span,
125 &format!("...so that type parameter instantiated with `{}`, will \
126 meet its declared lifetime bounds",
127 self.ty_to_string(t)));
128 }
129 infer::RelateRegionParamBound(span) => {
130 err.span_note(span,
131 "...so that the declared lifetime parameter bounds are satisfied");
132 }
133 infer::SafeDestructor(span) => {
134 err.span_note(span,
135 "...so that references are valid when the destructor runs");
136 }
137 infer::CompareImplMethodObligation { span, .. } => {
138 err.span_note(span,
139 "...so that the definition in impl matches the definition from the \
140 trait");
141 }
142 }
143 }
144
145 pub(super) fn report_concrete_failure(&self,
146 origin: SubregionOrigin<'tcx>,
147 sub: &'tcx Region,
148 sup: &'tcx Region)
149 -> DiagnosticBuilder<'tcx> {
150 match origin {
151 infer::Subtype(trace) => {
152 let terr = TypeError::RegionsDoesNotOutlive(sup, sub);
153 self.report_and_explain_type_error(trace, &terr)
154 }
155 infer::Reborrow(span) => {
156 let mut err = struct_span_err!(self.tcx.sess,
157 span,
158 E0312,
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 ",
163 sub,
164 "...");
165 self.tcx.note_and_explain_region(&mut err,
166 "...but the borrowed content is only valid for ",
167 sup,
168 "");
169 err
170 }
171 infer::ReborrowUpvar(span, ref upvar_id) => {
172 let mut err = struct_span_err!(self.tcx.sess,
173 span,
174 E0313,
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 ",
180 sub,
181 "...");
182 self.tcx
183 .note_and_explain_region(&mut err,
184 &format!("...but `{}` is only valid for ",
185 self.tcx
186 .local_var_name_str(upvar_id.var_id)),
187 sup,
188 "");
189 err
190 }
191 infer::InfStackClosure(span) => {
192 let mut err =
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 ",
196 sub,
197 "...");
198 self.tcx.note_and_explain_region(&mut err,
199 "...but the closure's stack frame is only valid \
200 for ",
201 sup,
202 "");
203 err
204 }
205 infer::InvokeClosure(span) => {
206 let mut err = struct_span_err!(self.tcx.sess,
207 span,
208 E0315,
209 "cannot invoke closure outside of its lifetime");
210 self.tcx
211 .note_and_explain_region(&mut err, "the closure is only valid for ", sup, "");
212 err
213 }
214 infer::DerefPointer(span) => {
215 let mut err = struct_span_err!(self.tcx.sess,
216 span,
217 E0473,
218 "dereference of reference outside its lifetime");
219 self.tcx
220 .note_and_explain_region(&mut err, "the reference is only valid for ", sup, "");
221 err
222 }
223 infer::FreeVariable(span, id) => {
224 let mut err = struct_span_err!(self.tcx.sess,
225 span,
226 E0474,
227 "captured variable `{}` does not outlive the \
228 enclosing closure",
229 self.tcx.local_var_name_str(id));
230 self.tcx
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, "");
233 err
234 }
235 infer::IndexSlice(span) => {
236 let mut err = struct_span_err!(self.tcx.sess,
237 span,
238 E0475,
239 "index of slice outside its lifetime");
240 self.tcx.note_and_explain_region(&mut err, "the slice is only valid for ", sup, "");
241 err
242 }
243 infer::RelateObjectBound(span) => {
244 let mut err = struct_span_err!(self.tcx.sess,
245 span,
246 E0476,
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 ",
252 sup,
253 "");
254 err
255 }
256 infer::RelateParamBound(span, ty) => {
257 let mut err = struct_span_err!(self.tcx.sess,
258 span,
259 E0477,
260 "the type `{}` does not fulfill the required \
261 lifetime",
262 self.ty_to_string(ty));
263 self.tcx.note_and_explain_region(&mut err, "type must outlive ", sub, "");
264 err
265 }
266 infer::RelateRegionParamBound(span) => {
267 let mut err =
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 ",
271 sup,
272 "");
273 self.tcx.note_and_explain_region(&mut err,
274 "but lifetime parameter must outlive ",
275 sub,
276 "");
277 err
278 }
279 infer::RelateDefaultParamBound(span, ty) => {
280 let mut err = struct_span_err!(self.tcx.sess,
281 span,
282 E0479,
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, "");
287 err
288 }
289 infer::CallRcvr(span) => {
290 let mut err = struct_span_err!(self.tcx.sess,
291 span,
292 E0480,
293 "lifetime of method receiver does not outlive the \
294 method call");
295 self.tcx
296 .note_and_explain_region(&mut err, "the receiver is only valid for ", sup, "");
297 err
298 }
299 infer::CallArg(span) => {
300 let mut err = struct_span_err!(self.tcx.sess,
301 span,
302 E0481,
303 "lifetime of function argument does not outlive \
304 the function call");
305 self.tcx.note_and_explain_region(&mut err,
306 "the function argument is only valid for ",
307 sup,
308 "");
309 err
310 }
311 infer::CallReturn(span) => {
312 let mut err = struct_span_err!(self.tcx.sess,
313 span,
314 E0482,
315 "lifetime of return value does not outlive the \
316 function call");
317 self.tcx.note_and_explain_region(&mut err,
318 "the return value is only valid for ",
319 sup,
320 "");
321 err
322 }
323 infer::Operand(span) => {
324 let mut err = struct_span_err!(self.tcx.sess,
325 span,
326 E0483,
327 "lifetime of operand does not outlive the \
328 operation");
329 self.tcx
330 .note_and_explain_region(&mut err, "the operand is only valid for ", sup, "");
331 err
332 }
333 infer::AddrOf(span) => {
334 let mut err = struct_span_err!(self.tcx.sess,
335 span,
336 E0484,
337 "reference is not valid at the time of borrow");
338 self.tcx
339 .note_and_explain_region(&mut err, "the borrow is only valid for ", sup, "");
340 err
341 }
342 infer::AutoBorrow(span) => {
343 let mut err = struct_span_err!(self.tcx.sess,
344 span,
345 E0485,
346 "automatically reference is not valid at the time \
347 of borrow");
348 self.tcx.note_and_explain_region(&mut err,
349 "the automatic borrow is only valid for ",
350 sup,
351 "");
352 err
353 }
354 infer::ExprTypeIsNotInScope(t, span) => {
355 let mut err = struct_span_err!(self.tcx.sess,
356 span,
357 E0486,
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, "");
362 err
363 }
364 infer::SafeDestructor(span) => {
365 let mut err = struct_span_err!(self.tcx.sess,
366 span,
367 E0487,
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, "");
373 err
374 }
375 infer::BindingTypeIsNotValidAtDecl(span) => {
376 let mut err = struct_span_err!(self.tcx.sess,
377 span,
378 E0488,
379 "lifetime of variable does not enclose its \
380 declaration");
381 self.tcx
382 .note_and_explain_region(&mut err, "the variable is only valid for ", sup, "");
383 err
384 }
385 infer::ParameterInScope(_, span) => {
386 let mut err = struct_span_err!(self.tcx.sess,
387 span,
388 E0489,
389 "type/lifetime parameter not in scope here");
390 self.tcx
391 .note_and_explain_region(&mut err, "the parameter is only valid for ", sub, "");
392 err
393 }
394 infer::DataBorrowed(ty, span) => {
395 let mut err = struct_span_err!(self.tcx.sess,
396 span,
397 E0490,
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, "");
402 err
403 }
404 infer::ReferenceOutlivesReferent(ty, span) => {
405 let mut err = struct_span_err!(self.tcx.sess,
406 span,
407 E0491,
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 ",
414 sup,
415 "");
416 err
417 }
418 infer::CompareImplMethodObligation { span,
419 item_name,
420 impl_item_def_id,
421 trait_item_def_id,
422 lint_id } => {
423 self.report_extra_impl_obligation(span,
424 item_name,
425 impl_item_def_id,
426 trait_item_def_id,
427 &format!("`{}: {}`", sup, sub),
428 lint_id)
429 }
430 }
431 }
432 }