]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/error_reporting/different_lifetimes.rs
Auto merge of #44350 - GuillaumeGomez:id-false-positive, r=QuietMisdreavus
[rust.git] / src / librustc / infer / error_reporting / different_lifetimes.rs
1 // Copyright 2012-2013 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 //! Error Reporting for Anonymous Region Lifetime Errors
12 //! where both the regions are anonymous.
13 use hir;
14 use infer::InferCtxt;
15 use ty::{self, Region};
16 use infer::region_inference::RegionResolutionError::*;
17 use infer::region_inference::RegionResolutionError;
18 use hir::map as hir_map;
19 use middle::resolve_lifetime as rl;
20 use hir::intravisit::{self, Visitor, NestedVisitorMap};
21
22 impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
23     // This method prints the error message for lifetime errors when both the concerned regions
24     // are anonymous.
25     // Consider a case where we have
26     // fn foo(x: &mut Vec<&u8>, y: &u8)
27     //    { x.push(y); }.
28     // The example gives
29     // fn foo(x: &mut Vec<&u8>, y: &u8) {
30     //                    ---      --- these references are declared with different lifetimes...
31     //            x.push(y);
32     //            ^ ...but data from `y` flows into `x` here
33     // It has been extended for the case of structs too.
34     // Consider the example
35     // struct Ref<'a> { x: &'a u32 }
36     // fn foo(mut x: Vec<Ref>, y: Ref) {
37     //                   ---      --- these structs are declared with different lifetimes...
38     //               x.push(y);
39     //               ^ ...but data from `y` flows into `x` here
40     // }
41     // It will later be extended to trait objects.
42     pub fn try_report_anon_anon_conflict(&self, error: &RegionResolutionError<'tcx>) -> bool {
43         let (span, sub, sup) = match *error {
44             ConcreteFailure(ref origin, sub, sup) => (origin.span(), sub, sup),
45             _ => return false, // inapplicable
46         };
47
48         // Determine whether the sub and sup consist of both anonymous (elided) regions.
49         let anon_reg_sup = or_false!(self.is_suitable_region(sup));
50
51         let anon_reg_sub = or_false!(self.is_suitable_region(sub));
52         let scope_def_id_sup = anon_reg_sup.def_id;
53         let bregion_sup = anon_reg_sup.boundregion;
54         let scope_def_id_sub = anon_reg_sub.def_id;
55         let bregion_sub = anon_reg_sub.boundregion;
56
57         let ty_sup = or_false!(self.find_anon_type(sup, &bregion_sup));
58
59         let ty_sub = or_false!(self.find_anon_type(sub, &bregion_sub));
60         debug!("try_report_anon_anon_conflict: found_arg1={:?} sup={:?} br1={:?}",
61                ty_sub,
62                sup,
63                bregion_sup);
64         debug!("try_report_anon_anon_conflict: found_arg2={:?} sub={:?} br2={:?}",
65                ty_sup,
66                sub,
67                bregion_sub);
68
69         let (main_label, label1, label2) = if let (Some(sup_arg), Some(sub_arg)) =
70             (self.find_arg_with_region(sup, sup), self.find_arg_with_region(sub, sub)) {
71
72             let (anon_arg_sup, is_first_sup, anon_arg_sub, is_first_sub) =
73                 (sup_arg.arg, sup_arg.is_first, sub_arg.arg, sub_arg.is_first);
74             if self.is_self_anon(is_first_sup, scope_def_id_sup) ||
75                self.is_self_anon(is_first_sub, scope_def_id_sub) {
76                 return false;
77             }
78
79             if self.is_return_type_anon(scope_def_id_sup, bregion_sup) ||
80                self.is_return_type_anon(scope_def_id_sub, bregion_sub) {
81                 return false;
82             }
83
84             if anon_arg_sup == anon_arg_sub {
85                 (format!("this type was declared with multiple lifetimes..."),
86                  format!(" with one lifetime"),
87                  format!(" into the other"))
88             } else {
89                 let span_label_var1 = if let Some(simple_name) = anon_arg_sup.pat.simple_name() {
90                     format!(" from `{}`", simple_name)
91                 } else {
92                     format!("")
93                 };
94
95                 let span_label_var2 = if let Some(simple_name) = anon_arg_sub.pat.simple_name() {
96                     format!(" into `{}`", simple_name)
97                 } else {
98                     format!("")
99                 };
100
101                 let span_label =
102                     format!("these two types are declared with different lifetimes...",);
103
104                 (span_label, span_label_var1, span_label_var2)
105             }
106         } else {
107             debug!("no arg with anon region found");
108             debug!("try_report_anon_anon_conflict: is_suitable(sub) = {:?}",
109                    self.is_suitable_region(sub));
110             debug!("try_report_anon_anon_conflict: is_suitable(sup) = {:?}",
111                    self.is_suitable_region(sup));
112             return false;
113         };
114
115         struct_span_err!(self.tcx.sess, span, E0623, "lifetime mismatch")
116             .span_label(ty_sup.span, main_label)
117             .span_label(ty_sub.span, format!(""))
118             .span_label(span, format!("...but data{} flows{} here", label1, label2))
119             .emit();
120         return true;
121     }
122
123     /// This function calls the `visit_ty` method for the parameters
124     /// corresponding to the anonymous regions. The `nested_visitor.found_type`
125     /// contains the anonymous type.
126     ///
127     /// # Arguments
128     /// region - the anonymous region corresponding to the anon_anon conflict
129     /// br - the bound region corresponding to the above region which is of type `BrAnon(_)`
130     ///
131     /// # Example
132     /// ```
133     /// fn foo(x: &mut Vec<&u8>, y: &u8)
134     ///    { x.push(y); }
135     /// ```
136     /// The function returns the nested type corresponding to the anonymous region
137     /// for e.g. `&u8` and Vec<`&u8`.
138     pub fn find_anon_type(&self, region: Region<'tcx>, br: &ty::BoundRegion) -> Option<&hir::Ty> {
139         if let Some(anon_reg) = self.is_suitable_region(region) {
140             let def_id = anon_reg.def_id;
141             if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
142                 let inputs: &[_] = match self.tcx.hir.get(node_id) {
143                     hir_map::NodeItem(&hir::Item { node: hir::ItemFn(ref fndecl, ..), .. }) => {
144                         &fndecl.inputs
145                     }
146                     hir_map::NodeTraitItem(&hir::TraitItem {
147                                                node: hir::TraitItemKind::Method(ref fndecl, ..), ..
148                                            }) => &fndecl.decl.inputs,
149                     hir_map::NodeImplItem(&hir::ImplItem {
150                                               node: hir::ImplItemKind::Method(ref fndecl, ..), ..
151                                           }) => &fndecl.decl.inputs,
152
153                     _ => &[],
154                 };
155
156                 return inputs
157                            .iter()
158                            .filter_map(|arg| self.find_component_for_bound_region(&**arg, br))
159                            .next();
160             }
161         }
162         None
163     }
164
165     // This method creates a FindNestedTypeVisitor which returns the type corresponding
166     // to the anonymous region.
167     fn find_component_for_bound_region(&self,
168                                        arg: &'gcx hir::Ty,
169                                        br: &ty::BoundRegion)
170                                        -> Option<(&'gcx hir::Ty)> {
171         let mut nested_visitor = FindNestedTypeVisitor {
172             infcx: &self,
173             hir_map: &self.tcx.hir,
174             bound_region: *br,
175             found_type: None,
176             depth: 1,
177         };
178         nested_visitor.visit_ty(arg);
179         nested_visitor.found_type
180     }
181 }
182
183 // The FindNestedTypeVisitor captures the corresponding `hir::Ty` of the
184 // anonymous region. The example above would lead to a conflict between
185 // the two anonymous lifetimes for &u8 in x and y respectively. This visitor
186 // would be invoked twice, once for each lifetime, and would
187 // walk the types like &mut Vec<&u8> and &u8 looking for the HIR
188 // where that lifetime appears. This allows us to highlight the
189 // specific part of the type in the error message.
190 struct FindNestedTypeVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
191     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
192     hir_map: &'a hir::map::Map<'gcx>,
193     // The bound_region corresponding to the Refree(freeregion)
194     // associated with the anonymous region we are looking for.
195     bound_region: ty::BoundRegion,
196     // The type where the anonymous lifetime appears
197     // for e.g. Vec<`&u8`> and <`&u8`>
198     found_type: Option<&'gcx hir::Ty>,
199     depth: u32,
200 }
201
202 impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
203     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
204         NestedVisitorMap::OnlyBodies(&self.hir_map)
205     }
206
207     fn visit_ty(&mut self, arg: &'gcx hir::Ty) {
208         match arg.node {
209             hir::TyBareFn(_) => {
210                 self.depth += 1;
211                 intravisit::walk_ty(self, arg);
212                 self.depth -= 1;
213                 return;
214             }
215
216             hir::TyTraitObject(ref bounds, _) => {
217                 for bound in bounds {
218                     self.depth += 1;
219                     self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
220                     self.depth -= 1;
221                 }
222             }
223
224             hir::TyRptr(ref lifetime, _) => {
225                 // the lifetime of the TyRptr
226                 let hir_id = self.infcx.tcx.hir.node_to_hir_id(lifetime.id);
227                 match (self.infcx.tcx.named_region(hir_id), self.bound_region) {
228                     // Find the index of the anonymous region that was part of the
229                     // error. We will then search the function parameters for a bound
230                     // region at the right depth with the same index
231                     (Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)),
232                      ty::BrAnon(br_index)) => {
233                         debug!("LateBoundAnon depth = {:?} anon_index = {:?} br_index={:?}",
234                                debruijn_index.depth,
235                                anon_index,
236                                br_index);
237                         if debruijn_index.depth == self.depth && anon_index == br_index {
238                             self.found_type = Some(arg);
239                             return; // we can stop visiting now
240                         }
241                     }
242
243                     // Find the index of the named region that was part of the
244                     // error. We will then search the function parameters for a bound
245                     // region at the right depth with the same index
246                     (Some(rl::Region::EarlyBound(_, id)), ty::BrNamed(def_id, _)) => {
247                         debug!("EarlyBound self.infcx.tcx.hir.local_def_id(id)={:?} \
248                                         def_id={:?}", id, def_id);
249                         if id == def_id {
250                             self.found_type = Some(arg);
251                             return; // we can stop visiting now
252                         }
253                     }
254
255                     // Find the index of the named region that was part of the
256                     // error. We will then search the function parameters for a bound
257                     // region at the right depth with the same index
258                     (Some(rl::Region::LateBound(debruijn_index, id)), ty::BrNamed(def_id, _)) => {
259                         debug!("FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
260                                debruijn_index.depth);
261                         debug!("self.infcx.tcx.hir.local_def_id(id)={:?}", id);
262                         debug!("def_id={:?}", def_id);
263                         if debruijn_index.depth == self.depth && id == def_id {
264                             self.found_type = Some(arg);
265                             return; // we can stop visiting now
266                         }
267                     }
268
269                     (Some(rl::Region::Static), _) |
270                     (Some(rl::Region::Free(_, _)), _) |
271                     (Some(rl::Region::EarlyBound(_, _)), _) |
272                     (Some(rl::Region::LateBound(_, _)), _) |
273                     (Some(rl::Region::LateBoundAnon(_, _)), _) |
274                     (None, _) => {
275                         debug!("no arg found");
276                     }
277                 }
278             }
279             // Checks if it is of type `hir::TyPath` which corresponds to a struct.
280             hir::TyPath(_) => {
281                 let subvisitor = &mut TyPathVisitor {
282                                           infcx: self.infcx,
283                                           found_it: false,
284                                           bound_region: self.bound_region,
285                                           hir_map: self.hir_map,
286                                           depth: self.depth,
287                                       };
288                 intravisit::walk_ty(subvisitor, arg); // call walk_ty; as visit_ty is empty,
289                 // this will visit only outermost type
290                 if subvisitor.found_it {
291                     self.found_type = Some(arg);
292                 }
293             }
294             _ => {}
295         }
296         // walk the embedded contents: e.g., if we are visiting `Vec<&Foo>`,
297         // go on to visit `&Foo`
298         intravisit::walk_ty(self, arg);
299     }
300 }
301
302 // The visitor captures the corresponding `hir::Ty` of the anonymous region
303 // in the case of structs ie. `hir::TyPath`.
304 // This visitor would be invoked for each lifetime corresponding to a struct,
305 // and would walk the types like Vec<Ref> in the above example and Ref looking for the HIR
306 // where that lifetime appears. This allows us to highlight the
307 // specific part of the type in the error message.
308 struct TyPathVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
309     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
310     hir_map: &'a hir::map::Map<'gcx>,
311     found_it: bool,
312     bound_region: ty::BoundRegion,
313     depth: u32,
314 }
315
316 impl<'a, 'gcx, 'tcx> Visitor<'gcx> for TyPathVisitor<'a, 'gcx, 'tcx> {
317     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
318         NestedVisitorMap::OnlyBodies(&self.hir_map)
319     }
320
321     fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
322
323         let hir_id = self.infcx.tcx.hir.node_to_hir_id(lifetime.id);
324         match (self.infcx.tcx.named_region(hir_id), self.bound_region) {
325             // the lifetime of the TyPath!
326             (Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)), ty::BrAnon(br_index)) => {
327                 if debruijn_index.depth == self.depth && anon_index == br_index {
328                     self.found_it = true;
329                     return;
330                 }
331             }
332
333             (Some(rl::Region::EarlyBound(_, id)), ty::BrNamed(def_id, _)) => {
334                 debug!("EarlyBound self.infcx.tcx.hir.local_def_id(id)={:?} \
335                                         def_id={:?}", id, def_id);
336                 if id == def_id {
337                     self.found_it = true;
338                     return; // we can stop visiting now
339                 }
340             }
341
342             (Some(rl::Region::LateBound(debruijn_index, id)), ty::BrNamed(def_id, _)) => {
343                 debug!("FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
344                        debruijn_index.depth);
345                 debug!("id={:?}", id);
346                 debug!("def_id={:?}", def_id);
347                 if debruijn_index.depth == self.depth && id == def_id {
348                     self.found_it = true;
349                     return; // we can stop visiting now
350                 }
351             }
352
353             (Some(rl::Region::Static), _) |
354             (Some(rl::Region::EarlyBound(_, _)), _) |
355             (Some(rl::Region::LateBound(_, _)), _) |
356             (Some(rl::Region::LateBoundAnon(_, _)), _) |
357             (Some(rl::Region::Free(_, _)), _) |
358             (None, _) => {
359                 debug!("no arg found");
360             }
361         }
362     }
363
364     fn visit_ty(&mut self, arg: &'gcx hir::Ty) {
365         // ignore nested types
366         //
367         // If you have a type like `Foo<'a, &Ty>` we
368         // are only interested in the immediate lifetimes ('a).
369         //
370         // Making `visit_ty` empty will ignore the `&Ty` embedded
371         // inside, it will get reached by the outer visitor.
372         debug!("`Ty` corresponding to a struct is {:?}", arg);
373     }
374 }