Skip to content

Commit fece59b

Browse files
committed
Change find_anon_type method to function
1 parent 8599bff commit fece59b

File tree

4 files changed

+61
-63
lines changed

4 files changed

+61
-63
lines changed

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/different_lifetimes.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Error Reporting for Anonymous Region Lifetime Errors
22
//! where both the regions are anonymous.
33
4+
use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
45
use crate::infer::error_reporting::nice_region_error::util::AnonymousParamInfo;
56
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
67
use crate::infer::lexical_region_resolve::RegionResolutionError;
@@ -66,9 +67,9 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
6667
let scope_def_id_sub = anon_reg_sub.def_id;
6768
let bregion_sub = anon_reg_sub.boundregion;
6869

69-
let ty_sup = self.find_anon_type(sup, &bregion_sup)?;
70+
let ty_sup = find_anon_type(self.tcx(), sup, &bregion_sup)?;
7071

71-
let ty_sub = self.find_anon_type(sub, &bregion_sub)?;
72+
let ty_sub = find_anon_type(self.tcx(), sub, &bregion_sub)?;
7273

7374
debug!(
7475
"try_report_anon_anon_conflict: found_param1={:?} sup={:?} br1={:?}",

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/find_anon_type.rs

Lines changed: 55 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,68 @@
1-
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
21
use rustc_hir as hir;
32
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
43
use rustc_hir::Node;
54
use rustc_middle::hir::map::Map;
65
use rustc_middle::middle::resolve_lifetime as rl;
76
use rustc_middle::ty::{self, Region, TyCtxt};
87

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+
};
4340

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
5248
}
49+
}
5350

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
7066
}
7167

7268
// The FindNestedTypeVisitor captures the corresponding `hir::Ty` of the

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use rustc_errors::{DiagnosticBuilder, ErrorReported};
55
use rustc_middle::ty::{self, TyCtxt};
66
use rustc_span::source_map::Span;
77

8+
pub mod find_anon_type;
89
mod different_lifetimes;
9-
mod find_anon_type;
1010
mod named_anon_conflict;
1111
mod placeholder_error;
1212
mod static_impl_trait;

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Error Reporting for Anonymous Region Lifetime Errors
22
//! where one region is named and the other is anonymous.
3+
use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
34
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
45
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
56
use rustc_hir::intravisit::Visitor;
@@ -74,7 +75,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
7475
return None;
7576
}
7677

77-
if let Some((_, fndecl)) = self.find_anon_type(anon, &br) {
78+
if let Some((_, fndecl)) = find_anon_type(self.tcx(), anon, &br) {
7879
if self.is_self_anon(is_first, scope_def_id) {
7980
return None;
8081
}

0 commit comments

Comments
 (0)