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.
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.
11 //! Error Reporting for Anonymous Region Lifetime Errors
12 //! where both the regions are anonymous.
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};
22 impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
23 // This method prints the error message for lifetime errors when both the concerned regions
25 // Consider a case where we have
26 // fn foo(x: &mut Vec<&u8>, y: &u8)
29 // fn foo(x: &mut Vec<&u8>, y: &u8) {
30 // --- --- these references are declared with different lifetimes...
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...
39 // ^ ...but data from `y` flows into `x` here
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
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)
71 return false; //inapplicable
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)) {
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) {
84 if self.is_return_type_anon(scope_def_id_1, bregion1) ||
85 self.is_return_type_anon(scope_def_id_2, bregion2) {
92 if anon_arg1 == anon_arg2 {
93 (format!(" with one lifetime"), format!(" into the other"))
95 let span_label_var1 = if let Some(simple_name) = anon_arg1.pat.simple_name() {
96 format!(" from `{}`", simple_name)
101 let span_label_var2 = if let Some(simple_name) = anon_arg2.pat.simple_name() {
102 format!(" into `{}`", simple_name)
107 (span_label_var1, span_label_var2)
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))
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.
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(_)`
134 /// fn foo(x: &mut Vec<&u8>, y: &u8)
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 {
151 self.find_visitor_found_type(&**arg, br)
155 } else if let hir_map::NodeTraitItem(it) = self.tcx.hir.get(node_id) {
156 if let hir::TraitItemKind::Method(ref fndecl, _) = it.node {
162 self.find_visitor_found_type(&**arg, br)
166 } else if let hir_map::NodeImplItem(it) = self.tcx.hir.get(node_id) {
167 if let hir::ImplItemKind::Method(ref fndecl, _) = it.node {
173 self.find_visitor_found_type(&**arg, br)
184 // This method creates a FindNestedTypeVisitor which returns the type corresponding
185 // to the anonymous region.
186 fn find_visitor_found_type(&self,
188 br: &ty::BoundRegion)
189 -> Option<(&'gcx hir::Ty)> {
190 let mut nested_visitor = FindNestedTypeVisitor {
192 hir_map: &self.tcx.hir,
197 nested_visitor.visit_ty(arg);
198 nested_visitor.found_type
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>,
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)
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,
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
245 Some(&rl::Region::Static) |
246 Some(&rl::Region::EarlyBound(_, _)) |
247 Some(&rl::Region::LateBound(_, _)) |
248 Some(&rl::Region::Free(_, _)) |
250 debug!("no arg found");
254 // Checks if it is of type `hir::TyPath` which corresponds to a struct.
256 let subvisitor = &mut TyPathVisitor {
259 bound_region: self.bound_region,
260 hir_map: self.hir_map,
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);
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) {
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
279 Some(&rl::Region::Static) |
280 Some(&rl::Region::EarlyBound(_, _)) |
281 Some(&rl::Region::LateBound(_, _)) |
282 Some(&rl::Region::Free(_, _)) |
284 debug!("no arg found");
292 // walk the embedded contents: e.g., if we are visiting `Vec<&Foo>`,
293 // go on to visit `&Foo`
295 intravisit::walk_ty(self, arg);
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>,
310 bound_region: ty::BoundRegion,
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)
318 fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
319 let br_index = match self.bound_region {
320 ty::BrAnon(index) => index,
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;
332 Some(&rl::Region::Static) |
333 Some(&rl::Region::EarlyBound(_, _)) |
334 Some(&rl::Region::LateBound(_, _)) |
335 Some(&rl::Region::Free(_, _)) |
337 debug!("no arg found");
342 fn visit_ty(&mut self, arg: &'gcx hir::Ty) {
343 // ignore nested types
345 // If you have a type like `Foo<'a, &Ty>` we
346 // are only interested in the immediate lifetimes ('a).
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);