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