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