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.
12 use ty::{self, Region, TyCtxt};
14 use middle::resolve_lifetime as rl;
15 use hir::intravisit::{self, NestedVisitorMap, Visitor};
16 use infer::error_reporting::nice_region_error::NiceRegionError;
18 impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
19 /// This function calls the `visit_ty` method for the parameters
20 /// corresponding to the anonymous regions. The `nested_visitor.found_type`
21 /// contains the anonymous type.
24 /// region - the anonymous region corresponding to the anon_anon conflict
25 /// br - the bound region corresponding to the above region which is of type `BrAnon(_)`
29 /// fn foo(x: &mut Vec<&u8>, y: &u8)
32 /// The function returns the nested type corresponding to the anonymous region
33 /// for e.g. `&u8` and Vec<`&u8`.
34 pub(super) fn find_anon_type(
38 ) -> Option<(&hir::Ty, &hir::FnDecl)> {
39 if let Some(anon_reg) = self.is_suitable_region(region) {
40 let def_id = anon_reg.def_id;
41 if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
42 let fndecl = match self.tcx.hir.get(node_id) {
43 Node::Item(&hir::Item {
44 node: hir::ItemKind::Fn(ref fndecl, ..),
47 Node::TraitItem(&hir::TraitItem {
48 node: hir::TraitItemKind::Method(ref m, ..),
51 | Node::ImplItem(&hir::ImplItem {
52 node: hir::ImplItemKind::Method(ref m, ..),
61 .filter_map(|arg| self.find_component_for_bound_region(arg, br))
63 .map(|ty| (ty, &**fndecl));
69 // This method creates a FindNestedTypeVisitor which returns the type corresponding
70 // to the anonymous region.
71 fn find_component_for_bound_region(
75 ) -> Option<(&'gcx hir::Ty)> {
76 let mut nested_visitor = FindNestedTypeVisitor {
80 current_index: ty::INNERMOST,
82 nested_visitor.visit_ty(arg);
83 nested_visitor.found_type
87 // The FindNestedTypeVisitor captures the corresponding `hir::Ty` of the
88 // anonymous region. The example above would lead to a conflict between
89 // the two anonymous lifetimes for &u8 in x and y respectively. This visitor
90 // would be invoked twice, once for each lifetime, and would
91 // walk the types like &mut Vec<&u8> and &u8 looking for the HIR
92 // where that lifetime appears. This allows us to highlight the
93 // specific part of the type in the error message.
94 struct FindNestedTypeVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
95 tcx: TyCtxt<'a, 'gcx, 'tcx>,
96 // The bound_region corresponding to the Refree(freeregion)
97 // associated with the anonymous region we are looking for.
98 bound_region: ty::BoundRegion,
99 // The type where the anonymous lifetime appears
100 // for e.g. Vec<`&u8`> and <`&u8`>
101 found_type: Option<&'gcx hir::Ty>,
102 current_index: ty::DebruijnIndex,
105 impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
106 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
107 NestedVisitorMap::OnlyBodies(&self.tcx.hir)
110 fn visit_ty(&mut self, arg: &'gcx hir::Ty) {
112 hir::TyKind::BareFn(_) => {
113 self.current_index.shift_in(1);
114 intravisit::walk_ty(self, arg);
115 self.current_index.shift_out(1);
119 hir::TyKind::TraitObject(ref bounds, _) => for bound in bounds {
120 self.current_index.shift_in(1);
121 self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
122 self.current_index.shift_out(1);
125 hir::TyKind::Rptr(ref lifetime, _) => {
126 // the lifetime of the TyRptr
127 let hir_id = self.tcx.hir.node_to_hir_id(lifetime.id);
128 match (self.tcx.named_region(hir_id), self.bound_region) {
129 // Find the index of the anonymous region that was part of the
130 // error. We will then search the function parameters for a bound
131 // region at the right depth with the same index
133 Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)),
134 ty::BrAnon(br_index),
137 "LateBoundAnon depth = {:?} anon_index = {:?} br_index={:?}",
142 if debruijn_index == self.current_index && anon_index == br_index {
143 self.found_type = Some(arg);
144 return; // we can stop visiting now
148 // Find the index of the named region that was part of the
149 // error. We will then search the function parameters for a bound
150 // region at the right depth with the same index
151 (Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
153 "EarlyBound self.infcx.tcx.hir.local_def_id(id)={:?} \
159 self.found_type = Some(arg);
160 return; // we can stop visiting now
164 // Find the index of the named region that was part of the
165 // error. We will then search the function parameters for a bound
166 // region at the right depth with the same index
168 Some(rl::Region::LateBound(debruijn_index, id, _)),
169 ty::BrNamed(def_id, _),
172 "FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
175 debug!("self.infcx.tcx.hir.local_def_id(id)={:?}", id);
176 debug!("def_id={:?}", def_id);
177 if debruijn_index == self.current_index && id == def_id {
178 self.found_type = Some(arg);
179 return; // we can stop visiting now
183 (Some(rl::Region::Static), _)
184 | (Some(rl::Region::Free(_, _)), _)
185 | (Some(rl::Region::EarlyBound(_, _, _)), _)
186 | (Some(rl::Region::LateBound(_, _, _)), _)
187 | (Some(rl::Region::LateBoundAnon(_, _)), _)
189 debug!("no arg found");
193 // Checks if it is of type `hir::TyKind::Path` which corresponds to a struct.
194 hir::TyKind::Path(_) => {
195 let subvisitor = &mut TyPathVisitor {
198 bound_region: self.bound_region,
199 current_index: self.current_index,
201 intravisit::walk_ty(subvisitor, arg); // call walk_ty; as visit_ty is empty,
202 // this will visit only outermost type
203 if subvisitor.found_it {
204 self.found_type = Some(arg);
209 // walk the embedded contents: e.g., if we are visiting `Vec<&Foo>`,
210 // go on to visit `&Foo`
211 intravisit::walk_ty(self, arg);
215 // The visitor captures the corresponding `hir::Ty` of the anonymous region
216 // in the case of structs ie. `hir::TyKind::Path`.
217 // This visitor would be invoked for each lifetime corresponding to a struct,
218 // and would walk the types like Vec<Ref> in the above example and Ref looking for the HIR
219 // where that lifetime appears. This allows us to highlight the
220 // specific part of the type in the error message.
221 struct TyPathVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
222 tcx: TyCtxt<'a, 'gcx, 'tcx>,
224 bound_region: ty::BoundRegion,
225 current_index: ty::DebruijnIndex,
228 impl<'a, 'gcx, 'tcx> Visitor<'gcx> for TyPathVisitor<'a, 'gcx, 'tcx> {
229 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
230 NestedVisitorMap::OnlyBodies(&self.tcx.hir)
233 fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
234 let hir_id = self.tcx.hir.node_to_hir_id(lifetime.id);
235 match (self.tcx.named_region(hir_id), self.bound_region) {
236 // the lifetime of the TyPath!
237 (Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)), ty::BrAnon(br_index)) => {
238 if debruijn_index == self.current_index && anon_index == br_index {
239 self.found_it = true;
244 (Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
246 "EarlyBound self.infcx.tcx.hir.local_def_id(id)={:?} \
252 self.found_it = true;
253 return; // we can stop visiting now
257 (Some(rl::Region::LateBound(debruijn_index, id, _)), ty::BrNamed(def_id, _)) => {
259 "FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
262 debug!("id={:?}", id);
263 debug!("def_id={:?}", def_id);
264 if debruijn_index == self.current_index && id == def_id {
265 self.found_it = true;
266 return; // we can stop visiting now
270 (Some(rl::Region::Static), _)
271 | (Some(rl::Region::EarlyBound(_, _, _)), _)
272 | (Some(rl::Region::LateBound(_, _, _)), _)
273 | (Some(rl::Region::LateBoundAnon(_, _)), _)
274 | (Some(rl::Region::Free(_, _)), _)
276 debug!("no arg found");
281 fn visit_ty(&mut self, arg: &'gcx hir::Ty) {
282 // ignore nested types
284 // If you have a type like `Foo<'a, &Ty>` we
285 // are only interested in the immediate lifetimes ('a).
287 // Making `visit_ty` empty will ignore the `&Ty` embedded
288 // inside, it will get reached by the outer visitor.
289 debug!("`Ty` corresponding to a struct is {:?}", arg);