|
1 | | -use crate::infer::error_reporting::nice_region_error::NiceRegionError; |
2 | 1 | use rustc_hir as hir; |
3 | 2 | use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; |
4 | 3 | use rustc_hir::Node; |
5 | 4 | use rustc_middle::hir::map::Map; |
6 | 5 | use rustc_middle::middle::resolve_lifetime as rl; |
7 | 6 | use rustc_middle::ty::{self, Region, TyCtxt}; |
8 | 7 |
|
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::BoundRegionKind, |
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 | | - }; |
| 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. |
| 11 | +/// |
| 12 | +/// # Arguments |
| 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(_)` |
| 15 | +/// |
| 16 | +/// # Example |
| 17 | +/// ``` |
| 18 | +/// fn foo(x: &mut Vec<&u8>, y: &u8) |
| 19 | +/// { x.push(y); } |
| 20 | +/// ``` |
| 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( |
| 24 | + tcx: TyCtxt<'tcx>, |
| 25 | + region: Region<'tcx>, |
| 26 | + br: &ty::BoundRegionKind, |
| 27 | +) -> Option<(&'tcx hir::Ty<'tcx>, &'tcx hir::FnDecl<'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 fndecl = match tcx.hir().get(hir_id) { |
| 31 | + Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. }) |
| 32 | + | Node::TraitItem(&hir::TraitItem { |
| 33 | + kind: hir::TraitItemKind::Fn(ref m, ..), .. |
| 34 | + }) |
| 35 | + | Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(ref m, ..), .. }) => { |
| 36 | + &m.decl |
| 37 | + } |
| 38 | + _ => return None, |
| 39 | + }; |
43 | 40 |
|
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 | | - } |
| 41 | + fndecl |
| 42 | + .inputs |
| 43 | + .iter() |
| 44 | + .find_map(|arg| find_component_for_bound_region(tcx, arg, br)) |
| 45 | + .map(|ty| (ty, &**fndecl)) |
| 46 | + } else { |
| 47 | + None |
52 | 48 | } |
| 49 | +} |
53 | 50 |
|
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::BoundRegionKind, |
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 | | - } |
| 51 | +// This method creates a FindNestedTypeVisitor which returns the type corresponding |
| 52 | +// to the anonymous region. |
| 53 | +fn find_component_for_bound_region( |
| 54 | + tcx: TyCtxt<'tcx>, |
| 55 | + arg: &'tcx hir::Ty<'tcx>, |
| 56 | + br: &ty::BoundRegionKind, |
| 57 | +) -> Option<&'tcx hir::Ty<'tcx>> { |
| 58 | + let mut nested_visitor = FindNestedTypeVisitor { |
| 59 | + tcx, |
| 60 | + bound_region: *br, |
| 61 | + found_type: None, |
| 62 | + current_index: ty::INNERMOST, |
| 63 | + }; |
| 64 | + nested_visitor.visit_ty(arg); |
| 65 | + nested_visitor.found_type |
70 | 66 | } |
71 | 67 |
|
72 | 68 | // The FindNestedTypeVisitor captures the corresponding `hir::Ty` of the |
|
0 commit comments