]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs
Rollup merge of #51760 - GuillaumeGomez:add-another-partialeq-example, r=QuietMisdreavus
[rust.git] / src / librustc / infer / error_reporting / nice_region_error / find_anon_type.rs
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.
4 //
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.
10
11 use hir;
12 use ty::{self, Region, TyCtxt};
13 use hir::Node;
14 use middle::resolve_lifetime as rl;
15 use hir::intravisit::{self, NestedVisitorMap, Visitor};
16 use infer::error_reporting::nice_region_error::NiceRegionError;
17
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.
22     ///
23     /// # Arguments
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(_)`
26     ///
27     /// # Example
28     /// ```
29     /// fn foo(x: &mut Vec<&u8>, y: &u8)
30     ///    { x.push(y); }
31     /// ```
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(
35         &self,
36         region: Region<'tcx>,
37         br: &ty::BoundRegion,
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, ..),
45                         ..
46                     }) => &fndecl,
47                     Node::TraitItem(&hir::TraitItem {
48                         node: hir::TraitItemKind::Method(ref m, ..),
49                         ..
50                     })
51                     | Node::ImplItem(&hir::ImplItem {
52                         node: hir::ImplItemKind::Method(ref m, ..),
53                         ..
54                     }) => &m.decl,
55                     _ => return None,
56                 };
57
58                 return fndecl
59                     .inputs
60                     .iter()
61                     .filter_map(|arg| self.find_component_for_bound_region(arg, br))
62                     .next()
63                     .map(|ty| (ty, &**fndecl));
64             }
65         }
66         None
67     }
68
69     // This method creates a FindNestedTypeVisitor which returns the type corresponding
70     // to the anonymous region.
71     fn find_component_for_bound_region(
72         &self,
73         arg: &'gcx hir::Ty,
74         br: &ty::BoundRegion,
75     ) -> Option<(&'gcx hir::Ty)> {
76         let mut nested_visitor = FindNestedTypeVisitor {
77             tcx: self.tcx,
78             bound_region: *br,
79             found_type: None,
80             current_index: ty::INNERMOST,
81         };
82         nested_visitor.visit_ty(arg);
83         nested_visitor.found_type
84     }
85 }
86
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,
103 }
104
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)
108     }
109
110     fn visit_ty(&mut self, arg: &'gcx hir::Ty) {
111         match arg.node {
112             hir::TyKind::BareFn(_) => {
113                 self.current_index.shift_in(1);
114                 intravisit::walk_ty(self, arg);
115                 self.current_index.shift_out(1);
116                 return;
117             }
118
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);
123             },
124
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
132                     (
133                         Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)),
134                         ty::BrAnon(br_index),
135                     ) => {
136                         debug!(
137                             "LateBoundAnon depth = {:?} anon_index = {:?} br_index={:?}",
138                             debruijn_index,
139                             anon_index,
140                             br_index
141                         );
142                         if debruijn_index == self.current_index && anon_index == br_index {
143                             self.found_type = Some(arg);
144                             return; // we can stop visiting now
145                         }
146                     }
147
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, _)) => {
152                         debug!(
153                             "EarlyBound self.infcx.tcx.hir.local_def_id(id)={:?} \
154                              def_id={:?}",
155                             id,
156                             def_id
157                         );
158                         if id == def_id {
159                             self.found_type = Some(arg);
160                             return; // we can stop visiting now
161                         }
162                     }
163
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
167                     (
168                         Some(rl::Region::LateBound(debruijn_index, id, _)),
169                         ty::BrNamed(def_id, _),
170                     ) => {
171                         debug!(
172                             "FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
173                             debruijn_index
174                         );
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
180                         }
181                     }
182
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(_, _)), _)
188                     | (None, _) => {
189                         debug!("no arg found");
190                     }
191                 }
192             }
193             // Checks if it is of type `hir::TyKind::Path` which corresponds to a struct.
194             hir::TyKind::Path(_) => {
195                 let subvisitor = &mut TyPathVisitor {
196                     tcx: self.tcx,
197                     found_it: false,
198                     bound_region: self.bound_region,
199                     current_index: self.current_index,
200                 };
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);
205                 }
206             }
207             _ => {}
208         }
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);
212     }
213 }
214
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>,
223     found_it: bool,
224     bound_region: ty::BoundRegion,
225     current_index: ty::DebruijnIndex,
226 }
227
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)
231     }
232
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;
240                     return;
241                 }
242             }
243
244             (Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
245                 debug!(
246                     "EarlyBound self.infcx.tcx.hir.local_def_id(id)={:?} \
247                      def_id={:?}",
248                     id,
249                     def_id
250                 );
251                 if id == def_id {
252                     self.found_it = true;
253                     return; // we can stop visiting now
254                 }
255             }
256
257             (Some(rl::Region::LateBound(debruijn_index, id, _)), ty::BrNamed(def_id, _)) => {
258                 debug!(
259                     "FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
260                     debruijn_index,
261                 );
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
267                 }
268             }
269
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(_, _)), _)
275             | (None, _) => {
276                 debug!("no arg found");
277             }
278         }
279     }
280
281     fn visit_ty(&mut self, arg: &'gcx hir::Ty) {
282         // ignore nested types
283         //
284         // If you have a type like `Foo<'a, &Ty>` we
285         // are only interested in the immediate lifetimes ('a).
286         //
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);
290     }
291 }