]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/error_reporting/anon_anon_conflict.rs
extend E0623 for fns
[rust.git] / src / librustc / infer / error_reporting / anon_anon_conflict.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 (ty1, ty2, scope_def_id_1, scope_def_id_2, bregion1, bregion2) = if
50             self.is_suitable_anonymous_region(sup, true).is_some() &&
51             self.is_suitable_anonymous_region(sub, true).is_some() {
52             if let (Some(anon_reg1), Some(anon_reg2)) =
53                 (self.is_suitable_anonymous_region(sup, true),
54                  self.is_suitable_anonymous_region(sub, true)) {
55                 let ((def_id1, br1), (def_id2, br2)) = (anon_reg1, anon_reg2);
56                 let found_arg1 = self.find_anon_type(sup, &br1);
57                 let found_arg2 = self.find_anon_type(sub, &br2);
58                 match (found_arg1, found_arg2) {
59                     (Some(anonarg_1), Some(anonarg_2)) => {
60                         (anonarg_1, anonarg_2, def_id1, def_id2, br1, br2)
61                     }
62                     _ => {
63                         return false;
64                     }
65                 }
66
67             } else {
68                 return false;
69             }
70         } else {
71             return false; //inapplicable
72         };
73
74         let (label1, label2) = if let (Some(sup_arg), Some(sub_arg)) =
75             (self.find_arg_with_anonymous_region(sup, sup),
76              self.find_arg_with_anonymous_region(sub, sub)) {
77
78             let ((anon_arg1, _, _, is_first1), (anon_arg2, _, _, is_first2)) = (sup_arg, sub_arg);
79             if self.is_self_anon(is_first1, scope_def_id_1) ||
80                self.is_self_anon(is_first2, scope_def_id_2) {
81                 return false;
82             }
83
84             if self.is_return_type_anon(scope_def_id_1, bregion1) ||
85                self.is_return_type_anon(scope_def_id_2, bregion2) {
86                 return false;
87             }
88
89
90
91
92             if anon_arg1 == anon_arg2 {
93                 (format!(" with one lifetime"), format!(" into the other"))
94             } else {
95                 let span_label_var1 = if let Some(simple_name) = anon_arg1.pat.simple_name() {
96                     format!(" from `{}`", simple_name)
97                 } else {
98                     format!("")
99                 };
100
101                 let span_label_var2 = if let Some(simple_name) = anon_arg2.pat.simple_name() {
102                     format!(" into `{}`", simple_name)
103                 } else {
104                     format!("")
105                 };
106
107                 (span_label_var1, span_label_var2)
108             }
109         } else {
110             return false;
111         };
112
113         struct_span_err!(self.tcx.sess, span, E0623, "lifetime mismatch")
114             .span_label(ty1.span,
115                         format!("these two types are declared with different lifetimes..."))
116             .span_label(ty2.span, format!(""))
117             .span_label(span, format!("...but data{} flows{} here", label1, label2))
118             .emit();
119         return true;
120
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     ///
129     /// region - the anonymous region corresponding to the anon_anon conflict
130     /// br - the bound region corresponding to the above region which is of type `BrAnon(_)`
131     ///
132     /// # Example
133     /// ```
134     /// fn foo(x: &mut Vec<&u8>, y: &u8)
135     ///    { x.push(y); }
136     /// ```
137     /// The function returns the nested type corresponding to the anonymous region
138     /// for e.g. `&u8` and Vec<`&u8`.
139     pub fn find_anon_type(&self, region: Region<'tcx>, br: &ty::BoundRegion) -> Option<(&hir::Ty)> {
140         if let Some(anon_reg) = self.is_suitable_anonymous_region(region, true) {
141             let (def_id, _) = anon_reg;
142             if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
143                 let ret_ty = self.tcx.type_of(def_id);
144                 if let ty::TyFnDef(_, _) = ret_ty.sty {
145                     if let hir_map::NodeItem(it) = self.tcx.hir.get(node_id) {
146                         if let hir::ItemFn(ref fndecl, _, _, _, _, _) = it.node {
147                             return fndecl
148                                        .inputs
149                                        .iter()
150                                        .filter_map(|arg| {
151                                                        self.find_visitor_found_type(&**arg, br)
152                                                    })
153                                        .next();
154                         }
155                     } else if let hir_map::NodeTraitItem(it) = self.tcx.hir.get(node_id) {
156                         if let hir::TraitItemKind::Method(ref fndecl, _) = it.node {
157                             return fndecl
158                                        .decl
159                                        .inputs
160                                        .iter()
161                                        .filter_map(|arg| {
162                                                        self.find_visitor_found_type(&**arg, br)
163                                                    })
164                                        .next();
165                         }
166                     } else if let hir_map::NodeImplItem(it) = self.tcx.hir.get(node_id) {
167                         if let hir::ImplItemKind::Method(ref fndecl, _) = it.node {
168                             return fndecl
169                                        .decl
170                                        .inputs
171                                        .iter()
172                                        .filter_map(|arg| {
173                                                        self.find_visitor_found_type(&**arg, br)
174                                                    })
175                                        .next();
176                         }
177                     }
178                 }
179             }
180         }
181         None
182     }
183
184     // This method creates a FindNestedTypeVisitor which returns the type corresponding
185     // to the anonymous region.
186     fn find_visitor_found_type(&self,
187                                arg: &'gcx hir::Ty,
188                                br: &ty::BoundRegion)
189                                -> Option<(&'gcx hir::Ty)> {
190         let mut nested_visitor = FindNestedTypeVisitor {
191             infcx: &self,
192             hir_map: &self.tcx.hir,
193             bound_region: *br,
194             found_type: None,
195             depth: 0,
196         };
197         nested_visitor.visit_ty(arg);
198         nested_visitor.found_type
199     }
200 }
201
202 // The FindNestedTypeVisitor captures the corresponding `hir::Ty` of the
203 // anonymous region. The example above would lead to a conflict between
204 // the two anonymous lifetimes for &u8 in x and y respectively. This visitor
205 // would be invoked twice, once for each lifetime, and would
206 // walk the types like &mut Vec<&u8> and &u8 looking for the HIR
207 // where that lifetime appears. This allows us to highlight the
208 // specific part of the type in the error message.
209 struct FindNestedTypeVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
210     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
211     hir_map: &'a hir::map::Map<'gcx>,
212     // The bound_region corresponding to the Refree(freeregion)
213     // associated with the anonymous region we are looking for.
214     bound_region: ty::BoundRegion,
215     // The type where the anonymous lifetime appears
216     // for e.g. Vec<`&u8`> and <`&u8`>
217     found_type: Option<&'gcx hir::Ty>,
218     depth: u32,
219 }
220
221 impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
222     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
223         NestedVisitorMap::OnlyBodies(&self.hir_map)
224     }
225
226     fn visit_ty(&mut self, arg: &'gcx hir::Ty) {
227         // Find the index of the anonymous region that was part of the
228         // error. We will then search the function parameters for a bound
229         // region at the right depth with the same index.
230         let br_index = match self.bound_region {
231             ty::BrAnon(index) => index,
232             _ => return,
233         };
234
235         match arg.node {
236             hir::TyRptr(ref lifetime, _) => {
237                 match self.infcx.tcx.named_region_map.defs.get(&lifetime.id) {
238                     // the lifetime of the TyRptr
239                     Some(&rl::Region::LateBoundAnon(debuijn_index, anon_index)) => {
240                         if debuijn_index.depth == 1 && anon_index == br_index {
241                             self.found_type = Some(arg);
242                             return; // we can stop visiting now
243                         }
244                     }
245                     Some(&rl::Region::Static) |
246                     Some(&rl::Region::EarlyBound(_, _)) |
247                     Some(&rl::Region::LateBound(_, _)) |
248                     Some(&rl::Region::Free(_, _)) |
249                     None => {
250                         debug!("no arg found");
251                     }
252                 }
253             }
254             // Checks if it is of type `hir::TyPath` which corresponds to a struct.
255             hir::TyPath(_) => {
256                 let subvisitor = &mut TyPathVisitor {
257                                           infcx: self.infcx,
258                                           found_it: false,
259                                           bound_region: self.bound_region,
260                                           hir_map: self.hir_map,
261                                       };
262                 intravisit::walk_ty(subvisitor, arg); // call walk_ty; as visit_ty is empty,
263                 // this will visit only outermost type
264                 if subvisitor.found_it {
265                     self.found_type = Some(arg);
266                 }
267             }
268
269             hir::TyBareFn(ref fndecl) => {
270                 fndecl.lifetimes.iter().filter_map(|lf| {
271                     match self.infcx.tcx.named_region_map.defs.get(&lf.lifetime.id) {
272
273                         Some(&rl::Region::LateBoundAnon(debuijn_index, anon_index)) => {
274                         if debuijn_index.depth == self.depth && anon_index == br_index {
275                             self.found_type = Some(arg);
276                             return; // we can stop visiting now
277                         }else{}
278                     }
279                     Some(&rl::Region::Static) |
280                     Some(&rl::Region::EarlyBound(_, _)) |
281                     Some(&rl::Region::LateBound(_, _)) |
282                     Some(&rl::Region::Free(_, _)) |
283                     None => {
284                         debug!("no arg found");
285                     }
286                 }       
287             
288             }).next();}
289             
290             _ => {}
291         }
292         // walk the embedded contents: e.g., if we are visiting `Vec<&Foo>`,
293         // go on to visit `&Foo`
294         self.depth += 1;
295         intravisit::walk_ty(self, arg);
296         self.depth += 1;
297     }
298 }
299
300 // The visitor captures the corresponding `hir::Ty` of the anonymous region
301 // in the case of structs ie. `hir::TyPath`.
302 // This visitor would be invoked for each lifetime corresponding to a struct,
303 // and would walk the types like Vec<Ref> in the above example and Ref looking for the HIR
304 // where that lifetime appears. This allows us to highlight the
305 // specific part of the type in the error message.
306 struct TyPathVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
307     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
308     hir_map: &'a hir::map::Map<'gcx>,
309     found_it: bool,
310     bound_region: ty::BoundRegion,
311 }
312
313 impl<'a, 'gcx, 'tcx> Visitor<'gcx> for TyPathVisitor<'a, 'gcx, 'tcx> {
314     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
315         NestedVisitorMap::OnlyBodies(&self.hir_map)
316     }
317
318     fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
319         let br_index = match self.bound_region {
320             ty::BrAnon(index) => index,
321             _ => return,
322         };
323
324
325         match self.infcx.tcx.named_region_map.defs.get(&lifetime.id) {
326             // the lifetime of the TyPath!
327             Some(&rl::Region::LateBoundAnon(debuijn_index, anon_index)) => {
328                 if debuijn_index.depth == 1 && anon_index == br_index {
329                     self.found_it = true;
330                 }
331             }
332             Some(&rl::Region::Static) |
333             Some(&rl::Region::EarlyBound(_, _)) |
334             Some(&rl::Region::LateBound(_, _)) |
335             Some(&rl::Region::Free(_, _)) |
336             None => {
337                 debug!("no arg found");
338             }
339         }
340     }
341
342     fn visit_ty(&mut self, arg: &'gcx hir::Ty) {
343         // ignore nested types
344         //
345         // If you have a type like `Foo<'a, &Ty>` we
346         // are only interested in the immediate lifetimes ('a).
347         //
348         // Making `visit_ty` empty will ignore the `&Ty` embedded
349         // inside, it will get reached by the outer visitor.
350         debug!("`Ty` corresponding to a struct is {:?}", arg);
351     }
352 }