]> git.lizzy.rs Git - rust.git/blob - src/librustc_infer/infer/error_reporting/note.rs
Remove unused error reporting code
[rust.git] / src / librustc_infer / infer / error_reporting / note.rs
1 use crate::infer::error_reporting::{note_and_explain_region, ObligationCauseExt};
2 use crate::infer::{self, InferCtxt, SubregionOrigin};
3 use rustc_errors::{struct_span_err, DiagnosticBuilder};
4 use rustc_middle::middle::region;
5 use rustc_middle::ty::error::TypeError;
6 use rustc_middle::ty::{self, Region};
7
8 impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
9     pub(super) fn note_region_origin(
10         &self,
11         err: &mut DiagnosticBuilder<'_>,
12         origin: &SubregionOrigin<'tcx>,
13     ) {
14         match *origin {
15             infer::Subtype(ref trace) => {
16                 if let Some((expected, found)) = self.values_str(&trace.values) {
17                     err.span_note(
18                         trace.cause.span,
19                         &format!("...so that the {}", trace.cause.as_requirement_str()),
20                     );
21
22                     err.note_expected_found(&"", expected, &"", found);
23                 } else {
24                     // FIXME: this really should be handled at some earlier stage. Our
25                     // handling of region checking when type errors are present is
26                     // *terrible*.
27
28                     err.span_note(
29                         trace.cause.span,
30                         &format!("...so that {}", trace.cause.as_requirement_str()),
31                     );
32                 }
33             }
34             infer::Reborrow(span) => {
35                 err.span_note(span, "...so that reference does not outlive borrowed content");
36             }
37             infer::ReborrowUpvar(span, ref upvar_id) => {
38                 let var_name = self.tcx.hir().name(upvar_id.var_path.hir_id);
39                 err.span_note(span, &format!("...so that closure can access `{}`", var_name));
40             }
41             infer::RelateObjectBound(span) => {
42                 err.span_note(span, "...so that it can be closed over into an object");
43             }
44             infer::CallReturn(span) => {
45                 err.span_note(span, "...so that return value is valid for the call");
46             }
47             infer::DataBorrowed(ty, span) => {
48                 err.span_note(
49                     span,
50                     &format!(
51                         "...so that the type `{}` is not borrowed for too long",
52                         self.ty_to_string(ty)
53                     ),
54                 );
55             }
56             infer::ReferenceOutlivesReferent(ty, span) => {
57                 err.span_note(
58                     span,
59                     &format!(
60                         "...so that the reference type `{}` does not outlive the \
61                                         data it points at",
62                         self.ty_to_string(ty)
63                     ),
64                 );
65             }
66             infer::RelateParamBound(span, t) => {
67                 err.span_note(
68                     span,
69                     &format!(
70                         "...so that the type `{}` will meet its required \
71                                         lifetime bounds",
72                         self.ty_to_string(t)
73                     ),
74                 );
75             }
76             infer::RelateRegionParamBound(span) => {
77                 err.span_note(
78                     span,
79                     "...so that the declared lifetime parameter bounds are satisfied",
80                 );
81             }
82             infer::CompareImplMethodObligation { span, .. } => {
83                 err.span_note(
84                     span,
85                     "...so that the definition in impl matches the definition from the \
86                                trait",
87                 );
88             }
89         }
90     }
91
92     pub(super) fn report_concrete_failure(
93         &self,
94         region_scope_tree: &region::ScopeTree,
95         origin: SubregionOrigin<'tcx>,
96         sub: Region<'tcx>,
97         sup: Region<'tcx>,
98     ) -> DiagnosticBuilder<'tcx> {
99         match origin {
100             infer::Subtype(box trace) => {
101                 let terr = TypeError::RegionsDoesNotOutlive(sup, sub);
102                 let mut err = self.report_and_explain_type_error(trace, &terr);
103                 note_and_explain_region(self.tcx, region_scope_tree, &mut err, "", sup, "...");
104                 note_and_explain_region(
105                     self.tcx,
106                     region_scope_tree,
107                     &mut err,
108                     "...does not necessarily outlive ",
109                     sub,
110                     "",
111                 );
112                 err
113             }
114             infer::Reborrow(span) => {
115                 let mut err = struct_span_err!(
116                     self.tcx.sess,
117                     span,
118                     E0312,
119                     "lifetime of reference outlives lifetime of \
120                                                 borrowed content..."
121                 );
122                 note_and_explain_region(
123                     self.tcx,
124                     region_scope_tree,
125                     &mut err,
126                     "...the reference is valid for ",
127                     sub,
128                     "...",
129                 );
130                 note_and_explain_region(
131                     self.tcx,
132                     region_scope_tree,
133                     &mut err,
134                     "...but the borrowed content is only valid for ",
135                     sup,
136                     "",
137                 );
138                 err
139             }
140             infer::ReborrowUpvar(span, ref upvar_id) => {
141                 let var_name = self.tcx.hir().name(upvar_id.var_path.hir_id);
142                 let mut err = struct_span_err!(
143                     self.tcx.sess,
144                     span,
145                     E0313,
146                     "lifetime of borrowed pointer outlives lifetime \
147                                                 of captured variable `{}`...",
148                     var_name
149                 );
150                 note_and_explain_region(
151                     self.tcx,
152                     region_scope_tree,
153                     &mut err,
154                     "...the borrowed pointer is valid for ",
155                     sub,
156                     "...",
157                 );
158                 note_and_explain_region(
159                     self.tcx,
160                     region_scope_tree,
161                     &mut err,
162                     &format!("...but `{}` is only valid for ", var_name),
163                     sup,
164                     "",
165                 );
166                 err
167             }
168             infer::RelateObjectBound(span) => {
169                 let mut err = struct_span_err!(
170                     self.tcx.sess,
171                     span,
172                     E0476,
173                     "lifetime of the source pointer does not outlive \
174                                                 lifetime bound of the object type"
175                 );
176                 note_and_explain_region(
177                     self.tcx,
178                     region_scope_tree,
179                     &mut err,
180                     "object type is valid for ",
181                     sub,
182                     "",
183                 );
184                 note_and_explain_region(
185                     self.tcx,
186                     region_scope_tree,
187                     &mut err,
188                     "source pointer is only valid for ",
189                     sup,
190                     "",
191                 );
192                 err
193             }
194             infer::RelateParamBound(span, ty) => {
195                 let mut err = struct_span_err!(
196                     self.tcx.sess,
197                     span,
198                     E0477,
199                     "the type `{}` does not fulfill the required \
200                                                 lifetime",
201                     self.ty_to_string(ty)
202                 );
203                 match *sub {
204                     ty::ReStatic => note_and_explain_region(
205                         self.tcx,
206                         region_scope_tree,
207                         &mut err,
208                         "type must satisfy ",
209                         sub,
210                         "",
211                     ),
212                     _ => note_and_explain_region(
213                         self.tcx,
214                         region_scope_tree,
215                         &mut err,
216                         "type must outlive ",
217                         sub,
218                         "",
219                     ),
220                 }
221                 err
222             }
223             infer::RelateRegionParamBound(span) => {
224                 let mut err =
225                     struct_span_err!(self.tcx.sess, span, E0478, "lifetime bound not satisfied");
226                 note_and_explain_region(
227                     self.tcx,
228                     region_scope_tree,
229                     &mut err,
230                     "lifetime parameter instantiated with ",
231                     sup,
232                     "",
233                 );
234                 note_and_explain_region(
235                     self.tcx,
236                     region_scope_tree,
237                     &mut err,
238                     "but lifetime parameter must outlive ",
239                     sub,
240                     "",
241                 );
242                 err
243             }
244             infer::CallReturn(span) => {
245                 let mut err = struct_span_err!(
246                     self.tcx.sess,
247                     span,
248                     E0482,
249                     "lifetime of return value does not outlive the \
250                                                 function call"
251                 );
252                 note_and_explain_region(
253                     self.tcx,
254                     region_scope_tree,
255                     &mut err,
256                     "the return value is only valid for ",
257                     sup,
258                     "",
259                 );
260                 err
261             }
262             infer::DataBorrowed(ty, span) => {
263                 let mut err = struct_span_err!(
264                     self.tcx.sess,
265                     span,
266                     E0490,
267                     "a value of type `{}` is borrowed for too long",
268                     self.ty_to_string(ty)
269                 );
270                 note_and_explain_region(
271                     self.tcx,
272                     region_scope_tree,
273                     &mut err,
274                     "the type is valid for ",
275                     sub,
276                     "",
277                 );
278                 note_and_explain_region(
279                     self.tcx,
280                     region_scope_tree,
281                     &mut err,
282                     "but the borrow lasts for ",
283                     sup,
284                     "",
285                 );
286                 err
287             }
288             infer::ReferenceOutlivesReferent(ty, span) => {
289                 let mut err = struct_span_err!(
290                     self.tcx.sess,
291                     span,
292                     E0491,
293                     "in type `{}`, reference has a longer lifetime than the data it references",
294                     self.ty_to_string(ty)
295                 );
296                 note_and_explain_region(
297                     self.tcx,
298                     region_scope_tree,
299                     &mut err,
300                     "the pointer is valid for ",
301                     sub,
302                     "",
303                 );
304                 note_and_explain_region(
305                     self.tcx,
306                     region_scope_tree,
307                     &mut err,
308                     "but the referenced data is only valid for ",
309                     sup,
310                     "",
311                 );
312                 err
313             }
314             infer::CompareImplMethodObligation {
315                 span,
316                 item_name,
317                 impl_item_def_id,
318                 trait_item_def_id,
319             } => self.report_extra_impl_obligation(
320                 span,
321                 item_name,
322                 impl_item_def_id,
323                 trait_item_def_id,
324                 &format!("`{}: {}`", sup, sub),
325             ),
326         }
327     }
328
329     pub(super) fn report_placeholder_failure(
330         &self,
331         region_scope_tree: &region::ScopeTree,
332         placeholder_origin: SubregionOrigin<'tcx>,
333         sub: Region<'tcx>,
334         sup: Region<'tcx>,
335     ) -> DiagnosticBuilder<'tcx> {
336         // I can't think how to do better than this right now. -nikomatsakis
337         match placeholder_origin {
338             infer::Subtype(box trace) => {
339                 let terr = TypeError::RegionsPlaceholderMismatch;
340                 self.report_and_explain_type_error(trace, &terr)
341             }
342
343             _ => self.report_concrete_failure(region_scope_tree, placeholder_origin, sub, sup),
344         }
345     }
346 }