]> git.lizzy.rs Git - rust.git/blob - src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs
Rollup merge of #71330 - ecstatic-morse:const-qualif-lazy, r=oli-obk
[rust.git] / src / librustc_infer / infer / error_reporting / nice_region_error / find_anon_type.rs
1 use crate::infer::error_reporting::nice_region_error::NiceRegionError;
2 use rustc_hir as hir;
3 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
4 use rustc_hir::Node;
5 use rustc_middle::hir::map::Map;
6 use rustc_middle::middle::resolve_lifetime as rl;
7 use rustc_middle::ty::{self, Region, TyCtxt};
8
9 impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
10     /// This function calls the `visit_ty` method for the parameters
11     /// corresponding to the anonymous regions. The `nested_visitor.found_type`
12     /// contains the anonymous type.
13     ///
14     /// # Arguments
15     /// region - the anonymous region corresponding to the anon_anon conflict
16     /// br - the bound region corresponding to the above region which is of type `BrAnon(_)`
17     ///
18     /// # Example
19     /// ```
20     /// fn foo(x: &mut Vec<&u8>, y: &u8)
21     ///    { x.push(y); }
22     /// ```
23     /// The function returns the nested type corresponding to the anonymous region
24     /// for e.g., `&u8` and Vec<`&u8`.
25     pub(super) fn find_anon_type(
26         &self,
27         region: Region<'tcx>,
28         br: &ty::BoundRegion,
29     ) -> Option<(&hir::Ty<'_>, &hir::FnDecl<'_>)> {
30         if let Some(anon_reg) = self.tcx().is_suitable_region(region) {
31             let def_id = anon_reg.def_id;
32             if let Some(def_id) = def_id.as_local() {
33                 let hir_id = self.tcx().hir().as_local_hir_id(def_id);
34                 let fndecl = match self.tcx().hir().get(hir_id) {
35                     Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. })
36                     | Node::TraitItem(&hir::TraitItem {
37                         kind: hir::TraitItemKind::Fn(ref m, ..),
38                         ..
39                     })
40                     | Node::ImplItem(&hir::ImplItem {
41                         kind: hir::ImplItemKind::Fn(ref m, ..),
42                         ..
43                     }) => &m.decl,
44                     _ => return None,
45                 };
46
47                 return fndecl
48                     .inputs
49                     .iter()
50                     .filter_map(|arg| self.find_component_for_bound_region(arg, br))
51                     .next()
52                     .map(|ty| (ty, &**fndecl));
53             }
54         }
55         None
56     }
57
58     // This method creates a FindNestedTypeVisitor which returns the type corresponding
59     // to the anonymous region.
60     fn find_component_for_bound_region(
61         &self,
62         arg: &'tcx hir::Ty<'tcx>,
63         br: &ty::BoundRegion,
64     ) -> Option<&'tcx hir::Ty<'tcx>> {
65         let mut nested_visitor = FindNestedTypeVisitor {
66             tcx: self.tcx(),
67             bound_region: *br,
68             found_type: None,
69             current_index: ty::INNERMOST,
70         };
71         nested_visitor.visit_ty(arg);
72         nested_visitor.found_type
73     }
74 }
75
76 // The FindNestedTypeVisitor captures the corresponding `hir::Ty` of the
77 // anonymous region. The example above would lead to a conflict between
78 // the two anonymous lifetimes for &u8 in x and y respectively. This visitor
79 // would be invoked twice, once for each lifetime, and would
80 // walk the types like &mut Vec<&u8> and &u8 looking for the HIR
81 // where that lifetime appears. This allows us to highlight the
82 // specific part of the type in the error message.
83 struct FindNestedTypeVisitor<'tcx> {
84     tcx: TyCtxt<'tcx>,
85     // The bound_region corresponding to the Refree(freeregion)
86     // associated with the anonymous region we are looking for.
87     bound_region: ty::BoundRegion,
88     // The type where the anonymous lifetime appears
89     // for e.g., Vec<`&u8`> and <`&u8`>
90     found_type: Option<&'tcx hir::Ty<'tcx>>,
91     current_index: ty::DebruijnIndex,
92 }
93
94 impl Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
95     type Map = Map<'tcx>;
96
97     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
98         NestedVisitorMap::OnlyBodies(self.tcx.hir())
99     }
100
101     fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
102         match arg.kind {
103             hir::TyKind::BareFn(_) => {
104                 self.current_index.shift_in(1);
105                 intravisit::walk_ty(self, arg);
106                 self.current_index.shift_out(1);
107                 return;
108             }
109
110             hir::TyKind::TraitObject(bounds, _) => {
111                 for bound in bounds {
112                     self.current_index.shift_in(1);
113                     self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
114                     self.current_index.shift_out(1);
115                 }
116             }
117
118             hir::TyKind::Rptr(ref lifetime, _) => {
119                 // the lifetime of the TyRptr
120                 let hir_id = lifetime.hir_id;
121                 match (self.tcx.named_region(hir_id), self.bound_region) {
122                     // Find the index of the anonymous region that was part of the
123                     // error. We will then search the function parameters for a bound
124                     // region at the right depth with the same index
125                     (
126                         Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)),
127                         ty::BrAnon(br_index),
128                     ) => {
129                         debug!(
130                             "LateBoundAnon depth = {:?} anon_index = {:?} br_index={:?}",
131                             debruijn_index, anon_index, br_index
132                         );
133                         if debruijn_index == self.current_index && anon_index == br_index {
134                             self.found_type = Some(arg);
135                             return; // we can stop visiting now
136                         }
137                     }
138
139                     // Find the index of the named region that was part of the
140                     // error. We will then search the function parameters for a bound
141                     // region at the right depth with the same index
142                     (Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
143                         debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
144                         if id == def_id {
145                             self.found_type = Some(arg);
146                             return; // we can stop visiting now
147                         }
148                     }
149
150                     // Find the index of the named region that was part of the
151                     // error. We will then search the function parameters for a bound
152                     // region at the right depth with the same index
153                     (
154                         Some(rl::Region::LateBound(debruijn_index, id, _)),
155                         ty::BrNamed(def_id, _),
156                     ) => {
157                         debug!(
158                             "FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
159                             debruijn_index
160                         );
161                         debug!("LateBound id={:?} def_id={:?}", id, def_id);
162                         if debruijn_index == self.current_index && id == def_id {
163                             self.found_type = Some(arg);
164                             return; // we can stop visiting now
165                         }
166                     }
167
168                     (
169                         Some(
170                             rl::Region::Static
171                             | rl::Region::Free(_, _)
172                             | rl::Region::EarlyBound(_, _, _)
173                             | rl::Region::LateBound(_, _, _)
174                             | rl::Region::LateBoundAnon(_, _),
175                         )
176                         | None,
177                         _,
178                     ) => {
179                         debug!("no arg found");
180                     }
181                 }
182             }
183             // Checks if it is of type `hir::TyKind::Path` which corresponds to a struct.
184             hir::TyKind::Path(_) => {
185                 let subvisitor = &mut TyPathVisitor {
186                     tcx: self.tcx,
187                     found_it: false,
188                     bound_region: self.bound_region,
189                     current_index: self.current_index,
190                 };
191                 intravisit::walk_ty(subvisitor, arg); // call walk_ty; as visit_ty is empty,
192                 // this will visit only outermost type
193                 if subvisitor.found_it {
194                     self.found_type = Some(arg);
195                 }
196             }
197             _ => {}
198         }
199         // walk the embedded contents: e.g., if we are visiting `Vec<&Foo>`,
200         // go on to visit `&Foo`
201         intravisit::walk_ty(self, arg);
202     }
203 }
204
205 // The visitor captures the corresponding `hir::Ty` of the anonymous region
206 // in the case of structs ie. `hir::TyKind::Path`.
207 // This visitor would be invoked for each lifetime corresponding to a struct,
208 // and would walk the types like Vec<Ref> in the above example and Ref looking for the HIR
209 // where that lifetime appears. This allows us to highlight the
210 // specific part of the type in the error message.
211 struct TyPathVisitor<'tcx> {
212     tcx: TyCtxt<'tcx>,
213     found_it: bool,
214     bound_region: ty::BoundRegion,
215     current_index: ty::DebruijnIndex,
216 }
217
218 impl Visitor<'tcx> for TyPathVisitor<'tcx> {
219     type Map = Map<'tcx>;
220
221     fn nested_visit_map(&mut self) -> NestedVisitorMap<Map<'tcx>> {
222         NestedVisitorMap::OnlyBodies(self.tcx.hir())
223     }
224
225     fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
226         match (self.tcx.named_region(lifetime.hir_id), self.bound_region) {
227             // the lifetime of the TyPath!
228             (Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)), ty::BrAnon(br_index)) => {
229                 if debruijn_index == self.current_index && anon_index == br_index {
230                     self.found_it = true;
231                     return;
232                 }
233             }
234
235             (Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
236                 debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
237                 if id == def_id {
238                     self.found_it = true;
239                     return; // we can stop visiting now
240                 }
241             }
242
243             (Some(rl::Region::LateBound(debruijn_index, id, _)), ty::BrNamed(def_id, _)) => {
244                 debug!("FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}", debruijn_index,);
245                 debug!("id={:?}", id);
246                 debug!("def_id={:?}", def_id);
247                 if debruijn_index == self.current_index && id == def_id {
248                     self.found_it = true;
249                     return; // we can stop visiting now
250                 }
251             }
252
253             (
254                 Some(
255                     rl::Region::Static
256                     | rl::Region::EarlyBound(_, _, _)
257                     | rl::Region::LateBound(_, _, _)
258                     | rl::Region::LateBoundAnon(_, _)
259                     | rl::Region::Free(_, _),
260                 )
261                 | None,
262                 _,
263             ) => {
264                 debug!("no arg found");
265             }
266         }
267     }
268
269     fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
270         // ignore nested types
271         //
272         // If you have a type like `Foo<'a, &Ty>` we
273         // are only interested in the immediate lifetimes ('a).
274         //
275         // Making `visit_ty` empty will ignore the `&Ty` embedded
276         // inside, it will get reached by the outer visitor.
277         debug!("`Ty` corresponding to a struct is {:?}", arg);
278     }
279 }