2 use rustc_hir::intravisit::{self, Visitor};
3 use rustc_middle::hir::map::Map;
4 use rustc_middle::hir::nested_filter;
5 use rustc_middle::middle::resolve_lifetime as rl;
6 use rustc_middle::ty::{self, Region, TyCtxt};
8 /// This function calls the `visit_ty` method for the parameters
9 /// corresponding to the anonymous regions. The `nested_visitor.found_type`
10 /// contains the anonymous type.
13 /// region - the anonymous region corresponding to the anon_anon conflict
14 /// br - the bound region corresponding to the above region which is of type `BrAnon(_)`
18 /// fn foo(x: &mut Vec<&u8>, y: &u8)
21 /// The function returns the nested type corresponding to the anonymous region
22 /// for e.g., `&u8` and `Vec<&u8>`.
23 pub(crate) fn find_anon_type<'tcx>(
26 br: &ty::BoundRegionKind,
27 ) -> Option<(&'tcx hir::Ty<'tcx>, &'tcx hir::FnSig<'tcx>)> {
28 if let Some(anon_reg) = tcx.is_suitable_region(region) {
29 let hir_id = tcx.hir().local_def_id_to_hir_id(anon_reg.def_id);
30 let Some(fn_sig) = tcx.hir().get(hir_id).fn_sig() else {
38 .find_map(|arg| find_component_for_bound_region(tcx, arg, br))
39 .map(|ty| (ty, fn_sig))
45 // This method creates a FindNestedTypeVisitor which returns the type corresponding
46 // to the anonymous region.
47 fn find_component_for_bound_region<'tcx>(
49 arg: &'tcx hir::Ty<'tcx>,
50 br: &ty::BoundRegionKind,
51 ) -> Option<&'tcx hir::Ty<'tcx>> {
52 let mut nested_visitor = FindNestedTypeVisitor {
56 current_index: ty::INNERMOST,
58 nested_visitor.visit_ty(arg);
59 nested_visitor.found_type
62 // The FindNestedTypeVisitor captures the corresponding `hir::Ty` of the
63 // anonymous region. The example above would lead to a conflict between
64 // the two anonymous lifetimes for &u8 in x and y respectively. This visitor
65 // would be invoked twice, once for each lifetime, and would
66 // walk the types like &mut Vec<&u8> and &u8 looking for the HIR
67 // where that lifetime appears. This allows us to highlight the
68 // specific part of the type in the error message.
69 struct FindNestedTypeVisitor<'tcx> {
71 // The bound_region corresponding to the Refree(freeregion)
72 // associated with the anonymous region we are looking for.
73 bound_region: ty::BoundRegionKind,
74 // The type where the anonymous lifetime appears
75 // for e.g., Vec<`&u8`> and <`&u8`>
76 found_type: Option<&'tcx hir::Ty<'tcx>>,
77 current_index: ty::DebruijnIndex,
80 impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
81 type NestedFilter = nested_filter::OnlyBodies;
83 fn nested_visit_map(&mut self) -> Self::Map {
87 fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
89 hir::TyKind::BareFn(_) => {
90 self.current_index.shift_in(1);
91 intravisit::walk_ty(self, arg);
92 self.current_index.shift_out(1);
96 hir::TyKind::TraitObject(bounds, ..) => {
98 self.current_index.shift_in(1);
99 self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
100 self.current_index.shift_out(1);
104 hir::TyKind::Rptr(ref lifetime, _) => {
105 // the lifetime of the TyRptr
106 let hir_id = lifetime.hir_id;
107 match (self.tcx.named_region(hir_id), self.bound_region) {
108 // Find the index of the anonymous region that was part of the
109 // error. We will then search the function parameters for a bound
110 // region at the right depth with the same index
112 Some(rl::Region::LateBoundAnon(debruijn_index, _, anon_index)),
113 ty::BrAnon(br_index),
116 "LateBoundAnon depth = {:?} anon_index = {:?} br_index={:?}",
117 debruijn_index, anon_index, br_index
119 if debruijn_index == self.current_index && anon_index == br_index {
120 self.found_type = Some(arg);
121 return; // we can stop visiting now
125 // Find the index of the named region that was part of the
126 // error. We will then search the function parameters for a bound
127 // region at the right depth with the same index
128 (Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
129 debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
131 self.found_type = Some(arg);
132 return; // we can stop visiting now
136 // Find the index of the named region that was part of the
137 // error. We will then search the function parameters for a bound
138 // region at the right depth with the same index
140 Some(rl::Region::LateBound(debruijn_index, _, id, _)),
141 ty::BrNamed(def_id, _),
144 "FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
147 debug!("LateBound id={:?} def_id={:?}", id, def_id);
148 if debruijn_index == self.current_index && id == def_id {
149 self.found_type = Some(arg);
150 return; // we can stop visiting now
157 | rl::Region::Free(_, _)
158 | rl::Region::EarlyBound(_, _, _)
159 | rl::Region::LateBound(_, _, _, _)
160 | rl::Region::LateBoundAnon(_, _, _),
165 debug!("no arg found");
169 // Checks if it is of type `hir::TyKind::Path` which corresponds to a struct.
170 hir::TyKind::Path(_) => {
171 let subvisitor = &mut TyPathVisitor {
174 bound_region: self.bound_region,
175 current_index: self.current_index,
177 intravisit::walk_ty(subvisitor, arg); // call walk_ty; as visit_ty is empty,
178 // this will visit only outermost type
179 if subvisitor.found_it {
180 self.found_type = Some(arg);
185 // walk the embedded contents: e.g., if we are visiting `Vec<&Foo>`,
186 // go on to visit `&Foo`
187 intravisit::walk_ty(self, arg);
191 // The visitor captures the corresponding `hir::Ty` of the anonymous region
192 // in the case of structs ie. `hir::TyKind::Path`.
193 // This visitor would be invoked for each lifetime corresponding to a struct,
194 // and would walk the types like Vec<Ref> in the above example and Ref looking for the HIR
195 // where that lifetime appears. This allows us to highlight the
196 // specific part of the type in the error message.
197 struct TyPathVisitor<'tcx> {
200 bound_region: ty::BoundRegionKind,
201 current_index: ty::DebruijnIndex,
204 impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> {
205 type NestedFilter = nested_filter::OnlyBodies;
207 fn nested_visit_map(&mut self) -> Map<'tcx> {
211 fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
212 match (self.tcx.named_region(lifetime.hir_id), self.bound_region) {
213 // the lifetime of the TyPath!
215 Some(rl::Region::LateBoundAnon(debruijn_index, _, anon_index)),
216 ty::BrAnon(br_index),
218 if debruijn_index == self.current_index && anon_index == br_index {
219 self.found_it = true;
224 (Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
225 debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
227 self.found_it = true;
228 return; // we can stop visiting now
232 (Some(rl::Region::LateBound(debruijn_index, _, id, _)), ty::BrNamed(def_id, _)) => {
233 debug!("FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}", debruijn_index,);
234 debug!("id={:?}", id);
235 debug!("def_id={:?}", def_id);
236 if debruijn_index == self.current_index && id == def_id {
237 self.found_it = true;
238 return; // we can stop visiting now
245 | rl::Region::EarlyBound(_, _, _)
246 | rl::Region::LateBound(_, _, _, _)
247 | rl::Region::LateBoundAnon(_, _, _)
248 | rl::Region::Free(_, _),
253 debug!("no arg found");
258 fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
259 // ignore nested types
261 // If you have a type like `Foo<'a, &Ty>` we
262 // are only interested in the immediate lifetimes ('a).
264 // Making `visit_ty` empty will ignore the `&Ty` embedded
265 // inside, it will get reached by the outer visitor.
266 debug!("`Ty` corresponding to a struct is {:?}", arg);