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(_)`
17 /// ```compile_fail,E0623
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 fn find_anon_type<'tcx>(
26 br: &ty::BoundRegionKind,
27 ) -> Option<(&'tcx hir::Ty<'tcx>, &'tcx hir::FnSig<'tcx>)> {
28 let 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 fn_sig = tcx.hir().get(hir_id).fn_sig()?;
36 .find_map(|arg| find_component_for_bound_region(tcx, arg, br))
37 .map(|ty| (ty, fn_sig))
40 // This method creates a FindNestedTypeVisitor which returns the type corresponding
41 // to the anonymous region.
42 fn find_component_for_bound_region<'tcx>(
44 arg: &'tcx hir::Ty<'tcx>,
45 br: &ty::BoundRegionKind,
46 ) -> Option<&'tcx hir::Ty<'tcx>> {
47 let mut nested_visitor = FindNestedTypeVisitor {
51 current_index: ty::INNERMOST,
53 nested_visitor.visit_ty(arg);
54 nested_visitor.found_type
57 // The FindNestedTypeVisitor captures the corresponding `hir::Ty` of the
58 // anonymous region. The example above would lead to a conflict between
59 // the two anonymous lifetimes for &u8 in x and y respectively. This visitor
60 // would be invoked twice, once for each lifetime, and would
61 // walk the types like &mut Vec<&u8> and &u8 looking for the HIR
62 // where that lifetime appears. This allows us to highlight the
63 // specific part of the type in the error message.
64 struct FindNestedTypeVisitor<'tcx> {
66 // The bound_region corresponding to the Refree(freeregion)
67 // associated with the anonymous region we are looking for.
68 bound_region: ty::BoundRegionKind,
69 // The type where the anonymous lifetime appears
70 // for e.g., Vec<`&u8`> and <`&u8`>
71 found_type: Option<&'tcx hir::Ty<'tcx>>,
72 current_index: ty::DebruijnIndex,
75 impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
76 type NestedFilter = nested_filter::OnlyBodies;
78 fn nested_visit_map(&mut self) -> Self::Map {
82 fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
84 hir::TyKind::BareFn(_) => {
85 self.current_index.shift_in(1);
86 intravisit::walk_ty(self, arg);
87 self.current_index.shift_out(1);
91 hir::TyKind::TraitObject(bounds, ..) => {
93 self.current_index.shift_in(1);
94 self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
95 self.current_index.shift_out(1);
99 hir::TyKind::Rptr(ref lifetime, _) => {
100 // the lifetime of the TyRptr
101 let hir_id = lifetime.hir_id;
102 match (self.tcx.named_region(hir_id), self.bound_region) {
103 // Find the index of the named region that was part of the
104 // error. We will then search the function parameters for a bound
105 // region at the right depth with the same index
106 (Some(rl::Region::EarlyBound(_, id)), ty::BrNamed(def_id, _)) => {
107 debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
109 self.found_type = Some(arg);
110 return; // we can stop visiting now
114 // Find the index of the named region that was part of the
115 // error. We will then search the function parameters for a bound
116 // region at the right depth with the same index
118 Some(rl::Region::LateBound(debruijn_index, _, id)),
119 ty::BrNamed(def_id, _),
122 "FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
125 debug!("LateBound id={:?} def_id={:?}", id, def_id);
126 if debruijn_index == self.current_index && id == def_id {
127 self.found_type = Some(arg);
128 return; // we can stop visiting now
135 | rl::Region::Free(_, _)
136 | rl::Region::EarlyBound(_, _)
137 | rl::Region::LateBound(_, _, _),
142 debug!("no arg found");
146 // Checks if it is of type `hir::TyKind::Path` which corresponds to a struct.
147 hir::TyKind::Path(_) => {
148 let subvisitor = &mut TyPathVisitor {
151 bound_region: self.bound_region,
152 current_index: self.current_index,
154 intravisit::walk_ty(subvisitor, arg); // call walk_ty; as visit_ty is empty,
155 // this will visit only outermost type
156 if subvisitor.found_it {
157 self.found_type = Some(arg);
162 // walk the embedded contents: e.g., if we are visiting `Vec<&Foo>`,
163 // go on to visit `&Foo`
164 intravisit::walk_ty(self, arg);
168 // The visitor captures the corresponding `hir::Ty` of the anonymous region
169 // in the case of structs ie. `hir::TyKind::Path`.
170 // This visitor would be invoked for each lifetime corresponding to a struct,
171 // and would walk the types like Vec<Ref> in the above example and Ref looking for the HIR
172 // where that lifetime appears. This allows us to highlight the
173 // specific part of the type in the error message.
174 struct TyPathVisitor<'tcx> {
177 bound_region: ty::BoundRegionKind,
178 current_index: ty::DebruijnIndex,
181 impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> {
182 type NestedFilter = nested_filter::OnlyBodies;
184 fn nested_visit_map(&mut self) -> Map<'tcx> {
188 fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
189 match (self.tcx.named_region(lifetime.hir_id), self.bound_region) {
190 // the lifetime of the TyPath!
191 (Some(rl::Region::EarlyBound(_, id)), ty::BrNamed(def_id, _)) => {
192 debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
194 self.found_it = true;
195 return; // we can stop visiting now
199 (Some(rl::Region::LateBound(debruijn_index, _, id)), ty::BrNamed(def_id, _)) => {
200 debug!("FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}", debruijn_index,);
201 debug!("id={:?}", id);
202 debug!("def_id={:?}", def_id);
203 if debruijn_index == self.current_index && id == def_id {
204 self.found_it = true;
205 return; // we can stop visiting now
212 | rl::Region::EarlyBound(_, _)
213 | rl::Region::LateBound(_, _, _)
214 | rl::Region::Free(_, _),
219 debug!("no arg found");
224 fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
225 // ignore nested types
227 // If you have a type like `Foo<'a, &Ty>` we
228 // are only interested in the immediate lifetimes ('a).
230 // Making `visit_ty` empty will ignore the `&Ty` embedded
231 // inside, it will get reached by the outer visitor.
232 debug!("`Ty` corresponding to a struct is {:?}", arg);